Received: from localhost (unknown [200.46.204.184]) by developer.postgresql.org (Postfix) with ESMTP id 9A17A2E0048 for ; Thu, 24 Apr 2008 21:23:40 -0300 (ADT) Received: from developer.postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 44536-04 for ; Thu, 24 Apr 2008 21:23:30 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by developer.postgresql.org (Postfix) with ESMTP id 242C02E005F for ; Thu, 24 Apr 2008 21:23:30 -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 m3P0NRtU011005; Thu, 24 Apr 2008 20:23:27 -0400 (EDT) To: Dave Cramer cc: postgresql performance list Subject: Re: Planner won't use composite index if there is an order by ???? In-reply-to: References: <4810E914.9030109@ourstage.com> Comments: In-reply-to Dave Cramer message dated "Thu, 24 Apr 2008 20:06:01 -0400" Date: Thu, 24 Apr 2008 20:23:27 -0400 Message-ID: <11004.1209083007@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200804/366 X-Sequence-Number: 29859 Dave Cramer writes: > Any idea why it wouldn't choose the right index ? It thinks that way is cheaper. It's estimating its cost at 8381, whereas selecting all the rows then sorting must take 10131 plus some time to sort. The reason why those estimates are so far off from reality is directly tied to the 400-estimated-vs-2-actual rowcount estimation error. I'm not sure how much of that could be fixed by raising the stats target for the table, but that's certainly something to try. Another thing you should look at is eliminating dependences between columns. I'll bet that the "scored_at is not null" condition is either redundant with the "score in ..." condition, or could be made so (ie force score to null when scored_at is null). If you could get rid of the separate test on scored_at, it'd help to avoid the estimation weak spot of correlated restrictions. regards, tom lane