From: Tom Lane <tgl@sss.pgh.pa.us>
To: Greg Stark <gsstark@mit.edu>
Cc: Clive Page <cgp@leicester.ac.uk>
Cc: pgsql-performance@postgresql.org
Subject: Re: Inefficient SELECT with OFFSET and LIMIT
Date: Tue, 06 Jan 2004 13:36:44 -0500
Message-ID: <22305.1073414204@sss.pgh.pa.us> (raw)
In-Reply-To: <878ykl0wvy.fsf@stark.dyndns.tv>
References: <Pine.SGI.4.21.0401061539460.10317357-100000@harrier.le.ac.uk>
<878ykl0wvy.fsf@stark.dyndns.tv>
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.
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-performance@postgresql.org
Cc: tgl@sss.pgh.pa.us, gsstark@mit.edu, cgp@leicester.ac.uk
Subject: Re: Inefficient SELECT with OFFSET and LIMIT
In-Reply-To: <22305.1073414204@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox