前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >MySQL Hash Join前世今生

MySQL Hash Join前世今生

作者头像
GreatSQL社区
发布2023-02-22 10:24:18
4490
发布2023-02-22 10:24:18
举报
文章被收录于专栏:GreatSQL出品技术文章

* GreatSQL社区原创内容未经授权不得随意使用,转载请联系小编并注明来源。

因工作需要,对MySQL Hash Join的内部实现做了一些探索和实践,对这个由8.0.18开始引入的连接算法有了一定的了解,写文总结与各位大佬分享,欢迎大家指教。因篇幅较长,这份总结分成若干部分,我们今天先一起来看一下MySQL Hash join的变迁史。

爬了一下 MySQL worklog[1],并结合源码及各版本的实际使用,个人认为比较重要的 worklogs 为如下几个, 其它的变更一般围绕这些 worklogs 做的小调整或 bugfixes ,这里我们就不一一列举。

1. WL#2241: Hash join (变更版本:8.0.18)

主要内容:

  • 新增执行类 HashJoinIterator ,实现 hash join 算法原型 (支持on-disk hash)
  • HASH JOIN 仅支持 INNER JOIN ,并对使用 hash join 做了限制:关联表连接条件必须至少包含一条等值条件(equi-join condition, 如t1.a = t2.a),且join条件中的列不包含索引

注:这里我认为官网的 Release Notes[2] 描述是不太准确的,以如下例子为例,虽然该查询包含了非等值连接条件(non-equi-join condition, 如 t1.a <> t2.a ,t1.a = 1, t2.a > 1 等),但实际查询中还是使用hash join的, 因为查询语句在解析执行过程中,可能会经历语句重写、sql优化等步骤,与表象上会有所不同,建议借助 explain 工具,查看实际上查询语句选择的 join 算法。

代码语言:javascript
复制
-- 版本:8.0.18
-- 在创建iterator时,t1.a > 1 会被当成表的过滤条件,而非inner join的join条件
-- 此查询仍使用了hash join(join 条件为空)
EXPLAIN FORMAT=tree SELECT * FROM t1 JOIN t2 ON t1.i > 1;
-> Inner hash join  (cost=1.17 rows=3)
    -> Table scan on t2  (cost=0.34 rows=2)
    -> Hash
        -> Filter: (t1.i > 1)  (cost=0.65 rows=1)
            -> Table scan on t1  (cost=0.65 rows=4)
  • (尽量)使用HASH JOIN算法替换BNL(Blocked Nested-Loop)连接算法

2. WL#13377: Add support for hash outer, anti and semi join( 变更版本:8.0.20)

主要内容:

  • HASH INNER JOIN改进 INNER JOIN中的non-equi-join conditions, 会被附为hash join的过滤条件:
代码语言:javascript
复制
-- 版本:8.0.20
EXPLAIN FORMAT=tree SELECT * FROM t1 JOIN t2 ON t1.i <> t2.i;
-> Filter: (t1.i <> t2.i)  (cost=1.10 rows=2)
    -> Inner hash join (no condition)  (cost=1.10 rows=2)
        -> Table scan on t2  (cost=0.18 rows=2)
        -> Hash
            -> Table scan on t1  (cost=0.45 rows=2)
  • HAH JOIN支持outer join/anti join/semi join
代码语言:javascript
复制
-- 版本:8.0.20
-- Left outer join
EXPLAIN FORMAT=tree SELECT * FROM t1 LEFT JOIN t2 ON t1.i=t2.i;
-> Left hash join (t2.i = t1.i)  (cost=0.88 rows=4)                                                                                                                                                                          
    -> Table scan on t1  (cost=0.45 rows=2)                                                                                                                                                                                    
    -> Hash                                                                                                                                                                                                                    
        -> Table scan on t2  (cost=0.23 rows=2)

-- Right outer join(注:MySQL在parser阶段会将所有的right join改写为left join
--                      所以我们这里看到的explain为Left hash join
EXPLAIN FORMAT=tree SELECT * FROM t1 RIGHT JOIN t2 ON t1.i=t2.i;
-> Left hash join (t1.i = t2.i)  (cost=0.88 rows=4)                                                                                                                                                                          
    -> Table scan on t2  (cost=0.45 rows=2)                                                                                                                                                                                    
    -> Hash                                                                                                                                                                                                                    
        -> Table scan on t1  (cost=0.23 rows=2)

-- Semijoin
EXPLAIN FORMAT=tree SELECT * FROM t1 WHERE (t1.i) IN (SELECT t2.i FROM t2);
-> Hash semijoin (t2.i = t1.i)  (cost=0.83 rows=2)
    -> Table scan on t1  (cost=0.45 rows=2)
    -> Hash
        -> Table scan on t2  (cost=0.18 rows=2)

-- Antijoin
EXPLAIN FORMAT=tree 
SELECT * FROM t2 WHERE NOT EXISTS (SELECT * FROM t1 WHERE t1.i = t2.i);
-> Hash antijoin (t1.i = t2.i)  (cost=1.10 rows=4)                                                                                                                                                                           
    -> Table scan on t2  (cost=0.45 rows=2)                                                                                                                                                                                    
    -> Hash                                                                                                                                                                                                                    
        -> Table scan on t1  (cost=0.45 rows=2)
  • 所有使用BNL的连接,都将被转为使用HASH JOIN
  • non-equi-join conditions 处理 Hash join iterator引入"extra" condition的概念,部分 non-equi-join conditions会被当成Hash join iteratorextra condition, 在建hash table时,join key的计算不依赖这些条件,但会在hash查找到匹配项后,作为附加的过滤条件:
代码语言:javascript
复制
-- 版本: 8.0.20
-- 注: t1.i > 1 会被放到hash join的 extra conditions中
EXPLAIN FORMAT=tree SELECT * FROM  t1 LEFT JOIN t2 ON t1.i=t2.i AND t1.i > 1;
-> Left hash join (t2.i = t1.i), extra conditions: (t1.i > 1)  (cost=0.88 rows=4)
    -> Table scan on t1  (cost=0.45 rows=2)
    -> Hash
        -> Table scan on t2  (cost=0.23 rows=2)

相关源码:

代码语言:javascript
复制
// 代码版本:8.0.20 HEAD: commit 91a17cedb1ee880fe7915fb14cfd74c04e8d6588
// 文件名:sql/hash_join_iterator.cc
int HashJoinIterator::ReadNextJoinedRowFromHashTable() {
  int res;
  bool passes_extra_conditions = false;
  do { // 所有匹配行都需要多做一个extra condition的判定,因为有可能存在不同行的记录
       // 映射在同一个join key的情况,因此需要通过遍历逐条读取出符合extra conditions
       // 的数据
    res = ReadJoinedRow();  // 读取通过join key查找已经得到的匹配行(单行记录)
    DBUG_ASSERT(res == 0 || res == -1);
    
    if (res == 0) {
      passes_extra_conditions = JoinedRowPassesExtraConditions();
      if (thd()->is_error()) {
        // Evaluation of extra conditions raised an error, so abort the join.
        return 1;
      }

      if (!passes_extra_conditions) {
        ++m_hash_map_iterator;
      }
    }
  } while (res == 0 && !passes_extra_conditions);
}

3. WL#13459: Optimize hash table in hash join (变更版本:8.0.23)

主要内容:

  • 优化hash join table的创建方法 这里MySQL所说的“优化”, 实际上会更激进一点,这个版本中,MySQL直接使用了一个基于 robin hood hashing[3] 实现的 开源hash table[4] ,更换了原先的hash join table实现( from mem_root_unordered_multimap to robin_hood::unordered_flat_map)
  • 优化内存管理和使用,降低了 on-disk hash 的频率,提高了内存有效使用率等(其他的改进内容及提升的效果可以参考MySQL 8.0.23的release notes[5] 以及 worklog #13459[6] 的Low Level Design页面)

本篇我们对MySQL hash join的3个重要变更做了简要的总结,算是对它的前世今生有了印象,谢谢各位阅读;之后让我们会结合具体的sql查询样例,去跟踪一下对应的代码执行流程,不日更新,敬请期待。

参考文档

[1] MySQL worklog: https://dev.mysql.com/worklog/ [2] MySQL 8.0.18 release notes#optimizer: https://dev.mysql.com/doc/relnotes/mysql/8.0/en/news-8-0-18.html#mysqld-8-0-18-optimizer [3] robin hood hashing 算法介绍: https://programming.guide/robin-hood-hashing.html [4] robin hood hasing 开源实现: https://github.com/martinus/robin-hood-hashing [5] MySQL 8.0.23 release notes#optimizer: https://dev.mysql.com/doc/relnotes/mysql/8.0/en/news-8-0-23.html#mysqld-8-0-23-optimizer [6] MySQL worklog #13459: https://dev.mysql.com/worklog/task/?id=13459

Enjoy GreatSQL :)


《零基础学习MySQL》视频课程

戳此小程序即可直达B站

https://www.bilibili.com/video/BV1Da411W7Va?spm_id_from=333.999.0.0&vd_source=ae1951b64ea7b9e6ba11f1d0bbcff0e4


文章推荐:


关于 GreatSQL

GreatSQL是由万里数据库维护的MySQL分支,专注于提升MGR可靠性及性能,支持InnoDB并行查询特性,是适用于金融级应用的MySQL分支版本。

GreatSQL社区官网: https://greatsql.cn/

Gitee: https://gitee.com/GreatSQL/GreatSQL

GitHub: https://github.com/GreatSQL/GreatSQL

Bilibili:

https://space.bilibili.com/1363850082/video

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-11-21,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 GreatSQL社区 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. WL#2241: Hash join (变更版本:8.0.18)
  • 2. WL#13377: Add support for hash outer, anti and semi join( 变更版本:8.0.20)
  • 3. WL#13459: Optimize hash table in hash join (变更版本:8.0.23)
  • 参考文档
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档