JOIN по двум коррелирующим полям в Postgres
Всем доброго!
Ниже прикладываю код с примером. Есть ли способ заставить оптимизатор postgres не перемножать селективность двух условий в Join-е?
Попробовал create statistics, но видимо в Join-е это не работает.
drop table if exists test_data_1;
create table test_data_1 as
select g.rn as id
, g.rn as second_id
from generate_series ( 1, 10) g ( rn);
drop table if exists test_data_2;
create table test_data_2 as
select g.rn as id
, g.rn as second_id
from generate_series ( 1, 10) g ( rn);
set default_statistics_target = 10000;
analyze test_data_1;
analyze test_data_2;
drop statistics if exists s1;
create statistics s1 ( dependencies) on id, second_id from test_data_1;
drop statistics if exists s2;
create statistics s2 ( dependencies) on id, second_id from test_data_2;
set default_statistics_target = 10000;
analyze test_data_1;
analyze test_data_2;
explain
select *
from test_data_1 t1
join test_data_2 t2
on t2.id = t1.id
and t2.second_id = t1.second_id
Источник: Stack Overflow на русском