匿名用户pg中的merge join的条件,比如等值还是非等值
像这样
[postgres@halo-centos-8-release ~]$ psql
psql (17beta1)
Type "help" for help.
postgres=# create temp table tt4x(c1 int, c2 int, c3 int);
CREATE TABLE
postgres=# explain (costs off)
postgres-# select * from tt4x t1
postgres-# where not exists (
postgres(# select 1 from tt4x t2
postgres(# left join tt4x t3 on t2.c3 = t3.c1
postgres(# left join ( select t5.c1 as c1
postgres(# from tt4x t4 left join tt4x t5 on t4.c2 = t5.c1
postgres(# ) a1 on t3.c2 = a1.c1
postgres(# where t1.c1 = t2.c2
postgres(# );
QUERY PLAN
---------------------------------------------------------
Hash Anti Join
Hash Cond: (t1.c1 = t2.c2)
-> Seq Scan on tt4x t1
-> Hash
-> Merge Right Join
Merge Cond: (t5.c1 = t3.c2)
-> Merge Join
Merge Cond: (t4.c2 = t5.c1)
-> Sort
Sort Key: t4.c2
-> Seq Scan on tt4x t4
-> Sort
Sort Key: t5.c1
-> Seq Scan on tt4x t5
-> Sort
Sort Key: t3.c2
-> Merge Left Join
Merge Cond: (t2.c3 = t3.c1)
-> Sort
Sort Key: t2.c3
-> Seq Scan on tt4x t2
-> Sort
Sort Key: t3.c1
-> Seq Scan on tt4x t3
(24 rows)
postgres=#
评论
有用 1PostgreSQL 中的 MERGE JOIN 是一种联接类型,通常用于执行 JOIN 查询。MERGE JOIN 在实现上是基于排序的合并算法,它要求联接的两边输入数据都是已经排序过的。
在 PostgreSQL 中,MERGE JOIN 可以是等值联接,也可以是非等值联接。等值联接是指使用等号(=)来比较联接条件中的两个关系的属性。非等值联接是指使用其他比较运算符(如>、<、>=、<=)来比较联接条件中的两个关系的属性。
在执行计划中,如果你看到 MERGE JOIN 并且联接条件是等值联接,那么联接条件就是等值的。如果联接条件包含其他比较运算符,那么联接就是非等值的。
以下是一个使用 MERGE JOIN 的 SQL 查询示例,这里展示的是等值联接:
SELECT a.column1, b.column2
FROM table_a a
JOIN table_b b ON a.join_key = b.join_key;
在这个例子中,table_a 和 table_b 是通过 join_key 这个字段进行等值联接的。
非等值联接的例子如下:
SELECT a.column1, b.column2
FROM table_a a
JOIN table_b b ON a.join_key > b.join_key;
在这个例子中,table_a 和 table_b 是通过 join_key 字段进行非等值联接的,联接条件是寻找 table_a 中 join_key 大于 table_b 中 join_key 的记录。
评论
有用 1pg三种表连接nested loop,hash join,merge join,只有hash join因为涉及hash值匹配,所以只有hash join要求必须是等值连接。
评论
有用 2上面答案均不正确,oracle支持非等值,但是pg不支持非等值。
lightdb@postgres=# set enable_nestloop = on;
SET
lightdb@postgres=# explain select /*+ use_merge(t,t2) */* from t join t2 on t.id=t2.id;
QUERY PLAN
--------------------------------------------------------------------------
Merge Join (cost=359.57..860.00 rows=32512 width=8)
Merge Cond: (t.id = t2.id)
-> Sort (cost=179.78..186.16 rows=2550 width=4)
Sort Key: t.id
-> Seq Scan on t @"lt#0" (cost=0.00..35.50 rows=2550 width=4)
-> Sort (cost=179.78..186.16 rows=2550 width=4)
Sort Key: t2.id
-> Seq Scan on t2 @"lt#0" (cost=0.00..35.50 rows=2550 width=4)
(8 rows)
lightdb@postgres=# explain select /*+ use_merge(t,t2) */* from t join t2 on t.id>t2.id;
QUERY PLAN
--------------------------------------------------------------------------
Nested Loop (cost=10000000000.00..10000097614.88 rows=2167500 width=8)
Join Filter: (t.id > t2.id)
-> Seq Scan on t @"lt#0" (cost=0.00..35.50 rows=2550 width=4)
-> Materialize (cost=0.00..48.25 rows=2550 width=4)
-> Seq Scan on t2 @"lt#0" (cost=0.00..35.50 rows=2550 width=4)
(5 rows)
lightdb@postgres=# set enable_nestloop = off
lightdb@postgres-# ;
SET
lightdb@postgres=# explain select /*+ use_merge(t,t2) */* from t join t2 on t.id>t2.id;
QUERY PLAN
--------------------------------------------------------------------------
Nested Loop (cost=10000000000.00..10000097614.88 rows=2167500 width=8)
Join Filter: (t.id > t2.id)
-> Seq Scan on t @"lt#0" (cost=0.00..35.50 rows=2550 width=4)
-> Materialize (cost=0.00..48.25 rows=2550 width=4)
-> Seq Scan on t2 @"lt#0" (cost=0.00..35.50 rows=2550 width=4)
(5 rows)
评论
有用 0
墨值悬赏

