Re: ORDER BY and DISTINCT ON

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Bruno Wolff III <bruno(at)wolff(dot)to>
Cc: Greg Stark <gsstark(at)mit(dot)edu>, pgsql-hackers(at)postgresql(dot)org, Neil Conway <neilc(at)samurai(dot)com>
Subject: Re: ORDER BY and DISTINCT ON
Date: 2003-12-15 14:53:23
Message-ID: 12581.1071500003@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Bruno Wolff III <bruno(at)wolff(dot)to> writes:
> Specifically the interpretation I think makes sense is that
> SELECT DISTINCT ON (a, b, c) * FROM tablename ORDER BY d, e, f
> should be treated as equvialent to
> SELECT * FROM
> (SELECT DISTINCT ON (a, b, c) FROM tablename ORDER BY a, b, c, d, e, f) AS t
> ORDER BY d, e, f

Right, that's the same thing Neil said elsewhere in the thread. I guess
I agree that this would be cleaner, and also that it doesn't seem like a
high priority problem.

BTW, you could imagine implementing DISTINCT and DISTINCT ON by a hash
table, if you didn't expect too many distinct rows. The hash key would
be the DISTINCT columns, and in the DISTINCT ON case, when you get a new
row with DISTINCT columns matching an existing entry, you compare the
two rows using the ORDER BY key columns to decide which row to keep. In
this formulation it's fairly obvious that the DISTINCT and ORDER BY keys
don't need to overlap at all. This technique does not require presorted
input (good) but also delivers unsorted output (bad). It could still be
a big win if you expect the DISTINCT filtering to reduce the number of
rows substantially, because you'd end up sorting a much smaller number
of rows. I didn't get around to trying to implement this when I was
doing hash aggregation for 7.4, but it seems like a reasonable item for
the TODO list.

regards, tom lane

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Claudia D'amato 2003-12-15 16:59:28 postgres does not respond to a query view of 10000 records roughl y
Previous Message Bruno Wolff III 2003-12-15 14:03:34 Re: ORDER BY and DISTINCT ON