Received: from localhost (unknown [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id 227B89FA1F1 for ; Fri, 21 Sep 2007 14:20:07 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-maia, port 10024) with ESMTP id 83979-04-3 for ; Fri, 21 Sep 2007 14:19:38 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from outmail129165.authsmtp.net (outmail129165.authsmtp.net [62.13.129.165]) by postgresql.org (Postfix) with ESMTP id E02A99FA28D for ; Fri, 21 Sep 2007 14:19:37 -0300 (ADT) Received: from outmail128184.authsmtp.net (outmail128184.authsmtp.net [62.13.128.184]) by punt3.authsmtp.com (8.13.8/8.13.8/Kp) with ESMTP id l8LHJTfm012594; Fri, 21 Sep 2007 18:19:30 +0100 (BST) Received: from [192.168.0.3] (85-211-21-1.dyn.gotadsl.co.uk [85.211.21.1]) (authenticated bits=0) by mail.authsmtp.com (8.13.8/8.13.8/Kp) with ESMTP id l8LHJSLl041850; Fri, 21 Sep 2007 18:19:29 +0100 (BST) Subject: Re: Searching for the cause of a bad plan From: Simon Riggs To: Csaba Nagy Cc: postgres performance list In-Reply-To: <1190384799.4661.217.camel@PCD12478> References: <1190369024.4661.173.camel@PCD12478> <1190372365.4202.14.camel@ebony.site> <1190374166.4661.194.camel@PCD12478> <1190374478.4202.20.camel@ebony.site> <1190376744.4661.208.camel@PCD12478> <1190381777.4202.50.camel@ebony.site> <1190384799.4661.217.camel@PCD12478> Content-Type: text/plain Date: Fri, 21 Sep 2007 18:18:09 +0100 Message-Id: <1190395089.4202.103.camel@ebony.site> Mime-Version: 1.0 X-Mailer: Evolution 2.8.2 Content-Transfer-Encoding: 7bit X-Server-Quench: cfa17dd5-6866-11dc-b86a-001185d377ca X-AuthRoute: OCdxZQATClZOTQEd DAteCiNZVAwpPBRK HVkIKg5MJUcNSQVJ NksadBtFagRbflxB HGQLW1NEUVV7WGp/ aQMfZQBDYEtPQQdr TklWQFdNFEs0CWB5 fH1iDhl2dg1DeDBy Z0ZqECMNDUV9IRJ4 X0hcE2sbZGY0O31N WEYJagNUcQJCe0tG bVcsBT1vNG8XDRoV JSEUBRUEdQ5SIT5e QgALIhocRlxDAj4x DxsLAC4iVUoLDyx7 JRUqYkYYFUB5 X-Authentic-SMTP: 61633235383639.squirrel.dmpriest.net.uk:752/Kp X-Report-SPAM: If SPAM / abuse - report it at: http://www.authsmtp.com/abuse X-Virus-Status: No virus detected - but ensure you scan with your own anti-virus system! X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200709/371 X-Sequence-Number: 26916 On Fri, 2007-09-21 at 16:26 +0200, Csaba Nagy wrote: > [snip] > > Ok, I was not able to follow your explanation, it's too deep for me into > what the planner does... I'm thinking that this case is too narrow to do too much with, when I think about how we might do what I proposed. OTOH this isn't the first bad plan we've had because we used the index for ordering. There might be some common link that we can improve upon. > > Incidentally, the way out of this is to improve the stats by setting > > stats target = 1000 on column a of ta. That will allow the optimizer to > > have a better estimate of the tail of the distribution of a, which > > should then be more sensibly reflected in the cost of the Index Scan. > > That doesn't solve the actual problem, but should help in your case. > > OK, I can confirm that. I set the statistics target for column "a" on > table_a to 1000, analyzed, and got the plan below. The only downside is > that analyze became quite expensive on table_a, it took 15 minutes and > touched half of the pages... I will experiment with lower settings, > maybe it will work with less than 1000 too. Well, we know there are ways of optimizing ANALYZE. ISTM we should be able to auto-select stats target based upon the shape of the frequency distribution of the column values. We'd need to make some calculations about the index cost model, but its probably worth it for the future. -- Simon Riggs 2ndQuadrant http://www.2ndQuadrant.com