Received: from localhost (unknown [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id 465F89F994A for ; Fri, 21 Sep 2007 20:30:43 -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 05217-03 for ; Fri, 21 Sep 2007 20:30:40 -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 postgresql.org (Postfix) with ESMTP id 73F209FA481 for ; Fri, 21 Sep 2007 20:30:40 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.1/8.14.1) with ESMTP id l8LNUYAf027858; Fri, 21 Sep 2007 19:30:34 -0400 (EDT) To: Simon Riggs cc: Csaba Nagy , postgres performance list Subject: Re: Searching for the cause of a bad plan In-reply-to: <1190399879.4202.127.camel@ebony.site> 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> Comments: In-reply-to Simon Riggs message dated "Fri, 21 Sep 2007 19:37:59 +0100" Date: Fri, 21 Sep 2007 19:30:34 -0400 Message-ID: <27857.1190417434@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200709/393 X-Sequence-Number: 26938 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; * 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. Had the latter estimate been right, the cost of pulling results this way would indeed have been something like 50K units per joined row, because of the large number of inner index probes per successful join. It might be interesting to look into why those estimates are so far off; the stats Csaba displayed don't seem to have any obvious oddity that would justify such bizarre results. But the LIMIT has got nothing to do with this. regards, tom lane