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 1gYwHd-0006Ui-6J for pgsql-general@arkaria.postgresql.org; Mon, 17 Dec 2018 17:02:21 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gYwHb-0004M6-Jr for pgsql-general@arkaria.postgresql.org; Mon, 17 Dec 2018 17:02:19 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gYwHb-0004Lz-8c for pgsql-general@lists.postgresql.org; Mon, 17 Dec 2018 17:02:19 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gYwHY-0001ow-Ud for pgsql-general@lists.postgresql.org; Mon, 17 Dec 2018 17:02:18 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id wBHH2Cla032744; Mon, 17 Dec 2018 12:02:12 -0500 From: Tom Lane To: "Kumar, Virendra" cc: "pgsql-general@lists.postgresql.org" Subject: Re: NL Join vs Merge Join - 5 hours vs 2 seconds In-reply-to: References: Comments: In-reply-to "Kumar, Virendra" message dated "Mon, 17 Dec 2018 15:42:20 +0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <32742.1545066132.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Mon, 17 Dec 2018 12:02:12 -0500 Message-ID: <32743.1545066132@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk "Kumar, Virendra" writes: > We are using Postgers-XL based on PostgreSQL 10.5 on RHEL7. We have an e= xtreme case of one query which running in 4 hour 45 mins and 33 seconds wi= th Nested Loops Join vs 2.5 seconds when we disable enable_nestloop. The q= uery is given below. I would be happy to share more information if require= d. It looks like what's burning you is bad estimates at these joins: > -> Nested Loop (cost=3D33681.00= ..71431.33 rows=3D1 width=3D348) (actual time=3D359.006..926.303 rows=3D37= 207 loops=3D3) > Join Filter: (se.account_id= =3D a.account_id) > -> Hash Join (cost=3D3368= 0.71..71298.94 rows=3D395 width=3D300) (actual time=3D358.953..656.923 row= s=3D36828 loops=3D3) > Hash Cond: ((se.accou= nt_id =3D sh.account_id) AND (se.site_id =3D sh.site_id)) ... > -> Index Scan using accoun= t_p1000013_account_id_idx on account_p1000013 a (cost=3D0.29..0.32 rows=3D= 1 width=3D52) (actual time=3D0.003..0.005 rows=3D1 loops=3D110485) > Index Cond: (account_= id =3D sh.account_id) > Filter: (portfolio_id= =3D 1000013) I'm guessing that account_id, site_id, and portfolio_id are all highly correlated, but the planner doesn't know that and thinks the additional conditions will remove way more rows than they actually do. In PG10 and later, you can probably fix that by creating functional-dependency statistics on those pairs of columns. regards, tom lane