Re: weird behaviour on DISTINCT ON

Поиск
Список
Период
Сортировка
От Greg Stark
Тема Re: weird behaviour on DISTINCT ON
Дата
Msg-id 87lla9qwku.fsf@stark.xeocode.com
обсуждение исходный текст
Ответ на weird behaviour on DISTINCT ON  (Gaetano Mendola <mendola@bigfoot.com>)
Ответы Re: weird behaviour on DISTINCT ON  (Gaetano Mendola <mendola@bigfoot.com>)
Список pgsql-hackers
Gaetano Mendola <mendola@bigfoot.com> writes:

> now what do I see is that for each different x value
> the foo is executed more than once, I guess this is because
> the distinct filter out the rows after executing the query.
> 
> Is this behaviour the normal one? Shall be not documented ?

Usually DISTINCT ON is only really useful if you're sorting on something.
Otherwise the choice of which record is output is completely arbitrary.

So the above would typically be something like:

SELECT DISTINCT ON (x), y, foo(x)...ORDER BY x, y

Now you can see why every record does need to be looked at to handle that.
In fact the ORDER BY kicks in before output columns are generated so you can
do things like:

SELECT DISTINCT ON (x), y, foo(x)...ORDER BY x, y, foo(x)

And of course obviously foo() has to be executed for every record to do this.

Postgres doesn't try to detect cases where it's safe to change the regular
order in which things are done and delay execution of functions whose results
aren't needed right away. 

You could just use

SELECT x, foo(x) from (SELECT x ... GROUP BY x)

-- 
greg



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

Предыдущее
От: "Marc G. Fournier"
Дата:
Сообщение: 7.2.7 -> 8.0.1 Bundles Ready ...
Следующее
От: Gaetano Mendola
Дата:
Сообщение: Re: weird behaviour on DISTINCT ON