Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 38D241337B39 for ; Tue, 19 Oct 2010 00:43:54 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 16100-05 for ; Tue, 19 Oct 2010 03:43:47 +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 EE9D01337B34 for ; Tue, 19 Oct 2010 00:43:46 -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 o9J3hjwD001533; Mon, 18 Oct 2010 23:43:46 -0400 (EDT) To: Scott Carey cc: "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 "Mon, 18 Oct 2010 18:40:11 -0700" Date: Mon, 18 Oct 2010 23:43:45 -0400 Message-ID: <1532.1287459825@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: 201010/297 X-Sequence-Number: 40715 Scott Carey writes: > I consistently see HashJoin plans that hash the large table, and scan > the small table. Could we see a self-contained test case? And what cost parameters are you using, especially work_mem? > This is especially puzzling in some cases where I have 30M rows in the big table and ~ 100 in the small... shouldn't it hash the small table and scan the big one? Well, size of the table isn't the only factor; in particular, a highly nonuniform distribution of the key value will inflate the cost estimate for using a table on the inner size of the hash. But the example you show here seems a bit extreme. regards, tom lane