Re: Speeding up select distinct

From: "Merlin Moncure" <merlin(dot)moncure(at)rcsonline(dot)com>
To: "Laurent Martelli" <laurent(at)aopsys(dot)com>
Cc: <pgsql-performance(at)postgresql(dot)org>
Subject: Re: Speeding up select distinct
Date: 2005-03-16 18:19:09
Message-ID: 6EE64EF3AB31D5448D0007DD34EEB3412A7650@Herge.rcsinc.local
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

> Consider this query:
>
> SELECT distinct owner from pictures;

[...]
> Any ideas, apart from more or less manually maintaining a list of
> distinct owners in another table ?

you answered your own question. With a 20 row owners table, you should
be directing your efforts there group by is faster than distinct, but
both are very wasteful and essentially require s full seqscan of the
detail table.

With a little hacking, you can change 'manual maintenance' to 'automatic
maintenance'.

1. create table owner as select distinct owner from pictures;
2. alter table owner add constraint owner_pkey(owner);
3. alter table pictures add constraint ri_picture_owner(owner)
references owner;
4. make a little append_ownder function which adds an owner to the owner
table if there is not already one there. Inline this to your insert
statement on pictures.

Voila!
Merlin
p.s. normalize your data always!

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Laurent Martelli 2005-03-16 18:29:54 Re: Speeding up select distinct
Previous Message Rod Taylor 2005-03-16 18:10:23 Re: Speeding up select distinct