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

From Greg Stark
Subject Re: Inefficient SELECT with OFFSET and LIMIT
Date
Msg-id 878ykl0wvy.fsf@stark.dyndns.tv
Whole thread Raw
In response to Inefficient SELECT with OFFSET and LIMIT  (Clive Page <cgp@leicester.ac.uk>)
Responses Re: Inefficient SELECT with OFFSET and LIMIT
List pgsql-performance
Clive Page <cgp@leicester.ac.uk> writes:

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

> 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.

You could do something like:

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

I think it's not easy for the optimizer to do it because there are lots of
cases where it can't. Consider if you had an ORDER BY clause on the myfunc
output column for example. Or if myfunc was a set-returning function.

--
greg

pgsql-performance by date:

Previous
From: Shridhar Daithankar
Date:
Subject: Re: Select max(foo) and select count(*) optimization
Next
From: Tom Lane
Date:
Subject: Re: Inefficient SELECT with OFFSET and LIMIT