pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Greg Stark <gsstark@mit.edu>
To: Andrei Bintintan <klodoma@ar-sd.net>
Cc: Richard Huxton <dev@archonet.com>
Cc: pgsql-sql@postgresql.org
Cc: pgsql-performance@postgresql.org
Subject: Re: [SQL] OFFSET impact on Performance???
Date: 20 Jan 2005 11:59:34 -0500
Message-ID: <87pt00c9zd.fsf@stark.xeocode.com> (raw)
In-Reply-To: <020d01c4fef6$595bb9d0$0b00a8c0@forge>
References: <019f01c4fee1$1b58d600$0b00a8c0@forge>
	<41EF9FD3.5020400@archonet.com>
	<020d01c4fef6$595bb9d0$0b00a8c0@forge>

"Andrei Bintintan" <klodoma@ar-sd.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




view thread (22+ messages)  latest in thread

Message-ID: <87pt00c9zd.fsf@stark.xeocode.com>
Permalink:  ../87pt00c9zd.fsf@stark.xeocode.com/
Also on:    postgresql.org/message-id/87pt00c9zd.fsf@stark.xeocode.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: gsstark@mit.edu, klodoma@ar-sd.net, dev@archonet.com, pgsql-performance@postgresql.org
  Subject: Re: [SQL] OFFSET impact on Performance???
  In-Reply-To: <87pt00c9zd.fsf@stark.xeocode.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox