对PostgreSQL 的 hash join 的原理的学习
来源:互联网 发布:淘宝违规提醒怎么处理 编辑:程序博客网 时间:2024/06/10 07:08
开始
PostgreSQL 名人 momjian 的文章指出了其pseudo code:
for (j = 0; j < length(inner); j++) hash_key = hash(inner[j]); append(hash_store[hash_key], inner[j]);for (i = 0; i < length(outer); i++) hash_key = hash(outer[i]); for (j = 0; j < length(hash_store[hash_key]); j++) if (outer[i] == hash_store[hash_key][j]) output(outer[i], inner[j]);
为了看的更加清楚一点,加上自己的注释:
//利用 inner 表, 来构造 hash 表(放在内存里) for (j = 0; j < length(inner); j++) { hash_key = hash(inner[j]); append(hash_store[hash_key], inner[j]); } //对 outer 表的每一个元素, 进行遍历 for (i = 0; i < length(outer); i++) { //拿到 outer 表中的 某个元素, 进行 hash运算, 得到其 hash_key 值 hash_key = hash(outer[i]); //用上面刚得到的 hash_key值, 来 对 hash 表进行 探测(假定hash表中有此key 值) //采用 length (hash_store[hash_Key]) 是因为,hash算法构造完hash 表后,有可能出现一个key值处有多个元素的情况。 //例如: hash_key 100 ,对应 a,c, e; 而 hash_key 200 , 对应 d; hash_key 300, 对应 f; //也就是说, 如下的遍历,其实是对 拥有相同 的 (此处是上面刚运算的,特定的)hash_key 值的各个元素的遍历 for (j = 0; j < length(hash_store[hash_key]); j++) { //如果找到了匹配值,则输出一行结果 if (outer[i] == hash_store[hash_key][j]) output(outer[i], inner[j]); } }
[作者:技术者高健@博客园 mail: luckyjackgao@gmail.com ]
实践一下:
postgres=# \d employee Table "public.employee" Column | Type | Modifiers --------+-----------------------+----------- id | integer | name | character varying(20) | deptno | integer | age | integer | Indexes: "idx_id_dept" btree (id, deptno)postgres=# \d deptment Table "public.deptment" Column | Type | Modifiers ----------+-----------------------+----------- deptno | integer | deptname | character varying(20) | postgres=# postgres=# select count(*) from employee; count -------1000(1 row)postgres=# select count(*) from deptment; count -------102(1 row)postgres=#
执行计划:
postgres=# explain select a.name, b.deptname from employee a, deptment b where a.deptno=b.deptno; QUERY PLAN ------------------------------------------------------------------------- Hash Join (cost=3.29..34.05 rows=1000 width=14) Hash Cond: (a.deptno = b.deptno) -> Seq Scan on employee a (cost=0.00..17.00 rows=1000 width=10) -> Hash (cost=2.02..2.02 rows=102 width=12) -> Seq Scan on deptment b (cost=0.00..2.02 rows=102 width=12)(5 rows)postgres=#
[作者:技术者高健@博客园 mail: luckyjackgao@gmail.com ]
阅读全文
0 0
- 对PostgreSQL 的 hash join 的原理的学习
- 对Hash Join的一次优化
- 对oracle hash join trace的研究
- hash join (Oracle里的哈希连接原理)
- 对Hash Join的一次优化之--10104 trace-bucket
- postgreSQL 的 dynamic hash table
- 关于hash join的过程
- 详解Oracle的Hash Join
- hash join的基本机制
- hash join、merge join、nested loop的原理演示及性能对比
- hash算法的原理
- hash算法的原理
- hash原理的理解
- Oracle 的 hash join连接方式
- Oracle的Hash Join之探究整理
- 错误的选择了HASH JOIN!
- 浅谈merge join 与hash join的区别
- oracle的表连接hash join、nested loop join
- Monkey and Banana LIS
- 下载百度文库里面的PDF文档(只能下PDF文档)
- 一个注册页面的HTML实现
- python 全套教程视频 传智博客 百度网盘
- 权限管理系统。
- 对PostgreSQL 的 hash join 的原理的学习
- 手机wap前端开发经验
- 基于深度学习的CVaaS计算机视觉即服务案例(Computer Vision as a Service)
- Movie Studio裁切的使用技巧
- 基础数论算法(4) 中国剩余定理
- IntelliJ IDEA实用快捷键
- HBase设计之RowKey行键生成器工具(3)
- 大数据时代,如何构建国家地质基础数据更新体系
- 2017年8月9日提高组T2 覆盖