Re: Inefficient SELECT with OFFSET and LIMIT - Mailing list pgsql-performance

From Tom Lane
Subject Re: Inefficient SELECT with OFFSET and LIMIT
Date
Msg-id 22305.1073414204@sss.pgh.pa.us
Whole thread Raw
In response to Re: Inefficient SELECT with OFFSET and LIMIT  (Greg Stark <gsstark@mit.edu>)
List pgsql-performance
Greg Stark <gsstark@mit.edu> writes:
> Clive Page <cgp@leicester.ac.uk> writes:
>> It would be nice if OFFSET could be implemented in some more efficient
>> way.

> You could do something like:

> select myfunc(mycol) from (select mycol from table limit 50 offset 10000) as x;

Note that this won't eliminate the major inefficiency, which is having
to read 10000+50 rows from the table.  But if myfunc() has side-effects
or is very expensive to run, it'd probably be worth doing.

> I think it's not easy for the optimizer to do it because there are lots of
> cases where it can't.

I don't actually know of any cases where it could do much of anything to
avoid fetching the OFFSET rows.  The problems are basically the same as
with COUNT(*) optimization: without examining each row, you don't know
if it would have been returned or not.  We could possibly postpone
evaluation of the SELECT output list until after the OFFSET step (thus
automating the above hack), but even that only works if there are no
set-returning functions in the output list ...

            regards, tom lane

PS: BTW, the one-extra-row effect that Clive noted is gone in 7.4.

pgsql-performance by date:

Previous
From: Greg Stark
Date:
Subject: Re: Inefficient SELECT with OFFSET and LIMIT
Next
From: Mark Kirkwood
Date:
Subject: Re: Select max(foo) and select count(*) optimization