From: Tom Lane <tgl@sss.pgh.pa.us>
To: Scott Marlowe <scott.marlowe@gmail.com>
Cc: pepone. onrez <pepone.onrez@gmail.com>
Cc: pgsql-general@postgresql.org
Subject: Re: query performance
Date: Sun, 13 Jan 2008 22:52:25 -0500
Message-ID: <18894.1200282745@sss.pgh.pa.us> (raw)
In-Reply-To: <dcc563d10801131939t6df7fe66n150fde7562362a61@mail.gmail.com>
References: <198501d60801131858n529b46dcmeb286cda32d56e93@mail.gmail.com>
<dcc563d10801131939t6df7fe66n150fde7562362a61@mail.gmail.com>
"Scott Marlowe" <scott.marlowe@gmail.com> writes:
> On Jan 13, 2008 8:58 PM, pepone. onrez <pepone.onrez@gmail.com> wrote:
>> t_documentcontent._id AS _id
>> FROM t_documentcontent LIMIT 50 OFFSET 80000
> with no order by, and possibly no index on t_documentcontent._id,
> there's no choice but a seq scan.
More to the point: a query with a huge OFFSET is *always* going to suck,
because there is no choice but to read through all those records before
getting to the ones you want. You need to fundamentally rethink how you
are going about this. I'm assuming that this is actually just one query
in a series that are intended to eventually fetch the whole table.
One solution is to set up a cursor and FETCH 50 rows at a time from it.
However that requires holding a transaction open, which might not work
well in your environment.
Another possibility, if you have a primary key on the table, is to do
something like
SELECT ... FROM ... WHERE id > ? ORDER BY id LIMIT 50
where you leave out the WHERE clause on the first call, and on
subsequent calls '?' is the last id value seen in the prior call.
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-general@postgresql.org
Cc: tgl@sss.pgh.pa.us, scott.marlowe@gmail.com, pepone.onrez@gmail.com
Subject: Re: query performance
In-Reply-To: <18894.1200282745@sss.pgh.pa.us>
References: <198501d60801131858n529b46dcmeb286cda32d56e93@mail.gmail.com> <dcc563d10801131939t6df7fe66n150fde7562362a61@mail.gmail.com> <18894.1200282745@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