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

Re: Query slows after offset of 100K

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Michael Lorenz <mlorenz1(at)hotmail(dot)com>
Cc: pgsql-performance(at)postgresql(dot)org
Subject: Re: Query slows after offset of 100K
Date: 2008-02-14 19:08:15
Message-ID: 17159.1203016095@sss.pgh.pa.us (view raw or flat)
Thread:
Lists: pgsql-performance
Michael Lorenz <mlorenz1(at)hotmail(dot)com> writes:
> My query is as follows:
> SELECT o.objectid, o.objectname, o.isactive, o.modificationtime 
> FROM    object o 
> WHERE  ( o.deleted = false OR o.deleted IS NULL ) 
> AND      o.accountid = 111 
> ORDER BY 2 
> LIMIT 20 OFFSET 10000;

This is guaranteed to lose --- huge OFFSET values are never a good idea
(hint: the database still has to fetch those rows it's skipping over).

A saner way to do pagination is to remember the last key you displayed
and do something like "WHERE key > $lastkey ORDER BY key LIMIT 20",
which will allow the database to go directly to the desired rows,
as long as you have an index on the key.  You do need a unique ordering
key for this to work, though.

			regards, tom lane

In response to

Responses

pgsql-performance by date

Next:From: Michael LorenzDate: 2008-02-14 19:49:22
Subject: Re: Query slows after offset of 100K
Previous:From: Michael LorenzDate: 2008-02-14 18:28:13
Subject: Query slows after offset of 100K

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