Received: from localhost (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id C73D564FF05 for ; Mon, 24 Nov 2008 18:58:11 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 52806-01 for ; Mon, 24 Nov 2008 18:58:08 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from 29.mail-out.ovh.net (29.mail-out.ovh.net [87.98.216.213]) by mail.postgresql.org (Postfix) with SMTP id 76BAB64FEFD for ; Mon, 24 Nov 2008 18:58:08 -0400 (AST) Received: (qmail 17159 invoked by uid 503); 24 Nov 2008 22:58:07 -0000 Received: from unknown (HELO mail179.ha.ovh.net) (213.186.33.59) by 29.mail-out.ovh.net with SMTP; 24 Nov 2008 22:58:07 -0000 Received: from b0.ovh.net (HELO queue-out) (213.186.33.50) by b0.ovh.net with SMTP; 24 Nov 2008 22:57:36 -0000 Received: from par69-8-88-161-102-87.fbx.proxad.net (HELO soyouz) (88.161.102.87) by ns0.ovh.net with SMTP; 24 Nov 2008 22:57:35 -0000 Date: Mon, 24 Nov 2008 23:58:50 +0100 To: Andrus , "Scott Marlowe" Subject: Re: limit clause produces wrong query plan From: PFC Cc: pgsql-performance@postgresql.org Content-Type: text/plain; format=flowed; delsp=yes; charset=utf-8 MIME-Version: 1.0 References: <0e6967e0-fb33-49aa-a745-a1d11ef6a9e0@q9g2000yqc.googlegroups.com> <372C245439584A6CBF49F996503BF162@andrusnotebook> Content-Transfer-Encoding: 7bit Message-ID: In-Reply-To: <372C245439584A6CBF49F996503BF162@andrusnotebook> User-Agent: Opera Mail/9.62 (Linux) X-Ovh-Tracer-Id: 3557562231379003014 X-Ovh-Remote: 88.161.102.87 (par69-8-88-161-102-87.fbx.proxad.net) X-Ovh-Local: 213.186.33.20 (ns0.ovh.net) X-Spam-Check: DONE|H 0.5/N X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests=none X-Spam-Level: X-Archive-Number: 200811/248 X-Sequence-Number: 31979 > SELECT ... FROM bigtable ORDER BY intprimarykey OFFSET 100 LIMIT 100 I think pagination is overrated. If the query produces, for instance, something like 100 rows or less, more often than not, getting all the rows will take the exact same time as getting a portion of the rows... in all those cases, it is much better to cache the results somewhere (user session, table, whatever) and paginate based on that, rather than perform the same query lots of times. Especially when some non-indexed sorting takes place in which case you are gonna fetch all the rows anyway. Something like row-id can be stored instead of the full rows, also. There are exceptions of course. And if the query produces 20.000 results... who is ever going to scroll to page 1257 ? > The example which I posted shows that > > SELECT ... FROM bigtable ORDER BY intprimarykey LIMIT 100 > > this is extremely *slow*: seq scan is performed over whole bigtable. This is wrong though. It should use an index, especially if you have a LIMIT...