Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gYHzx-000800-QM for pgsql-general@arkaria.postgresql.org; Sat, 15 Dec 2018 22:01:25 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gYHzu-0003xF-SU for pgsql-general@arkaria.postgresql.org; Sat, 15 Dec 2018 22:01:22 +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_SHA1:256) (Exim 4.89) (envelope-from ) id 1gYHzu-0003x8-I5 for pgsql-general@lists.postgresql.org; Sat, 15 Dec 2018 22:01:22 +0000 Received: from mail-oi1-x242.google.com ([2607:f8b0:4864:20::242]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gYHzr-0002ZK-6Q for pgsql-general@lists.postgresql.org; Sat, 15 Dec 2018 22:01:22 +0000 Received: by mail-oi1-x242.google.com with SMTP id y23so7392633oia.4 for ; Sat, 15 Dec 2018 14:01:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding:content-language; bh=1cIA7LP/t9okgnlHSin0+x2LN2lQCe7unsDZ6d0OCgI=; b=I5aU9g4nxkV7YEh6jTndIZSNILVEOooj+lanKTzzoUnVcQtVs2upppRHxQcrgKgszB oVs30kHUMntrL3wpd63wVBnAjjwtnzOgKrEA8b6j31+P1TZsieQVfFC1u2xDVnOfJEVj HpfdiVTJlnbKRnAyvjMFD0GucCYECqNRkI4f7uMQu+Nswt/mJIP7g5GMjj1WQr/e9DrB MqCVJCyTLNInVMDhc9zOY7i87Ebnr+HLVGtRr1Q/oWRErAz4k264tasmCX87bkEMZ8ZT H13yhyQIUGBuZgDC1xtq0O/tfiA1uSuee2Oms+ZhSYDTksWZ9ZQAiQURAwy9zq0UEFDb trIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=1cIA7LP/t9okgnlHSin0+x2LN2lQCe7unsDZ6d0OCgI=; b=kIjAPR9R0bkvTM6TxyNvrJg0HG4PwWJWC6JlXCL18VokaViqCjf47kwmHfO6XUEdVX csK9oNZkxjmMksLGRvbHCRntnlX2UaL0satTFH1BRMHdB5GgamjHF9DcIMqp/8iy74kn GmpYBqDsrfV84iF8y8mkXU8jT64PNrXaBTvrrfcoBfd08da/ii7Oo/LdqC+tkW5f9Dge AjI+kwsbWbOYRoetvljnr6sgX3v82u6TloBXmNuUbQS7cbRXtgCujO1ciEBWiMzeyAEa Wwv9eaHr4zxwRv0EGNPSxdd0lzfjmFNwA5ZbQLyaTlziFU4OYZS7pCwQVRNMenTg1oB6 xuag== X-Gm-Message-State: AA+aEWaz3fbj+f2QhEJJexRg/r3QizpiondtFLKo1HOiUzKS07AWaOBK RIdWYPjjbwInb/On4rfq8iReFrUk X-Google-Smtp-Source: AFSGD/X5j7CqWLyS0n9P3jx0Ayur9VZ61lR+EBO5wb/x3jp9SFjo6OS/8/MZ/kLWdgIElMbq7Hm2ew== X-Received: by 2002:aca:3644:: with SMTP id d65mr2205441oia.336.1544911275490; Sat, 15 Dec 2018 14:01:15 -0800 (PST) Received: from [192.168.1.10] (ip70-171-116-89.no.no.cox.net. [70.171.116.89]) by smtp.googlemail.com with ESMTPSA id q131sm3955727oih.1.2018.12.15.14.01.14 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 15 Dec 2018 14:01:14 -0800 (PST) Subject: Re: simple query on why a merge join plan got selected To: pgsql-general@lists.postgresql.org References: From: Ron Message-ID: Date: Sat, 15 Dec 2018 16:01:13 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk I'd run each query multiple times -- before and after disabling mergejoin -- to ensure that all the data is safely in RAM. On 12/15/2018 02:13 PM, Vijaykumar Jain wrote: > Hey Guys, > > I was just playing with exploring joins and plans i came across this > > create table t1(a int); > create table t2(a int); > insert into t1 select (x % 10) from generate_series(1, 100000) x; > insert into t2 select (x % 100) from generate_series(1, 100000) x; > > pgtesting=> analyze t1; > ANALYZE > > pgtesting=> analyze t2; > ANALYZE > > > this is reproducible > > the below query by default makes use of merge join (which takes way > longer to return rows as compared to when i explicitly disable merge > join it returns in half the time taken by merge join) but i am not > able to figure out why, although i have run analyze on the tables. > > > pgtesting=> explain (analyze, buffers) select * from t1 join t2 using (a); > > QUERY PLAN > > ----------------------------------------------------------------------------------------------------------------------- > > Merge Join (cost=19495.64..1039705.09 rows=97241600 width=4) (actual > time=124.153..22243.262 rows=100000000 loops=1) > > Merge Cond: (t1.a = t2.a) > > Buffers: shared hit=886, temp read=320384 written=616 > > -> Sort (cost=9747.82..9997.82 rows=100000 width=4) (actual > time=56.442..81.611 rows=100000 loops=1) > > Sort Key: t1.a > > Sort Method: external merge Disk: 1376kB > > Buffers: shared hit=443, temp read=172 written=173 > > -> Seq Scan on t1 (cost=0.00..1443.00 rows=100000 width=4) > (actual time=0.030..10.003 rows=100000 loops=1) > > Buffers: shared hit=443 > > -> Sort (cost=9747.82..9997.82 rows=100000 width=4) (actual > time=67.702..9469.366 rows=100000001 loops=1) > > Sort Key: t2.a > > Sort Method: external sort Disk: 1768kB > > Buffers: shared hit=443, temp read=220222 written=443 > > -> Seq Scan on t2 (cost=0.00..1443.00 rows=100000 width=4) > (actual time=0.013..8.186 rows=100000 loops=1) > > Buffers: shared hit=443 > > Planning time: 0.402 ms > > Execution time: 26093.192 ms > > (17 rows) > > > pgtesting=> set enable_mergejoin TO FALSE; > > SET > > pgtesting=> explain (analyze, buffers) select * from t1 join t2 using (a); > > QUERY PLAN > > --------------------------------------------------------------------------------------------------------------------- > > Hash Join (cost=3084.00..1117491.00 rows=97241600 width=4) (actual > time=26.893..10229.924 rows=100000000 loops=1) > > Hash Cond: (t1.a = t2.a) > > Buffers: shared hit=889, temp read=273 written=271 > > -> Seq Scan on t1 (cost=0.00..1443.00 rows=100000 width=4) > (actual time=0.028..18.123 rows=100000 loops=1) > > Buffers: shared hit=443 > > -> Hash (cost=1443.00..1443.00 rows=100000 width=4) (actual > time=26.255..26.255 rows=100000 loops=1) > > Buckets: 131072 Batches: 2 Memory Usage: 2713kB > > Buffers: shared hit=443, temp written=152 > > -> Seq Scan on t2 (cost=0.00..1443.00 rows=100000 width=4) > (actual time=0.017..9.163 rows=100000 loops=1) > > Buffers: shared hit=443 > > Planning time: 0.099 ms > > Execution time: 14095.975 ms > > (12 rows) > > > pgtesting=> show work_mem; > > work_mem > > ---------- > > 4MB > > (1 row) > > > pgtesting=> show shared_buffers; > > shared_buffers > > ---------------- > > 1GB > > (1 row) > > > pgtesting=> select version(); > > > version > > --------------------------------------------------------------------------------------------------------------------------------------------- > > PostgreSQL 10.5 (Ubuntu 10.5-1.pgdg16.04+1) on x86_64-pc-linux-gnu, > compiled by gcc (Ubuntu 5.4.0-6ubuntu1~16.04.10) 5.4.0 20160609, > 64-bit > > (1 row) > > > May be i am missing something way obvious :) but my only concern being > high cardinality joins may not use the statistics correctly? > > Regards, > Vijay > -- Angular momentum makes the world go 'round.