数据库系统概念介绍了实现连接操作的几种方法。其中两个是合并连接和散列连接。
- 我想知道优化器何时决定使用合并连接并且
什么时候进行哈希连接?
-
特别是,从https://stackoverflow.com/a/1114288/156458 https://stackoverflow.com/a/1114288/156458
散列连接只能用于等连接,但合并连接更灵活。
但 Database System Concepts 表示两者仅用于等值连接
自然连接。
合并连接算法(也称为排序合并连接算法)
可用于计算自然连接和等连接。
...
与合并连接算法类似,哈希连接算法可用于
实现自然连接和等连接。
Thanks.
我的问题来自 PostgreSQL 文档,其中有两个示例,我不确定为什么一个使用合并连接,另一个使用散列连接:
EXPLAIN SELECT *
FROM tenk1 t1, tenk2 t2
WHERE t1.unique1 < 100 AND t1.unique2 = t2.unique2;
QUERY PLAN
------------------------------------------------------------------------------------------
Hash Join (cost=230.47..713.98 rows=101 width=488)
Hash Cond: (t2.unique2 = t1.unique2)
-> Seq Scan on tenk2 t2 (cost=0.00..445.00 rows=10000 width=244)
-> Hash (cost=229.20..229.20 rows=101 width=244)
-> Bitmap Heap Scan on tenk1 t1 (cost=5.07..229.20 rows=101 width=244)
Recheck Cond: (unique1 < 100)
-> Bitmap Index Scan on tenk1_unique1
(cost=0.00..5.04 rows=101 width=0)
Index Cond: (unique1 < 100)
and
EXPLAIN SELECT *
FROM tenk1 t1, onek t2
WHERE t1.unique1 < 100 AND t1.unique2 = t2.unique2;
QUERY PLAN
------------------------------------------------------------------------------------------
Merge Join (cost=198.11..268.19 rows=10 width=488)
Merge Cond: (t1.unique2 = t2.unique2)
-> Index Scan using tenk1_unique2 on tenk1 t1 (cost=0.29..656.28 rows=101 width=244)
Filter: (unique1 < 100)
-> Sort (cost=197.83..200.33 rows=1000 width=244)
Sort Key: t2.unique2
-> Seq Scan on onek t2 (cost=0.00..148.00 rows=1000 width=244)
None
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)