Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id F0CB5B5DBDB for ; Thu, 29 Sep 2011 21:39:40 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 97268-01 for ; Fri, 30 Sep 2011 00:39:34 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.8.0-rc2 Received: from smtp-out-2.01.com (smtp.01.com [216.115.88.169]) by mail.postgresql.org (Postfix) with ESMTP id C6FA8B5DBCD for ; Thu, 29 Sep 2011 21:39:34 -0300 (ADT) Received: from localhost (localhost.localdomain [127.0.0.1]) by smtp-out-2.01.com (Postfix) with ESMTP id 538784141D0 for ; Thu, 29 Sep 2011 19:39:34 -0500 (CDT) X-Virus-Scanned: amavisd-new at smtp-out-2.01.com Received: from smtp-out-2.01.com ([127.0.0.1]) by localhost (smtp-out-2.01.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id hQba-Z7cQaoS for ; Thu, 29 Sep 2011 19:39:34 -0500 (CDT) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp-out-2.01.com (Postfix) with ESMTP id 34BFB41421D for ; Thu, 29 Sep 2011 19:39:34 -0500 (CDT) Received: from Sidney-Stratton.local (70-36-143-92.dsl.dynamic.sonic.net [70.36.143.92]) by smtp-out-2.01.com (Postfix) with ESMTPSA id 096FB4141D0 for ; Thu, 29 Sep 2011 19:39:33 -0500 (CDT) Message-ID: <4E850FC0.509@agliodbs.com> Date: Thu, 29 Sep 2011 17:39:28 -0700 From: Josh Berkus Organization: PostgreSQL Experts Inc. User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.2.15) Gecko/20110303 Lightning/1.0b2 Thunderbird/3.1.9 MIME-Version: 1.0 To: postgres performance list Subject: Shortcutting too-large offsets? X-Enigmail-Version: 1.1.1 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-10 required=5 tests=BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201109/424 X-Sequence-Number: 45200 All, Here's a case which it seems like we ought to be able to optimize for: datamart-# ORDER BY txn_timestamp DESC datamart-# LIMIT 200 datamart-# OFFSET 6000; QUERY PLAN --------------------------- Limit (cost=560529.82..560529.82 rows=1 width=145) (actual time=22419.760..22419.760 rows=0 loops=1) -> Sort (cost=560516.17..560529.82 rows=5459 width=145) (actual time=22418.076..22419.144 rows=5828 loops=1) Sort Key: lh.txn_timestamp Sort Method: quicksort Memory: 1744kB -> Nested Loop Left Join (cost=0.00..560177.32 rows=5459 width=145) (actual time=4216.898..22398.658 rows=5828 loops=1) -> Nested Loop Left Join (cost=0.00..88186.22 rows=5459 width=135) (actual time=4216.747..19250.891 rows=5828 loops=1) -> Nested Loop Left Join (cost=0.00..86657.26 rows=5459 width=124) (actual time=4216.723..19206.461 rows=5828 loops=1) ... it seems like, if we get as far as the sort and the executors knows that there are less rows than the final offset, it ought to be able to skip the final sort. Is there some non-obvious reason which would make this kind of optimization difficult? Doesn't the executor know at that point how many rows it has? -- Josh Berkus PostgreSQL Experts Inc. http://pgexperts.com