Received: from localhost (unknown [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id A12059F9C9D for ; Mon, 24 Sep 2007 10:28:39 -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 99516-03-4 for ; Mon, 24 Sep 2007 10:28:34 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from outmail129149.authsmtp.com (outmail129149.authsmtp.com [62.13.129.149]) by postgresql.org (Postfix) with ESMTP id 5EAEA9FA040 for ; Mon, 24 Sep 2007 10:28:34 -0300 (ADT) Received: from outmail128184.authsmtp.net (outmail128184.authsmtp.net [62.13.128.184]) by punt2.authsmtp.com (8.13.8/8.13.8/Kp) with ESMTP id l8ODST2o071087; Mon, 24 Sep 2007 14:28:29 +0100 (BST) Received: from [192.168.0.3] (85-211-149-22.dyn.gotadsl.co.uk [85.211.149.22]) (authenticated bits=0) by mail.authsmtp.com (8.13.8/8.13.8/Kp) with ESMTP id l8ODSSsk021098; Mon, 24 Sep 2007 14:28:28 +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: <27857.1190417434@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> <1190399879.4202.127.camel@ebony.site> <27857.1190417434@sss.pgh.pa.us> Content-Type: text/plain Date: Mon, 24 Sep 2007 14:27:09 +0100 Message-Id: <1190640429.4181.165.camel@ebony.site> Mime-Version: 1.0 X-Mailer: Evolution 2.8.2 Content-Transfer-Encoding: 7bit X-Server-Quench: 09a0617f-6aa2-11dc-b86a-001185d377ca X-AuthRoute: OCdxZQATClZOTQEd DAteCiNZVAwpPBRK HVkIKg5MJUcNSQVJ NksadRtFagFbflxB HGQLW1NEUVl7W2t/ aQMfZQBDYEtPQQRu WVZLQE1QERpoT0AJ eWp9MVopdwdHeHd4 K0VhWHUVXE0rdUB8 RRhJEzxXZ3phaTRM TRJQcVJJcANLfxdA PlN4UyBcLwdSbGoL JyIJERcLGAAXEylc WAoNLFQdCUgMBHYg Rx1KFjwyBkBNQCt7 JlQsI1JUBEIYNC05 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/413 X-Sequence-Number: 26958 On Fri, 2007-09-21 at 19:30 -0400, Tom Lane wrote: > Simon Riggs writes: > > 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. > > [ shrug... ] Your perspective is mistaken. There is nothing wrong with > the way the LIMIT estimation is being done. The plan in question was > > Limit (cost=0.00..498511.80 rows=10 width=1804) (actual time=17.729..21.672 rows=2 loops=1) > -> Nested Loop (cost=0.00..1794642.48 rows=36 width=1804) (actual time=17.729..21.671 rows=2 loops=1) > -> Index Scan using pk_table_a on table_a ta (cost=0.00..324880.88 rows=388638 width=16) (actual time=0.146..0.198 rows=2 loops=1) > Index Cond: (a = $1) > -> Index Scan using pk_table_b2 on table_b2 tb (cost=0.00..3.77 rows=1 width=1788) (actual time=10.729..10.731 rows=1 loops=2) > Index Cond: (ta.b = tb.b) > Total runtime: 21.876 ms > > and there are two fairly serious estimation errors here, neither related > at all to the LIMIT: > > * five-orders-of-magnitude overestimate of the number of table_a rows > that will match the condition on a; I don't see any problem with this estimate, but I do now agree there is a problem with the other estimate. We check to see if the value is an MFV, else we assume that the distribution is uniformly distributed across the histogram bucket. Csaba provided details of the fairly shallow distribution of values of a in table_a. 96% of rows aren't covered by the MFVs, so its a much shallower distribution than is typical, but still easily possible. So based upon what we know there should be ~330,000 rows with the value of a used for the EXPLAIN. So it looks to me like we did the best we could with the available information, so I can't see that as a planner problem per se. We cannot do better a priori without risking worse plans in other circumstances. > * enormous underestimate of the number of join rows --- it's apparently > thinking only 0.0001 of the table_a rows will have a join partner, > whereas at least for this case they all do. OK, I agree this estimate does have a problem and it has nothing to do with LIMIT. Looking at the code I can't see how this selectivity can have been calculated. AFAICS eqjoinsel() gives a selectivity of 1.0 using the data supplied by Csaba and it ought to cover this case reasonably well. Csaba, please can you copy that data into fresh tables, re-ANALYZE and then re-post the EXPLAINs, with stats data. -- Simon Riggs 2ndQuadrant http://www.2ndQuadrant.com