Skip site navigation (1) Skip section navigation (2)

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: (view raw, whole thread or download thread mbox)
Lists: pgsql-performancepgsql-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:


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


In response to


pgsql-performance by date

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

pgsql-sql by date

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

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group