From: Richard Huxton <dev@archonet.com>
To: Greg Stark <gsstark@mit.edu>
Cc: Andrei Bintintan <klodoma@ar-sd.net>
Cc: pgsql-sql@postgresql.org
Cc: pgsql-performance@postgresql.org
Subject: Re: [SQL] OFFSET impact on Performance???
Date: Thu, 20 Jan 2005 17:24:36 +0000
Message-ID: <41EFE954.7080205@archonet.com> (raw)
In-Reply-To: <87pt00c9zd.fsf@stark.xeocode.com>
References: <019f01c4fee1$1b58d600$0b00a8c0@forge>
<41EF9FD3.5020400@archonet.com>
<020d01c4fef6$595bb9d0$0b00a8c0@forge>
<87pt00c9zd.fsf@stark.xeocode.com>
Greg Stark wrote:
> "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.
Greg's is the most efficient, but you need to make sure you have a
suitable key available in the output of your select.
Also, since you are repeating the query you could get different results
as people insert/delete rows. This might or might not be what you want.
A similar solution is to partition by date/alphabet or similar, then
page those results. That can reduce your resultset to a manageable size.
--
Richard Huxton
Archonet Ltd
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: dev@archonet.com, gsstark@mit.edu, klodoma@ar-sd.net, pgsql-performance@postgresql.org
Subject: Re: [SQL] OFFSET impact on Performance???
In-Reply-To: <41EFE954.7080205@archonet.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