Re: DISTINCT vs. GROUP BY

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Dimi Paun <dimi(at)lattica(dot)com>
Cc: pgsql-performance <pgsql-performance(at)postgresql(dot)org>
Subject: Re: DISTINCT vs. GROUP BY
Date: 2010-02-09 22:38:21
Message-ID: 25105.1265755101@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

Dimi Paun <dimi(at)lattica(dot)com> writes:
>> From what I've read on the net, these should be very similar,
> and should generate equivalent plans, in such cases:

> SELECT DISTINCT x FROM mytable
> SELECT x FROM mytable GROUP BY x

> However, in my case (postgresql-server-8.1.18-2.el5_4.1),
> they generated different results with quite different
> execution times (73ms vs 40ms for DISTINCT and GROUP BY
> respectively):

The results certainly ought to be the same (although perhaps not with
the same ordering) --- if they aren't, please provide a reproducible
test case.

As for efficiency, though, 8.1 didn't understand how to use hash
aggregation for DISTINCT. Less-obsolete versions do know how to do
that.

regards, tom lane

In response to

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Dimi Paun 2010-02-10 01:43:47 Re: DISTINCT vs. GROUP BY
Previous Message Thom Brown 2010-02-09 22:22:17 Re: DISTINCT vs. GROUP BY