Re: count distinct slow?

Поиск
Список
Период
Сортировка
От Tom Lane
Тема Re: count distinct slow?
Дата
Msg-id 8012.1416273984@sss.pgh.pa.us
обсуждение исходный текст
Ответ на count distinct slow?  (Roger Pack <rogerdpack2@gmail.com>)
Список pgsql-general
Roger Pack <rogerdpack2@gmail.com> writes:
> As a note, I ran into the following today (doing a select distinct is fast,
> doing a count distinct is significantly slower?)

The planner appears to prefer hash aggregation for the variants of
your query wherein the DISTINCT becomes a separate plan step.  This
is evidently a good choice, with only 6192 distinct values (hence
just that many hash table entries) in 7495551 input rows.  However,
COUNT(DISTINCT), or any other aggregate with a DISTINCT tag, uses
sort-then-remove-adjacent-duplicates logic for DISTINCT.  That's
evidently a good deal slower for your data set; most likely the data
doesn't fit in your work_mem setting so the sort spills to disk.

The reason DISTINCT aggregates don't consider hash aggregation is
partly lack of round tuits but mostly that an aggregate needs to
execute in a fairly limited amount of memory, and we can't be sure
that the hash table wouldn't get unreasonably large.

            regards, tom lane


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

Предыдущее
От: Roger Pack
Дата:
Сообщение: count distinct slow?
Следующее
От: Jonathan Vanasco
Дата:
Сообщение: Re: String searching