Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 481321337BBC for ; Sun, 13 Mar 2011 13:24:59 -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 19303-06-2 for ; Sun, 13 Mar 2011 16:24:48 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by mail.postgresql.org (Postfix) with ESMTP id AF4E11337BD4 for ; Sun, 13 Mar 2011 13:24:47 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id p2DGOfk1026709; Sun, 13 Mar 2011 12:24:42 -0400 (EDT) To: "John Surcombe" cc: pgsql-performance@postgresql.org Subject: Re: Planner wrongly shuns multi-column index for select .. order by col1, col2 limit 1 In-reply-to: References: Comments: In-reply-to "John Surcombe" message dated "Sat, 12 Mar 2011 10:07:41 +0000" Date: Sun, 13 Mar 2011 12:24:41 -0400 Message-ID: <26708.1300033481@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-10 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201103/129 X-Sequence-Number: 42808 "John Surcombe" writes: > When we 'EXPLAIN' this query, PostgreSQL says it is using the index > idx_receiveddatetime. The way the application is designed means that in > virtually all cases the query will have to scan a very long way into > idx_receiveddatetime to find the first record where userid = 311369000. > If however we delete the idx_receiveddatetime index, the query uses the > idx_userid_receiveddatetime index, and the query only takes a few > milliseconds. That's just bizarre ... it knows the index is applicable, and the cost estimates clearly favor the better index, so why did it pick the worse one? I tried to duplicate this locally, without success, so there's some contributing factor you've neglected to mention. Can you put together a self-contained test case that acts like this? regards, tom lane