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 E4F7A3A4894; Thu, 20 Jan 2005 17:24:48 +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 48559-08; Thu, 20 Jan 2005 17:24:38 +0000 (GMT) Received: from anchor-post-34.mail.demon.net (anchor-post-34.mail.demon.net [194.217.242.92]) by svr1.postgresql.org (Postfix) with ESMTP id C76D13A1CF7; Thu, 20 Jan 2005 17:24:38 +0000 (GMT) Received: from mwynhau.demon.co.uk ([193.237.186.96] helo=mainbox.archonet.com) by anchor-post-34.mail.demon.net with esmtp (Exim 4.42) id 1Crg2u-000I3o-Fh; Thu, 20 Jan 2005 17:24:37 +0000 Received: from [192.168.1.17] (client17.archonet.com [192.168.1.17]) by mainbox.archonet.com (Postfix) with ESMTP id BEAE617089; Thu, 20 Jan 2005 17:24:35 +0000 (GMT) Message-ID: <41EFE954.7080205@archonet.com> Date: Thu, 20 Jan 2005 17:24:36 +0000 From: Richard Huxton User-Agent: Mozilla Thunderbird 1.0 (X11/20041206) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Greg Stark Cc: Andrei Bintintan , pgsql-sql@postgresql.org, pgsql-performance@postgresql.org Subject: Re: [SQL] OFFSET impact on Performance??? References: <019f01c4fee1$1b58d600$0b00a8c0@forge> <41EF9FD3.5020400@archonet.com> <020d01c4fef6$595bb9d0$0b00a8c0@forge> <87pt00c9zd.fsf@stark.xeocode.com> In-Reply-To: <87pt00c9zd.fsf@stark.xeocode.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.074 tagged_above=0 required=5 tests=AWL, FORGED_RCVD_HELO X-Spam-Level: X-Archive-Number: 200501/325 X-Sequence-Number: 10015 Greg Stark wrote: > "Andrei Bintintan" writes: > > >>>If you're using this to provide "pages" of results, could you use a cursor? >> >>What do you mean by that? Cursor? >> >>Yes I'm using this to provide "pages", but If I jump to the last pages it goes >>very slow. > > > 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. Greg's is the most efficient, but you need to make sure you have a suitable key available in the output of your select. Also, since you are repeating the query you could get different results as people insert/delete rows. This might or might not be what you want. A similar solution is to partition by date/alphabet or similar, then page those results. That can reduce your resultset to a manageable size. -- Richard Huxton Archonet Ltd