Received: from localhost (unknown [200.46.204.182]) by postgresql.org (Postfix) with ESMTP id 436802E33D5 for ; Sun, 13 Jan 2008 23:52:32 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.182]) (amavisd-maia, port 10024) with ESMTP id 45007-06 for ; Sun, 13 Jan 2008 23:52:22 -0400 (AST) Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by postgresql.org (Postfix) with ESMTP id 730CA2E33CE for ; Sun, 13 Jan 2008 23:52:28 -0400 (AST) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.1/8.14.1) with ESMTP id m0E3qPaT018895; Sun, 13 Jan 2008 22:52:25 -0500 (EST) To: "Scott Marlowe" cc: "pepone. onrez" , pgsql-general@postgresql.org Subject: Re: query performance In-reply-to: References: <198501d60801131858n529b46dcmeb286cda32d56e93@mail.gmail.com> Comments: In-reply-to "Scott Marlowe" message dated "Sun, 13 Jan 2008 21:39:59 -0600" Date: Sun, 13 Jan 2008 22:52:25 -0500 Message-ID: <18894.1200282745@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200801/623 X-Sequence-Number: 126831 "Scott Marlowe" writes: > On Jan 13, 2008 8:58 PM, pepone. onrez 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