Re: Speeding up select distinct

Поиск
Список
Период
Сортировка
От Merlin Moncure
Тема Re: Speeding up select distinct
Дата
Msg-id 6EE64EF3AB31D5448D0007DD34EEB3412A7650@Herge.rcsinc.local
обсуждение исходный текст
Ответ на Speeding up select distinct  (Laurent Martelli <laurent@aopsys.com>)
Ответы Re: Speeding up select distinct
Список 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!

В списке pgsql-performance по дате отправления:

Предыдущее
От: Rod Taylor
Дата:
Сообщение: Re: Speeding up select distinct
Следующее
От: Laurent Martelli
Дата:
Сообщение: Re: Speeding up select distinct