Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 8D45E1337BE8 for ; Mon, 14 Mar 2011 14:24:51 -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 74125-09 for ; Mon, 14 Mar 2011 17:24:44 +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 004061337BE9 for ; Mon, 14 Mar 2011 14:24:43 -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 p2EHOXjt013032; Mon, 14 Mar 2011 13:24:33 -0400 (EDT) To: "John Surcombe" cc: pgsql-performance@postgresql.org, "Kieran Senior" Subject: Re: Planner wrongly shuns multi-column index for select .. order by col1, col2 limit 1 In-reply-to: References: <26708.1300033481@sss.pgh.pa.us> <5238.1300054229@sss.pgh.pa.us> Comments: In-reply-to "John Surcombe" message dated "Mon, 14 Mar 2011 09:37:40 -0000" Date: Mon, 14 Mar 2011 13:24:33 -0400 Message-ID: <13031.1300123473@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/143 X-Sequence-Number: 42822 "John Surcombe" writes: >> It'd be >> useful to see the pg_stats.correlation value for both the userid and >> receiveddatetime columns. > Yes, the table is indeed nearly perfectly ordered by receiveddatetime > (correlation 0.998479). correlation on userid is -0.065556. n_distinct > on userid is also low: 1097. Ah-hah. > Is the problem perhaps something like the following: PostgreSQL is > thinking that because there are not many userids and there is low > correlation, that if it just scans the table from the top in date order, > this will be cheap (because receiveddatetime correlation is high so it > won't have to seek randomly), and it won't have to scan very far before > it finds the first row with a matching userid. There's some of that, but I think the main problem is that there's a very high discount on the cost estimate for a perfectly-correlated index, and that makes it end up looking cheaper to use than the uncorrelated one. (It doesn't help any that we don't do correlation properly for multicolumn indexes; but given what you say above, the correlation estimate for the two-column index would be small even if we'd computed it exactly.) You might find that reducing random_page_cost would avoid the problem. That should reduce the advantage conferred on the high-correlation index, and it probably would represent your actual configuration better anyway, given the results you're showing here. regards, tom lane