Re: MAX/MIN optimization via rewrite (plus query rewrites - Mailing list pgsql-hackers

From Greg Stark
Subject Re: MAX/MIN optimization via rewrite (plus query rewrites
Date
Msg-id 87fz3gg64e.fsf@stark.xeocode.com
Whole thread Raw
In response to Re: MAX/MIN optimization via rewrite (plus query rewrites  (Bruno Wolff III <bruno@wolff.to>)
List pgsql-hackers
Bruno Wolff III <bruno@wolff.to> writes:

> I don't think you should be rewriting queries as much as providing
> alternate plans and letting the rest of the optimizer decided which
> plan to use. If you just rewrite a query you might lock yourself into
> using a poor plan.

Moreover, none of these rewritten queries would work properly for

select min(foo) from tab group by bar

This should still be aware it can use an index on <bar,foo> and get much
better performance. Well it can't know, but it should be able to. All it
should really need to know is that min() only needs a particular subset of the
dataset -- namely the first record, as long as the records are provided in a
particular order.

Also, the same code ought to be able to handle

select first(foo) from tab group by bar

Which is exactly equivalent to the min() case except that no particular
ordering is required.

-- 
greg



pgsql-hackers by date:

Previous
From: Mark Kirkwood
Date:
Subject: Re: MAX/MIN optimization via rewrite (plus query rewrites
Next
From: Patrick B Kelly
Date:
Subject: Re: multiline CSV fields