From: Tom Lane <tgl@sss.pgh.pa.us>
To: Gustavo Rezende Montesino <gustavo.montesino@trtsp.jus.br>
Cc: Clailson <clailson.dba@gmail.com>
Cc: phillip@couto.in, "pgsql-performance@postgresql.org" <pgsql-performance@postgresql.org>
Subject: Re: Optimization inner join
Date: Thu, 19 Jan 2017 09:13:41 -0500
Message-ID: <16727.1484835221@sss.pgh.pa.us> (raw)
In-Reply-To: <5880BDEA.9050804@trtsp.jus.br>
References: <7ad1128e-7fdb-9976-823c-d4abdf554fa2@gmail.com>
<497e3581-bcce-4efc-b281-6b17a93479c4@mtasv.net>
<2cd4d430-54ad-4ab8-baae-b597c4a18c43@gmail.com>
<5880BDEA.9050804@trtsp.jus.br>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-performance>
Gustavo Rezende Montesino <gustavo.montesino@trtsp.jus.br> writes:
> Being the client in question, I would like to make a little remark: What
> we thought could be optimized here at first is on the row estimate of
> the index scan; which could take null_frac into account. To put things
> into perspective, our similar case in production has a table with 6
> million lines where only 9.5k aren“t null for the join field, an the
> over-estimation is throwing away good plans (like ~150ms execution time)
> in favor of pretty bad ones (~80s execution time).
Please provide a concrete test case for that. AFAIK the null fraction
should be accounted for in join size estimates. Here's a little test
case showing that it is:
regression=# create table t1 as select generate_series(1,1000000) as f1;
SELECT 1000000
regression=# analyze t1;
ANALYZE
regression=# create table t2 as select generate_series(1,1000000) as f1;
SELECT 1000000
regression=# analyze t2;
ANALYZE
regression=# explain select * from t1,t2 where t1.f1=t2.f1;
QUERY PLAN
------------------------------------------------------------------------
Hash Join (cost=30832.00..70728.00 rows=1000000 width=8)
Hash Cond: (t1.f1 = t2.f1)
-> Seq Scan on t1 (cost=0.00..14425.00 rows=1000000 width=4)
-> Hash (cost=14425.00..14425.00 rows=1000000 width=4)
-> Seq Scan on t2 (cost=0.00..14425.00 rows=1000000 width=4)
(5 rows)
regression=# insert into t2 select null from generate_series(1,1000000);
INSERT 0 1000000
regression=# analyze t2;
ANALYZE
regression=# explain select * from t1,t2 where t1.f1=t2.f1;
QUERY PLAN
------------------------------------------------------------------------
Hash Join (cost=30832.00..95727.00 rows=1000000 width=8)
Hash Cond: (t2.f1 = t1.f1)
-> Seq Scan on t2 (cost=0.00..27862.00 rows=2000000 width=4)
-> Hash (cost=14425.00..14425.00 rows=1000000 width=4)
-> Seq Scan on t1 (cost=0.00..14425.00 rows=1000000 width=4)
(5 rows)
The join size estimate is still correct even though it knows there are
many more rows in t2.
As for inserting a not-null test at the scan level, I'm not exactly
convinced that it's a win:
regression=# \timing
Timing is on.
regression=# select count(*) from t1,t2 where t1.f1=t2.f1;
count
---------
1000000
(1 row)
Time: 562.914 ms
regression=# select count(*) from t1,t2 where t1.f1=t2.f1 and t2.f1 is not null;
count
---------
1000000
(1 row)
Time: 564.896 ms
[ ftr, these times are best-of-three-trials ]
It's possible that in the case where an explicit sort has to be inserted,
reducing the amount of data passing through the sort would be worth doing;
but in the general case that's unproven.
regards, tom lane
--
Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-performance
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 using the --to and --cc options:
reply via email
To: pgsql-performance@postgresql.org
Cc: tgl@sss.pgh.pa.us, gustavo.montesino@trtsp.jus.br, clailson.dba@gmail.com
Subject: Re: Optimization inner join
In-Reply-To: <16727.1484835221@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