From: Sean Davis <sdavis2@mail.nih.gov>
To: mailreg@numerixtechnology.de
Cc: Scott Marlowe <smarlowe@g2switchworks.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: cost of CREATE VIEW ... AS SELECT DISTINCT
Date: Tue, 29 Mar 2005 05:59:01 -0500
Message-ID: <140ac5a92c5f5fc2e38bd10a17e7d70f@mail.nih.gov> (raw)
In-Reply-To: <424928D8.7060705@numerixtechnology.de>
References: <42487A6D.6020506@numerixtechnology.de>
<1112047463.22988.25.camel@state.g2switchworks.com>
<424928D8.7060705@numerixtechnology.de>
On Mar 29, 2005, at 5:07 AM, T E Schmitz wrote:
> Hello Scott,
>
> Scott Marlowe wrote:
>> On Mon, 2005-03-28 at 15:43, T E Schmitz wrote:
>>> How expensive would it be to maintain the following VIEW:
>>>
>>> CREATE VIEW origin AS SELECT DISTINCT origin FROM transaktion
>>>
>>> if there is in index on transaktion.origin; the table transaktion
>>> has thousands of records and there are only a few distinct origin?
>> The cost will only be encurred when running the view. if you want
>> materialized views (which WOULD have maintenance costs) you'll have to
>
> Thank you for the pointer - that might come in handy for another
> scenario.
>
>> The cost of executing that view should be the same as the cost of
>> running the query by hand.
>
> I did an EXPLAIN ANALYZE and a sequential scan was carried out despite
> the index I had on the column. Maybe this is because I only have very
> few records in my test DB.
>
> Would the "SELECT DISTINCT origin" always cause a sequential table
> scan regardless whether there is an index on the origin column or not?
>
I think you are right. If this is expensive and run often, you could
always normalize further and create a table of "unique_origin" that
would have only unique origins and set transaktion to have a foreign
key referring to the unique_origin table and then just query the
unique_origin table when you need to do the query above.
Sean
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: sdavis2@mail.nih.gov, mailreg@numerixtechnology.de, smarlowe@g2switchworks.com
Subject: Re: cost of CREATE VIEW ... AS SELECT DISTINCT
In-Reply-To: <140ac5a92c5f5fc2e38bd10a17e7d70f@mail.nih.gov>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox