Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1aPWHW-00017i-R2 for pgsql-performance@arkaria.postgresql.org; Sat, 30 Jan 2016 14:13:43 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1aPWHW-0002FT-Dw for pgsql-performance@arkaria.postgresql.org; Sat, 30 Jan 2016 14:13:42 +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 1aPWHV-0002E6-Jp for pgsql-performance@postgresql.org; Sat, 30 Jan 2016 14:13:41 +0000 Received: from mail-wm0-x232.google.com ([2a00:1450:400c:c09::232]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84) (envelope-from ) id 1aPWHR-0000vf-33 for pgsql-performance@postgresql.org; Sat, 30 Jan 2016 14:13:41 +0000 Received: by mail-wm0-x232.google.com with SMTP id 128so16555288wmz.1 for ; Sat, 30 Jan 2016 06:13:36 -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=TwzuniGTsAZg+s0Et3kCbnQMExfeCmNytXlT506WRGU=; b=bckoJkR9SzaQ7FxLP/0nWfcMkQyg4wMiPM26x3fXj0scfqejNXmHB0jH54K/6ZNxhc 03vT5MsN6TwMNKnAy7oWnNBXNovYHj3iTr1i5PSFEqtY5k4NI8t7idJahjhQeAuwvvQ8 tx2Yyg48t2aqJ0gNjL5DkD9zfDME8tj0gd+yOHES4czGIgBVEkWrncEY7D7Dhg3ZNSO/ rbZw60G9UeGKEWPKVfmpsbspTz0IvFFWvP9P8rQKSiwdH4+PejpHJYOe3oBvgXaiS9s8 1neHgb+5r+ZLFhDP/zgXaJofGCTlsBrn6GqY3z3AXNBcyvzr9Xbo7Pn9D25Xmdi5qGns c05A== 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=TwzuniGTsAZg+s0Et3kCbnQMExfeCmNytXlT506WRGU=; b=flRxUBxFTa6Jg8eyV4gUv/+ks+L6q7Du1hETeKjly94g65fHPm4edoK3FAQYZvvnHz itzL1vd/t/RPzBMMZ5b1+pBRildBNGKO2FEELPmUB2+n20+2/wsA//UhWSGUk4gmvTPW zm7Qp1rBiSQOwggOJzw6t+pgRJKOfsxx6yhmRTADk8Auh70t793pKGd1a2ZK2EOpCvfV BLMV9G0SaBQig07If64CIPK4IlwyZIaZnFSCI4UJrJGvUvdwbJ0qcv4vQ64spCjB8Hu6 oukRN2SApekkyuNZ/vH35HcP0dHXLqnmi9L/MwEGUVp57Gen1aMVL+8O6HLcf69eUKH9 AKjQ== X-Gm-Message-State: AG10YOSXpdfa1a1e2eXnSU3+FqWLG+P6s3Fx99M3q8C9bY3T4zs05XAV/p7GccChKlzctWEBpT0gjElEL0Vt0pQ2HHWNX9kbrv2uEJgUYN9ubzInMKIyhlv6K9ilRQx5DoeVo/IWy+tU/gLNk3ZOGL0JyYC4r8/JDiiIvpknYa1JxXOYYEFCAVnBvn7Vd4BRZVqlXHOxISvQhrMieQ== X-Received: by 10.28.6.213 with SMTP id 204mr2781210wmg.47.1454163214162; Sat, 30 Jan 2016 06:13:34 -0800 (PST) Received: from [10.137.2.14] ([151.216.141.89]) by smtp.gmail.com with ESMTPSA id x10sm20264577wjx.8.2016.01.30.06.13.33 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 30 Jan 2016 06:13:33 -0800 (PST) Subject: Re: Hash join gets slower as work_mem increases? To: pgsql-performance@postgresql.org References: From: Tomas Vondra Message-ID: <56ACC50B.1090007@2ndquadrant.com> Date: Sat, 30 Jan 2016 15:13:31 +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 Hi, On 01/29/2016 04:17 PM, Albe Laurenz wrote: > I have a query that runs *slower* if I increase work_mem. > > The execution plans are identical in both cases, except that a temp file > is used when work_mem is smaller. > > The relevant lines of EXPLAIN ANALYZE output are: > > With work_mem='100MB': > -> 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 > > With work_mem='500MB': > -> 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 > > I ran operf on both backends, and they look quite similar, except that the > number of samples is different (this is "opreport -c" output): > > CPU: Intel Sandy Bridge microarchitecture, speed 2899.8 MHz (estimated) > Counted CPU_CLK_UNHALTED events (Clock cycles when not halted) with a unit mask of 0x00 (No unit mask) count 90000 > samples % image name symbol name > ------------------------------------------------------------------------------- > 112 0.0019 postgres ExecProcNode > 3020116 49.9904 postgres ExecScanHashBucket > 3021162 50.0077 postgres ExecHashJoin > 3020116 92.8440 postgres ExecScanHashBucket > 3020116 49.9207 postgres ExecScanHashBucket [self] > 3020116 49.9207 postgres ExecScanHashBucket > 8190 0.1354 vmlinux apic_timer_interrupt > > What could be an explanation for this? > Is this known behaviour? There is a bunch of possible causes for such behavior, but it's quite impossible to say if this is an example of one of them as you have not posted the interesting parts of the explain plan. Also, knowing PostgreSQL version would be useful. I don't think the example you posted is due to exceeding on-CPU cache as that's just a few MBs per socket, so the smaller work_mem is significantly larger. What I'd expect to be the issue here is under-estimate of the hash table size, resulting in too few buckets and thus long chains of tuples that need to be searched sequentially. Smaller work_mem values usually limit the length of those chains in favor of batching. Please, post the whole explain plan - especially the info about number of buckets/batches and the Hash node details. 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