Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id A1D0FB60EAF for ; Thu, 2 Jun 2011 11:57:35 -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 71378-01-4 for ; Thu, 2 Jun 2011 14:57:28 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from gw.wicourts.gov (gwmta.wicourts.gov [165.219.244.99]) by mail.postgresql.org (Postfix) with ESMTP id 58BDDB61FB1 for ; Thu, 2 Jun 2011 11:57:28 -0300 (ADT) Received: from Courts-MTA by gw.wicourts.gov with Novell_GroupWise; Thu, 02 Jun 2011 09:57:28 -0500 Message-Id: <4DE75E85020000250003DFEB@gw.wicourts.gov> X-Mailer: Novell GroupWise Internet Agent 8.0.1 Date: Thu, 02 Jun 2011 09:57:25 -0500 From: "Kevin Grittner" To: "srobertjames" , "pgsql-performance" Subject: Re: Understanding Hash Join performance References: In-Reply-To: Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Content-Disposition: inline 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: 201106/19 X-Sequence-Number: 43952 Robert James wrote: > A query I has spends a long time on Hash Joins (and Hash Left > Joins). To submit a post which gives us enough information to help you speed up that query, please read this page: http://wiki.postgresql.org/wiki/SlowQueryQuestions > I have a few questions: > > 1. When does Postgres decide to do a Hash Join, over another type > of Join? > 2. Do Hash Joins normally perform poorly? What can I do to speed > them up? > 3. What can I do to enable Postgres to use a faster type of join? Questions this general can only be answered in a general way, so here goes. The planner doesn't choose a particular plan type, exactly -- it generates a lot of alternative plans,, basically looking at all the ways it knows how to retrieve the requested set of data, and estimates a cost for each plan based on available resources and adjustable costing factors. It will choose the plan with the lowest estimated cost. There are many situations where a hash join is faster than the alternatives. If it's using one where another alternative is actually faster, it's not a matter of "enabling a faster join type" -- it's a matter of setting your cost factors to accurately reflect the real costs on your system. You can generally make hash joins faster by increasing work_mem, but that tends to cause data to be pushed from cache sooner and can run you out of memory entirely, so it must be tuned carefully. And the planner does take the size of work_mem and the expected data set into consideration when estimating the cost of the hash join. -Kevin