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

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

Greg Stark wrote:
> "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.

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

In response to

Browse pgsql-performance by date

  From Date Subject
Next Message Darcy Buskermolen 2005-01-20 17:29:37 Re: PostgreSQL clustering VS MySQL clustering
Previous Message Alex Turner 2005-01-20 17:23:12 Re: PostgreSQL clustering VS MySQL clustering

Browse pgsql-sql by date

  From Date Subject
Next Message Richard Huxton 2005-01-20 17:44:09 Re: ERROR: row is too big: size 9856, maximum size 8136
Previous Message Joel Fradkin 2005-01-20 17:20:26 Re: ERROR: row is too big: size 9856, maximum size 8136