Re: OFFSET and LIMIT - performance

From: David Wall <d(dot)wall(at)computer(dot)org>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: OFFSET and LIMIT - performance
Date: 2007-06-28 18:36:09
Message-ID: 4683FF99.1050104@computer.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


> Network transmission costs alone would make the second way a loser.
>
> Large OFFSETs are pretty inefficient because the backend generates and
> discards the rows internally ... but at least it never converts them to
> external form or ships them to the client. Rows beyond the LIMIT are
> not generated at all.
>
Some of this would depend on the query, too, I suspect, since an ORDER
BY would require the entire result set to be determined, sorted and then
the limit/offset could take place. Regardless, it's better than
filtering in the Java/client side to avoid sending it from the database
backend to the client.

But how would that compare to using a cursor/fetch query. It seems like
the JDBC library will automatically use a cursor if you specify some
params on the PreparedStatement, though the details escape me. I think
it's related to setFetchSize() and/or setMaxRows(). Of course, those
are not guaranteed to do anything special either, and you'd still need
to retrieve and discard initial rows unless you can adjust your WHERE
condition to find the "next set".

If you have an ORDER BY on a unique field, for example, you could use
that field to query the next set by remembering the last value in your
previous query set (or select 1 more row than you need so you have the
exact value that would be next) and specifying it in the WHERE clause.
Even this could be an issue if updates would change the grouping.

LIMIT/OFFSET are not part of the SQL standard, too, should that matter
for DB portability. I believe mysql supports it, but it seems like
Oracle didn't (at least at one time).

David

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Bauhaus 2007-06-28 18:42:48 SQL problem..
Previous Message Tom Lane 2007-06-28 18:19:18 Re: OFFSET and LIMIT - performance