Re: Group by more efficient than distinct?

From: Francisco Reyes <lists(at)stringsutils(dot)com>
To: Gregory Stark <stark(at)enterprisedb(dot)com>
Cc: Pgsql performance <pgsql-performance(at)postgresql(dot)org>
Subject: Re: Group by more efficient than distinct?
Date: 2008-04-20 15:12:10
Message-ID: cone.1208704330.298437.85914.1000@zoraida.natserv.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

Gregory Stark writes:

> HashAggregate needs to store more values in memory at the same time so it's
> not a good plan if you have a lot of distinct values.

So far the resulting number of rows where in the thousands and the source
data were in there hundreds of thousands and the group by was faster.

When you say "a lot of distinct values" you mean unique values as part of
the result data set?

In other words the HashAggregate will store in memory the resulting rows or
will be used for processing the source rows?

> But the planner knows that and so as long as your work_mem is set to a
> reasonable size (keeping in mind each sort or other operation feels free to

If I make sure to have vacuum analyze on a table will it be reasonable to
trust the explain to see whether distinct or group by is better? I started
a new job and still don't have a good feeling for the sizes or distributions
of the data. Soon I will be given access to the test DB so I will be able to
do counts and explore the data without affecting production.

In response to

Browse pgsql-performance by date

  From Date Subject
Next Message Francisco Reyes 2008-04-20 15:15:36 Re: Group by more efficient than distinct?
Previous Message James Mansion 2008-04-20 14:41:13 Re: Background writer underemphasized ...