Re: Inefficient SELECT with OFFSET and LIMIT

From: Greg Stark <gsstark(at)mit(dot)edu>
To: Clive Page <cgp(at)leicester(dot)ac(dot)uk>
Cc: pgsql-performance(at)postgresql(dot)org
Subject: Re: Inefficient SELECT with OFFSET and LIMIT
Date: 2004-01-06 18:12:17
Message-ID: 878ykl0wvy.fsf@stark.dyndns.tv
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance


Clive Page <cgp(at)leicester(dot)ac(dot)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

In response to

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Tom Lane 2004-01-06 18:36:44 Re: Inefficient SELECT with OFFSET and LIMIT
Previous Message Shridhar Daithankar 2004-01-06 16:31:20 Re: Select max(foo) and select count(*) optimization