PostgreSQL从小白到专家,是从入门逐渐能力提升的一个系列教程,内容包括对PG基础的认知、包括安装使用、包括角色权限、包括维护管理、、等内容,希望对热爱PG、学习PG的同学们有帮助,欢迎持续关注CUUG PG技术大讲堂。 第30讲:多表连接方式 第30讲预告:9月23日(周六)19:30-20:30,钉钉群直播,群号:35822460 内容2 : Merge Join连接方式
多表连接方式 nested loop join hash join NATURAL INNER JOIN LEFT/RIGHT OUTER JOIN
Nested Loop Join
Nested Loop Join图解
testdb=# EXPLAIN SELECT * FROM tbl_a AS a, tbl_b AS b WHERE a.id = b.id; ----------------------------------------------------------------------- Join Filter: (a.id = b.id) -> Materialize (cost=0.00..98.00 rows=5000 width=8) (5 rows) Materialize成本估算 (Materialized) Nested Loop成本估算
QUERY PLAN Nested Loop (cost=0.29..1935.50 rows=5000 width=16) -> Index Scan using tbl_c_pkey on tbl_c c (cost=0.29..0.36 rows=1 width=8) (4 rows) 具有外部索引扫描的嵌套循环联接的三种变体 Merge Join连接方式
Merge Join成本估算 testdb=# EXPLAIN SELECT * FROM tbl_a AS a, tbl_b AS b WHERE a.id = b.id AND b.id < 1000; ------------------------------------------------------------------------- Merge Cond: (a.id = b.id) Sort Key: a.id -> Sort (cost=135.33..137.83 rows=1000 width=8) -> Seq Scan on tbl_b b (cost=0.00..85.50 rows=1000 width=8) (9 rows) Materialized Merge Join
testdb=# SET enable_hashjoin TO off; testdb=# EXPLAIN SELECT * FROM tbl_c AS c, tbl_b AS b WHERE c.id = b.id AND b.id < 1000; -------------------------------------------------------------------------------------- Merge Cond: (c.id = b.id) -> Sort (cost=135.33..137.83 rows=1000 width=8) -> Seq Scan on tbl_b b (cost=0.00..85.50 rows=1000 width=8) (7 rows) materialized merge join with outer index scan testdb=# SET enable_nestloop TO off; QUERY PLAN Merge Join (cost=421.84..672.09 rows=4500 width=16) -> Index Scan using tbl_c_pkey on tbl_c c (cost=0.29..318.29 rows=10000 width=8) -> Sort (cost=421.55..432.80 rows=4500 width=8) -> Seq Scan on tbl_b b (cost=0.00..85.50 rows=4500 width=8) (8 rows) indexed merge join with outer index scan testdb=# SET enable_nestloop TO off; QUERY PLAN Merge Join (cost=0.57..226.07 rows=1000 width=16) -> Index Scan using tbl_c_pkey on tbl_c c (cost=0.29..318.29 rows=10000 width=8) Index Cond: (id < 1000)
Hash Join In-Memory Hash Join 将内部表的所有元组插入到一个批处理中 将外部表的每个元组与批处理中的内部元组进行比较,如果满足连接条件,则进行连接 Hash Join
预处理 2、向上拉子查询 testdb=# SELECT * FROM tbl_a AS a, (SELECT * FROM tbl_b) as b WHERE a.id = b.id; 3、将外部联接转换为内部联接 优化器可用规则 1、表数量小于12张,应用动态规划得到最优的计划 参数 geqo_threshold指定的阈值(默认值为12)
SGetting the Cheapest Path of a Triple-Table Query testdb=# WHERE a.id = b.id AND b.id = c.id AND a.data < 40; {tbl_a,tbl_b,tbl_c}=min({tbl_a,{tbl_b,tbl_c}},{tbl_b,{tbl_a,tbl_c}},{tbl_c,{tbl_a,tbl_b}}).
以上就是第30讲的内容,往期视频及文档,联系CUUG
|