Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 63C271337B67 for ; Sun, 13 Mar 2011 19:10:49 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 58822-08 for ; Sun, 13 Mar 2011 22:10:42 +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 CFA02133740B for ; Sun, 13 Mar 2011 19:10:41 -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 p2DMATjR005239; Sun, 13 Mar 2011 18:10:30 -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: <26708.1300033481@sss.pgh.pa.us> References: <26708.1300033481@sss.pgh.pa.us> Comments: In-reply-to Tom Lane message dated "Sun, 13 Mar 2011 12:24:41 -0400" Date: Sun, 13 Mar 2011 18:10:29 -0400 Message-ID: <5238.1300054229@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/131 X-Sequence-Number: 42810 I wrote: > "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? No, scratch that, I misread the plans. It *is* picking the plan it thinks has lower cost; it's just a mistaken cost estimate. It's strange though that the less selective indexscan is getting a lower cost estimate. I wonder whether your table is (almost) perfectly ordered by receiveddatetime, such that the one-column index has correlation close to 1.0. That could possibly lower the cost estimate to the point where it'd appear to dominate the other index. It'd be useful to see the pg_stats.correlation value for both the userid and receiveddatetime columns. regards, tom lane