Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 1D0531337BD9 for ; Wed, 13 Apr 2011 14:35:51 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 54842-06 for ; Wed, 13 Apr 2011 17:35:33 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by mail.postgresql.org (Postfix) with ESMTP id 194631337B83 for ; Wed, 13 Apr 2011 14:35:32 -0300 (ADT) 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 p3DHZVwZ008748; Wed, 13 Apr 2011 13:35:31 -0400 (EDT) To: Scott Carey cc: Robert Haas , "pgsql-performance@postgresql.org Performance" Subject: Re: HashJoin order, hash the large or small table? Postgres likes to hash the big one, why? In-reply-to: References: Comments: In-reply-to Scott Carey message dated "Wed, 13 Apr 2011 10:22:40 -0700" Date: Wed, 13 Apr 2011 13:35:31 -0400 Message-ID: <8747.1302716131@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-10 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201104/191 X-Sequence-Number: 43253 Scott Carey writes: >> On Oct 27, 2010, at 12:56 PM, Tom Lane wrote: >> Because a poorly distributed inner relation leads to long hash chains. >> In the very worst case, all the keys are on the same hash chain and it >> degenerates to a nested-loop join. > A pathological skew case (all relations with the same key), should be > _cheaper_ to probe. I think you're missing the point, which is that all the hash work is just pure overhead in such a case (and it is most definitely not zero-cost overhead). You might as well just do a nestloop join. Hashing is only beneficial to the extent that it allows a smaller subset of the inner relation to be compared to each outer-relation tuple. So I think biasing against skew-distributed inner relations is entirely appropriate. regards, tom lane