Received: from localhost (unknown [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id 208489FB504 for ; Fri, 21 Sep 2007 15:39:40 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-maia, port 10024) with ESMTP id 50514-09 for ; Fri, 21 Sep 2007 15:39:24 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from outmail128173.authsmtp.co.uk (outmail128173.authsmtp.co.uk [62.13.128.173]) by postgresql.org (Postfix) with ESMTP id 8BFB79FB4DD for ; Fri, 21 Sep 2007 15:39:26 -0300 (ADT) Received: from outmail128185.authsmtp.co.uk (outmail128185.authsmtp.co.uk [62.13.128.185]) by punt3.authsmtp.com (8.13.8/8.13.8/Kp) with ESMTP id l8LIdN85049452; Fri, 21 Sep 2007 19:39:23 +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 l8LIdIPI007051; Fri, 21 Sep 2007 19:39:18 +0100 (BST) Subject: Re: Searching for the cause of a bad plan From: Simon Riggs To: Tom Lane Cc: Csaba Nagy , postgres performance list In-Reply-To: <13644.1190397220@sss.pgh.pa.us> References: <1190369024.4661.173.camel@PCD12478> <6175.1190390913@sss.pgh.pa.us> <1190396352.4202.109.camel@ebony.site> <13644.1190397220@sss.pgh.pa.us> Content-Type: text/plain Date: Fri, 21 Sep 2007 19:37:59 +0100 Message-Id: <1190399879.4202.127.camel@ebony.site> Mime-Version: 1.0 X-Mailer: Evolution 2.8.2 Content-Transfer-Encoding: 7bit X-Server-Quench: f67ecc54-6871-11dc-b86a-001185d377ca X-AuthRoute: OCdxZQATClZOTQEd DAteCiNZVAwpPBRK HVkIKg5MJUcNSQVJ NksadRtFagRbflxB HGQLW1NEUVR7Wmp/ agMfZQBDYEtPQQdr TklWQFdNFEs0CWB4 Umd+KxlycAVDeTBx YkNkECZdWxEpJkN5 X0hcEmwbZGY0O31N 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/380 X-Sequence-Number: 26925 On Fri, 2007-09-21 at 13:53 -0400, Tom Lane wrote: > Simon Riggs writes: > > On Fri, 2007-09-21 at 12:08 -0400, Tom Lane wrote: > >> The reason you get a bad plan is that this rowcount estimate is so far > >> off: > > > That's true, but its not relevant, > > Yes it is --- the reason it wants to use a hashjoin instead of a > nestloop is exactly that it thinks the loop would iterate too many > times. (Ten is already too many in this case --- if it had estimated > five rows out of the join, it'd have gone with the nestloop, since > the cost estimate difference at the top level is less than 2x.) That's not my perspective. If the LIMIT had been applied accurately to the cost then the hashjoin would never even have been close to the nested join in the first place. It's just chance that the frequency distribution is favourable to us and thus amenable to using the hint of improving stats_target. The improved knowledge of the distribution just hides the fact that the cost model is still wrong: a cost of 45000 per row shows this. -- Simon Riggs 2ndQuadrant http://www.2ndQuadrant.com