Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1aQBHA-0001jO-QM for pgsql-performance@arkaria.postgresql.org; Mon, 01 Feb 2016 10:00:05 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1aQBHA-0005e1-9L for pgsql-performance@arkaria.postgresql.org; Mon, 01 Feb 2016 10:00:04 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1aQBH9-0005dv-Tj for pgsql-performance@postgresql.org; Mon, 01 Feb 2016 10:00:03 +0000 Received: from mail-wm0-x229.google.com ([2a00:1450:400c:c09::229]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84) (envelope-from ) id 1aQBH1-0003uo-Mg for pgsql-performance@postgresql.org; Mon, 01 Feb 2016 10:00:03 +0000 Received: by mail-wm0-x229.google.com with SMTP id p63so62492216wmp.1 for ; Mon, 01 Feb 2016 01:59:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-type:content-transfer-encoding; bh=AUukKlz5OiCSOtGRruaBMmXbt3Vg/LDyPoxJQtQ+I08=; b=o917wxhYyJg/yGMtTNeLtFoXvijx4UCAZ1d3IkUKRpPb3GPeYqVYDxMZkK5jgvEA6Z MDPjzJaG80gzB4E5hS04AskDEWpl07VtwulwqRRGaDxOs3IrAZB5AhkgmnUUIqxX4Ypw 8BEE9CuZtGv84a/IL69+/cyIbODFKBQH0NypDsNBjMfa4GpflhHZwPsoxnJrFK8Cc+mi Z7WsRlMvS8XbosqD0qDmz6bwB2VtzsRVtoezeTuyYAbBnvylm1TaGuBqkEamC+Uc7KNM 9sCzWD+ewqUpDauEklJmuXbG9LQFoNP7ueiVMzr3GNFQBSY3FhdqBCPMJ1h9LiY93FB7 iBig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-type :content-transfer-encoding; bh=AUukKlz5OiCSOtGRruaBMmXbt3Vg/LDyPoxJQtQ+I08=; b=HDH4sC3AIo9WIFOIbW7qVXwUplDkJtVfyg4FSRZspCLo+vQMPtgkAt6WJXZvPhbvxt Q63tl4fj8d1yvqBS+cBVMkoVhJ+X3qJw6a990AxYH5bETk+Y1sMQ1dI+CSZ+TGwUatMl JyVZJtAb/3kzyWbezSpdOi7HX2nwm4jvSKUAay1U0YY67ltEGe+YhAF5XGbPVcGNbuy9 Mh0FMyFApAt2dkos4Qv/ZLmw8PhN0fam2F+NwqxlBUGeGHl43nBpQkUFht1NsBYGUvqC n6EiL/R7QsnFzypwfGIfkbGIouOTohVReZup3sstjh7PMACtxjDQY+Ad2TGy6NC7uFCj Lchw== X-Gm-Message-State: AG10YOQoTl4GKSj/AzgAQpSzYYrajwA23j0CXnr2CvmH2dF0HeTfO4KJZEkRtVFmHTyrX7c+bIJKcyjsHrCAat4F2YUkALlEmjgIW55v97NqpBFKkKOy+NGAfsobczsr77tMxVdQB8cZMdzhbKIHfQ920Ic2tveIQSMCQqBKVGuMkUT5rNz5jnrqEgwkk4hXAmqqTg+ZjZI0ssFfVQ== X-Received: by 10.28.89.69 with SMTP id n66mr11375833wmb.63.1454320794800; Mon, 01 Feb 2016 01:59:54 -0800 (PST) Received: from [10.137.2.14] ([212.76.254.194]) by smtp.gmail.com with ESMTPSA id x2sm28246032wjf.13.2016.02.01.01.59.53 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 01 Feb 2016 01:59:54 -0800 (PST) Subject: Re: Hash join gets slower as work_mem increases? To: Albe Laurenz , "pgsql-performance@postgresql.org" References: <56ACC50B.1090007@2ndquadrant.com> From: Tomas Vondra Message-ID: <56AF2C97.4040303@2ndquadrant.com> Date: Mon, 1 Feb 2016 10:59:51 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.1.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org On 02/01/2016 10:38 AM, Albe Laurenz wrote: > Tomas Vondra wrote: ... > I didn't post the whole plan since it is awfully long, I'll include hyperlinks > for the whole plan. > > work_mem = '100MB' (http://explain.depesz.com/s/7b6a): > > -> Hash Join (cost=46738.74..285400.61 rows=292 width=8) (actual time=4296.986..106087.683 rows=187222 loops=1) > Hash Cond: ("*SELECT* 1_2".postadresse_id = p.postadresse_id) > Buffers: shared hit=1181177 dirtied=1, temp read=7232 written=7230 > [...] > -> Hash (cost=18044.92..18044.92 rows=4014 width=8) (actual time=4206.892..4206.892 rows=3096362 loops=1) > Buckets: 1024 Batches: 2 (originally 1) Memory Usage: 102401kB > Buffers: shared hit=1134522 dirtied=1, temp written=5296 > > work_mem = '500MB' (http://explain.depesz.com/s/Cgkl): > > -> Hash Join (cost=46738.74..285400.61 rows=292 width=8) (actual time=3802.849..245970.049 rows=187222 loops=1) > Hash Cond: ("*SELECT* 1_2".postadresse_id = p.postadresse_id) > Buffers: shared hit=1181175 dirtied=111 > [...] > -> Hash (cost=18044.92..18044.92 rows=4014 width=8) (actual time=3709.584..3709.584 rows=3096360 loops=1) > Buckets: 1024 Batches: 1 Memory Usage: 120952kB > Buffers: shared hit=1134520 dirtied=111 > > Does that support your theory? > > There is clearly an underestimate here, caused by correlated attributes, but > is that the cause for the bad performance with increased work_mem? Yes, that's clearly the culprit here. In both cases we estimate here are only ~4000 tuples in the hash, and 9.3 sizes the hash table to have at most ~10 tuples per bucket (in a linked list). However we actually get ~3M rows, so there will be ~3000 tuples per bucket, and that's extremely expensive to walk. The reason why 100MB is faster is that it's using 2 batches, thus making the lists "just" ~1500 tuples long. This is pretty much exactly the reason why I reworked hash joins in 9.5. I'd bet it's going to be ~20x faster on that version. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance