Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id D109EB5E05F for ; Wed, 8 Jun 2011 05:33:27 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 73752-02 for ; Wed, 8 Jun 2011 08:33:16 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from elizabeth.gransy.com (elizabeth.gransy.com [89.187.132.199]) by mail.postgresql.org (Postfix) with ESMTP id 81B14B5DEE9 for ; Wed, 8 Jun 2011 05:33:16 -0300 (ADT) Received: from sq.gransy.com (localhost [127.0.0.1]) by elizabeth.gransy.com (Postfix) with ESMTP id 48F7615E40BD; Wed, 8 Jun 2011 10:33:15 +0200 (CEST) Received: from 85.160.195.232 (SquirrelMail authenticated user tv@fuzzy.cz) by sq.gransy.com with HTTP; Wed, 8 Jun 2011 10:33:15 +0200 Message-ID: <22bab989ef6b6b073db4cecca0a19dd7.squirrel@sq.gransy.com> In-Reply-To: <201106081508.05668.anthony.shipman@symstream.com> References: <201106071802.08272.anthony.shipman@symstream.com> <30c6caeda38eb66a15665d147a5db451.squirrel@sq.gransy.com> <201106081508.05668.anthony.shipman@symstream.com> Date: Wed, 8 Jun 2011 10:33:15 +0200 Subject: Re: strange query plan with LIMIT From: tv@fuzzy.cz To: anthony.shipman@symstream.com Cc: pgsql-performance@postgresql.org, tv@fuzzy.cz User-Agent: SquirrelMail/1.4.21 MIME-Version: 1.0 Content-Type: text/plain;charset=utf-8 Content-Transfer-Encoding: 8bit X-Priority: 3 (Normal) Importance: Normal 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: 201106/55 X-Sequence-Number: 43988 > What seems odd to me is that the only difference between the two is the > limit > clause: > > select * from tdiag where (create_time >= '2011-06-03 > 09:49:04.000000+0' and create_time < '2011-06-06 09:59:04.000000+0') order > by > diag_id limit 1; > > select * from tdiag where (create_time >= '2011-06-03 > 09:49:04.000000+0' and create_time < '2011-06-06 09:59:04.000000+0') order > by > diag_id; > > and yet the plan completely changes. As Claudio Freire already pointed out, this is expected behavior. With LIMIT the planner prefers plans with low starting cost, as it expects to end soon and building index bitmap / hash table would be a waste. So actually it would be very odd if the plan did not change in this case ... Anyway I have no idea how to fix this "clean" - without messing with enable_* or cost variables or other such dirty tricks. regards Tomas