Re: query optimization: aggregate and distinct

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Jeff Davis <jdavis-pgsql(at)empires(dot)org>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: query optimization: aggregate and distinct
Date: 2003-08-21 13:36:39
Message-ID: 4568.1061472999@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Jeff Davis <jdavis-pgsql(at)empires(dot)org> writes:
> I had an idea about using aggregates: what if I made an aggregate function
> called "first" that just returned the value in the first tuple it
> encountered?

You could make that work in 7.4, but not in any existing releases.

The trouble is that you need something like

SELECT first(foo) FROM (SELECT ... ORDER BY col1,col2) ss
GROUP BY col1

and before 7.4 the optimizer doesn't realize that it can skip re-sorting
at the outer level. So unless the sort is stable (which it won't be, on
most platforms anyway) the needed ordering by col2 within each group is
destroyed.

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Shridhar Daithankar 2003-08-21 14:18:28 Re: [pgsql-advocacy] Need concrete "Why Postgres not MySQL"
Previous Message Greg Stark 2003-08-21 13:21:01 Re: [pgsql-advocacy] Need concrete "Why Postgres not MySQL"