Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id 1F1BD11DF228 for ; Thu, 31 May 2012 19:48:17 -0300 (ADT) Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1SaEAA-0005Ta-So for pgsql-bugs@postgresql.org; Thu, 31 May 2012 22:48:16 +0000 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 q4VMm1QT023376; Thu, 31 May 2012 18:48:01 -0400 (EDT) To: Postgres User cc: "pgsql-bugs@postgresql.org" Subject: Re: BUG #6668: hashjoin cost problem In-reply-to: <1338443837.61025.YahooMailNeo@web121105.mail.ne1.yahoo.com> References: <28259.1338440604@sss.pgh.pa.us> <1338443837.61025.YahooMailNeo@web121105.mail.ne1.yahoo.com> Comments: In-reply-to Postgres User message dated "Wed, 30 May 2012 22:57:17 -0700" Date: Thu, 31 May 2012 18:48:01 -0400 Message-ID: <23375.1338504481@sss.pgh.pa.us> From: Tom Lane X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201205/249 X-Sequence-Number: 33220 Postgres User writes: > Why is cost_hashjoin estimating 50 billion tuple comparisons for 10K rows of output though? Well, if it hashes the smaller table, there's 100 million rows on the outside, and each of them will probe one hash chain in the hash table. If you're unlucky, each of those probes will hit a populated hash chain with at least 1000 entries, leading to 100 billion comparisons. I think it might derate that worst-case by a factor of 2. Now if you're lucky, a lot of the outer tuples hit unpopulated hash chains and so the number of comparisons is a lot less --- but in non-artificial examples, that's not a very good bet to make. The conservative assumption is that both sides of the join have similar key distributions, so that the more populated hash chains are also more likely to be probed. The cost estimate is therefore designed to discriminate against using an inner relation with a non-flat distribution. regards, tom lane