From: Tom Lane <tgl@sss.pgh.pa.us>
To: Michael Lorenz <mlorenz1@hotmail.com>
Cc: pgsql-performance@postgresql.org
Subject: Re: Query slows after offset of 100K
Date: Thu, 14 Feb 2008 14:08:15 -0500
Message-ID: <17159.1203016095@sss.pgh.pa.us> (raw)
In-Reply-To: <BAY111-W56DE7AD29405FE5C2BF1A93250@phx.gbl>
References: <BAY111-W56DE7AD29405FE5C2BF1A93250@phx.gbl>
Michael Lorenz <mlorenz1@hotmail.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
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 from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-performance@postgresql.org
Cc: tgl@sss.pgh.pa.us, mlorenz1@hotmail.com
Subject: Re: Query slows after offset of 100K
In-Reply-To: <17159.1203016095@sss.pgh.pa.us>
References: <BAY111-W56DE7AD29405FE5C2BF1A93250@phx.gbl> <17159.1203016095@sss.pgh.pa.us>
* 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