Re: [SQL] OFFSET impact on Performance???

From: Greg Stark <gsstark(at)mit(dot)edu>
To: "Andrei Bintintan" <klodoma(at)ar-sd(dot)net>
Cc: "Richard Huxton" <dev(at)archonet(dot)com>, <pgsql-sql(at)postgresql(dot)org>, <pgsql-performance(at)postgresql(dot)org>
Subject: Re: [SQL] OFFSET impact on Performance???
Date: 2005-01-20 16:59:34
Message-ID: 87pt00c9zd.fsf@stark.xeocode.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance pgsql-sql

"Andrei Bintintan" <klodoma(at)ar-sd(dot)net> writes:

> > If you're using this to provide "pages" of results, could you use a cursor?
> What do you mean by that? Cursor?
>
> Yes I'm using this to provide "pages", but If I jump to the last pages it goes
> very slow.

The best way to do pages for is not to use offset or cursors but to use an
index. This only works if you can enumerate all the sort orders the
application might be using and can have an index on each of them.

To do this the query would look something like:

SELECT * FROM tab WHERE col > ? ORDER BY col LIMIT 50

Then you take note of the last value used on a given page and if the user
selects "next" you pass that as the starting point for the next page.

This query takes the same amount of time no matter how many records are in the
table and no matter what page of the result set the user is on. It should
actually be instantaneous even if the user is on the hundredth page of
millions of records because it uses an index both for the finding the right
point to start and for the ordering.

It also has the advantage that it works even if the list of items changes as
the user navigates. If you use OFFSET and someone inserts a record in the
table then the "next" page will overlap the current page. Worse, if someone
deletes a record then "next" will skip a record.

The disadvantages of this are a) it's hard (but not impossible) to go
backwards. And b) it's impossible to give the user a list of pages and let
them skip around willy nilly.

(If this is for a web page then specifically don't recommend cursors. It will
mean you'll have to have some complex session management system that
guarantees the user will always come to the same postgres session and has some
garbage collection if the user disappears. And it means the URL is only good
for a limited amount of time. If they bookmark it it'll break if they come
back the next day.)

--
greg

In response to

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Merlin Moncure 2005-01-20 17:00:06 Re: [SQL] OFFSET impact on Performance???
Previous Message Holger Hoffstaette 2005-01-20 16:55:56 Re: PostgreSQL clustering VS MySQL clustering

Browse pgsql-sql by date

  From Date Subject
Next Message Richard Huxton 2005-01-20 17:01:00 Re: automatic table locking on too many locked records?
Previous Message Richard Huxton 2005-01-20 16:53:14 Re: [SQL] OFFSET impact on Performance???