X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 19FFD3A48DB; Thu, 20 Jan 2005 19:11:03 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 61464-08; Thu, 20 Jan 2005 19:10:52 +0000 (GMT) Received: from quasar.skima.is (quasar.skima.is [212.30.200.205]) by svr1.postgresql.org (Postfix) with ESMTP id 99DC03A484F; Thu, 20 Jan 2005 19:10:50 +0000 (GMT) Received: from gnari ([157.157.193.37] [157.157.193.37]) by quasar.skima.is with ESMTP; Thu, 20 Jan 2005 19:10:49 Z Subject: Re: [SQL] OFFSET impact on Performance??? From: Ragnar =?ISO-8859-1?Q?Hafsta=F0?= To: pgsql-performance@postgresql.org Cc: Andrei Bintintan , pgsql-sql@postgresql.org In-Reply-To: <87pt00c9zd.fsf@stark.xeocode.com> References: <019f01c4fee1$1b58d600$0b00a8c0@forge> <41EF9FD3.5020400@archonet.com> <020d01c4fef6$595bb9d0$0b00a8c0@forge> <87pt00c9zd.fsf@stark.xeocode.com> Content-Type: text/plain Date: Thu, 20 Jan 2005 19:12:06 +0000 Message-Id: <1106248326.22416.22.camel@localhost.localdomain> Mime-Version: 1.0 X-Mailer: Evolution 2.0.2 Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests= X-Spam-Level: X-Archive-Number: 200501/332 X-Sequence-Number: 10022 On Thu, 2005-01-20 at 11:59 -0500, Greg Stark wrote: > 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. this will only work unchanged if the index is unique. imagine , for example if you have more than 50 rows with the same value of col. one way to fix this is to use ORDER BY col,oid gnari