pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Kumar, Virendra <Virendra.Kumar@guycarp.com>
Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: Re: NL Join vs Merge Join - 5 hours vs 2 seconds
Date: Mon, 17 Dec 2018 12:02:12 -0500
Message-ID: <32743.1545066132@sss.pgh.pa.us> (raw)
In-Reply-To: <e86620f9f6a94838b513e2218f0cef55@USFKL11XG20CN01.mercer.com>
References: <e86620f9f6a94838b513e2218f0cef55@USFKL11XG20CN01.mercer.com>

"Kumar, Virendra" <Virendra.Kumar@guycarp.com> writes:
> We are using Postgers-XL based on PostgreSQL 10.5 on RHEL7. We have an extreme case of one query which running in 4 hour 45 mins and 33 seconds with Nested Loops Join vs 2.5 seconds when we disable enable_nestloop. The query is given below. I would be happy to share more information if required.

It looks like what's burning you is bad estimates at these joins:

>                                        ->  Nested Loop  (cost=33681.00..71431.33 rows=1 width=348) (actual time=359.006..926.303 rows=37207 loops=3)
>                                              Join Filter: (se.account_id = a.account_id)
>                                              ->  Hash Join  (cost=33680.71..71298.94 rows=395 width=300) (actual time=358.953..656.923 rows=36828 loops=3)
>                                                    Hash Cond: ((se.account_id = sh.account_id) AND (se.site_id = sh.site_id))
...
>                                              ->  Index Scan using account_p1000013_account_id_idx on account_p1000013 a  (cost=0.29..0.32 rows=1 width=52) (actual time=0.003..0.005 rows=1 loops=110485)
>                                                    Index Cond: (account_id = sh.account_id)
>                                                    Filter: (portfolio_id = 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




view thread (4+ messages)  latest in thread

Message-ID: <32743.1545066132@sss.pgh.pa.us>
Permalink:  ../32743.1545066132@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/32743.1545066132@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: tgl@sss.pgh.pa.us, Virendra.Kumar@guycarp.com
  Subject: Re: NL Join vs Merge Join - 5 hours vs 2 seconds
  In-Reply-To: <32743.1545066132@sss.pgh.pa.us>
  References: <e86620f9f6a94838b513e2218f0cef55@USFKL11XG20CN01.mercer.com> <32743.1545066132@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox