Re: MAX/MIN optimization via rewrite (plus query rewrites

Поиск
Список
Период
Сортировка
От Greg Stark
Тема Re: MAX/MIN optimization via rewrite (plus query rewrites
Дата
Msg-id 871xeva962.fsf@stark.xeocode.com
обсуждение исходный текст
Ответ на Re: MAX/MIN optimization via rewrite (plus query rewrites  (Jan Wieck <JanWieck@Yahoo.com>)
Ответы Re: MAX/MIN optimization via rewrite (plus query rewrites  (Dawid Kuroczko <qnex42@gmail.com>)
Список pgsql-hackers
Jan Wieck <JanWieck@Yahoo.com> writes:

> Both cases can be expressed with order by + limit queries, that would indeed
> utilize those indexes. But what's been discussed so far does not cover any of
> them.

I think people should get away from thinking about "order by + limit". That
isn't going to work for anything with a GROUP BY. And it isn't going to work
for anything more complex than a single min() or max().

min() only needs the first record from whatever set of records it's operating
on as long as they're provided in a specified order. This is just as true for
a min() applied to only a single GROUP as it is for a min() applied to an
entire table.

I don't think you want to use the existing Limit executor node. That will only
ever let you handle these simple aggregates that return the first value they
see. What you want is a normal Aggregate node, but the node feeding it should
be an altered index scan that knows it only needs to pull out the first and/or
last record for each GROUP.

That will let you handle min() and max() in the same query for example. It
might also leave open the door for other more complex data subsets. Say a
geometric data type where it needs all the bounding points of an area.

-- 
greg



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

Предыдущее
От: joseph speigle
Дата:
Сообщение: Re: psql \e broken again
Следующее
От: Christopher Kings-Lynne
Дата:
Сообщение: Odd plpgsql behaviour