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

From Clive Page
Subject Inefficient SELECT with OFFSET and LIMIT
Date
Msg-id Pine.SGI.4.21.0401061539460.10317357-100000@harrier.le.ac.uk
Whole thread Raw
Responses Re: Inefficient SELECT with OFFSET and LIMIT  (Greg Stark <gsstark@mit.edu>)
List pgsql-performance
I have just discovered that if one does a SELECT with a LIMIT and OFFSET
     values, say

SELECT myfunc(mycol) FROM table LIMIT 50 OFFSET 10000 ;

Then the whole of the selection expressions, including the function calls,
are actuall executed for every record, not just those being selected but
also those being skipped, i.e. 10050 in this case.
Actually it's even odder, as the number is that plus one, as the next
record in sequence is also passed to the function.

I discovered this by accident, since I was using a user-defined function
in pl/pgsql and included by mistake some debug code using RAISE INFO, so
this diagnostic output gave the game away (and all of it came out before
any of the results of the selection, which was another surprise).

It looks as if OFFSET is implemented just be throwing away the results,
until the OFFSET has been reached.

It would be nice if OFFSET could be implemented in some more efficient
way.



--
Clive Page,
Dept of Physics & Astronomy,
University of Leicester, U.K.


pgsql-performance by date:

Previous
From: Shridhar Daithankar
Date:
Subject: Re: Select max(foo) and select count(*) optimization
Next
From: Robert Treat
Date:
Subject: Re: Select max(foo) and select count(*) optimization