MySQL8 哈希连接 Hash Join
    MySQL 开发组于 2019 年 10 月 14 日 正式发布了 MySQL 8.0.18 GA 版本,带来了一些新特性和增强功能。其中最引人注目的莫过于多表连接查询支持 hash join 方式了。先来看看官方的描述:

    https://dev.mysql.com/doc/refman/8.0/en/hash-joins.html

    MySQL 实现了用于内连接查询的 hash join 方式。例如,从 MySQL 8.0.18 开始以下查询可以使用 hash join 进行连接查询:

    1. SELECT *
    2. FROM t1
    3. JOIN t2
    4. ON t1.c1=t2.c1;

    Hash join 不需要索引的支持。大多数情况下,hash join 比之前的 Block Nested-Loop 算法在没有索引时的等值连接更加高效。
    使用以下语句创建三张测试表:

    1. CREATE TABLE t1 (c1 INT, c2 INT);
    2. CREATE TABLE t2 (c1 INT, c2 INT);
    3. CREATE TABLE t3 (c1 INT, c2 INT);

    使用EXPLAIN FORMAT=TREE命令可以看到执行计划中的 hash join,例如:

    1. mysql> EXPLAIN FORMAT=TREE
    2. -> SELECT *
    3. -> FROM t1
    4. -> JOIN t2
    5. -> ON t1.c1=t2.c1\G
    6. *************************** 1. row ***************************
    7. EXPLAIN: -> Inner hash join (t2.c1 = t1.c1) (cost=0.70 rows=1)
    8. -> Table scan on t2 (cost=0.35 rows=1)
    9. -> Hash
    10. -> Table scan on t1 (cost=0.35 rows=1)

    必须使用 EXPLAIN 命令的 FORMAT=TREE 选项才能看到节点中的 hash join。另外,EXPLAIN ANALYZE命令也可以显示 hash join 的使用信息。这也是该版本新增的一个功能。
    多个表之间使用等值连接的的查询也会进行这种优化。例如以下查询:

    1. SELECT *
    2. FROM t1
    3. JOIN t2
    4. ON (t1.c1 = t2.c1 AND t1.c2 < t2.c2)
    5. JOIN t3
    6. ON (t2.c1 = t3.c1);

    在以上示例中,任何其他非等值连接的条件将会在连接操作之后作为过滤器使用。可以通过EXPLAIN FORMAT=TREE命令的输出进行查看:

    1. mysql> EXPLAIN FORMAT=TREE
    2. -> SELECT *
    3. -> FROM t1
    4. -> JOIN t2
    5. -> ON (t1.c1 = t2.c1 AND t1.c2 < t2.c2)
    6. -> JOIN t3
    7. -> ON (t2.c1 = t3.c1)\G
    8. *************************** 1. row ***************************
    9. EXPLAIN: -> Inner hash join (t3.c1 = t1.c1) (cost=1.05 rows=1)
    10. -> Table scan on t3 (cost=0.35 rows=1)
    11. -> Hash
    12. -> Filter: (t1.c2 < t2.c2) (cost=0.70 rows=1)
    13. -> Inner hash join (t2.c1 = t1.c1) (cost=0.70 rows=1)
    14. -> Table scan on t2 (cost=0.35 rows=1)
    15. -> Hash
    16. -> Table scan on t1 (cost=0.35 rows=1)

    从以上输出同样可以看出,包含多个等值连接条件的查询也可以(会)使用多个 hash join 连接。
    但是,如果任何连接语句(ON)中没有使用等值连接条件,将不会采用 hash join 连接方式。例如:

    1. mysql> EXPLAIN FORMAT=TREE
    2. -> SELECT *
    3. -> FROM t1
    4. -> JOIN t2
    5. -> ON (t1.c1 = t2.c1)
    6. -> JOIN t3
    7. -> ON (t2.c1 < t3.c1)\G
    8. *************************** 1. row ***************************
    9. EXPLAIN: <not executable by iterator executor>

    此时,将会采用性能更慢的 block nested loop 连接算法。这与 MySQL 8.0.18 之前版本中没有索引时的情况一样:

    1. mysql> EXPLAIN
    2. -> SELECT *
    3. -> FROM t1
    4. -> JOIN t2
    5. -> ON (t1.c1 = t2.c1)
    6. -> JOIN t3
    7. -> ON (t2.c1 < t3.c1)\G
    8. *************************** 1. row ***************************
    9. id: 1
    10. select_type: SIMPLE
    11. table: t1
    12. partitions: NULL
    13. type: ALL
    14. possible_keys: NULL
    15. key: NULL
    16. key_len: NULL
    17. ref: NULL
    18. rows: 1
    19. filtered: 100.00
    20. Extra: NULL
    21. *************************** 2. row ***************************
    22. id: 1
    23. select_type: SIMPLE
    24. table: t2
    25. partitions: NULL
    26. type: ALL
    27. possible_keys: NULL
    28. key: NULL
    29. key_len: NULL
    30. ref: NULL
    31. rows: 1
    32. filtered: 100.00
    33. Extra: Using where; Using join buffer (Block Nested Loop)
    34. *************************** 3. row ***************************
    35. id: 1
    36. select_type: SIMPLE
    37. table: t3
    38. partitions: NULL
    39. type: ALL
    40. possible_keys: NULL
    41. key: NULL
    42. key_len: NULL
    43. ref: NULL
    44. rows: 1
    45. filtered: 100.00
    46. Extra: Using where; Using join buffer (Block Nested Loop)

    Hash join 连接同样适用于不指定查询条件时的笛卡尔积(Cartesian product),例如:

    1. mysql> EXPLAIN FORMAT=TREE
    2. -> SELECT *
    3. -> FROM t1
    4. -> JOIN t2
    5. -> WHERE t1.c2 > 50\G
    6. *************************** 1. row ***************************
    7. EXPLAIN: -> Inner hash join (cost=0.70 rows=1)
    8. -> Table scan on t2 (cost=0.35 rows=1)
    9. -> Hash
    10. -> Filter: (t1.c2 > 50) (cost=0.35 rows=1)
    11. -> Table scan on t1 (cost=0.35 rows=1)

    默认配置时,MySQL 所有可能的情况下都会使用 hash join。同时提供了两种控制是否使用 hash join 的方法:

    • 在全局或者会话级别设置服务器系统变量 optimizer_switch 中的 hash_join=on 或者 hash_join=off 选项。默认为 hash_join=on。
    • 在语句级别为特定的连接指定优化器提示 HASH_JOIN 或者 NO_HASH_JOIN。

    可以通过系统变量 join_buffer_size 控制 hash join 允许使用的内存数量;hash join 不会使用超过该变量设置的内存数量。如果 hash join 所需的内存超过该阈值,MySQL 将会在磁盘中执行操作。
    需要注意的是,如果 hash join 无法在内存中完成,并且打开的文件数量超过系统变量 open_files_limit 的值,连接操作可能会失败。为了解决这个问题,可以使用以下方法之一:

    • 增加 join_buffer_size 的值,确保 hash join 可以在内存中完成。
    • 增加 open_files_limit 的值。

    接下来比较一下 hash join 和 block nested loop 的性能,首先分别为 t1、t2 和 t3 生成 1000000 条记录:

    1. set join_buffer_size=2097152000;
    2. SET @@cte_max_recursion_depth = 99999999;
    3. INSERT INTO t1
    4. -- INSERT INTO t2
    5. -- INSERT INTO t3
    6. WITH RECURSIVE t AS (
    7. SELECT 1 AS c1, 1 AS c2
    8. UNION ALL
    9. SELECT t.c1 + 1, t.c1 * 2
    10. FROM t
    11. WHERE t.c1 < 1000000
    12. )
    13. SELECT *
    14. FROM t;

    没有索引情况下的 hash join:

    1. mysql> EXPLAIN ANALYZE
    2. -> SELECT COUNT(*)
    3. -> FROM t1
    4. -> JOIN t2
    5. -> ON (t1.c1 = t2.c1)
    6. -> JOIN t3
    7. -> ON (t2.c1 = t3.c1)\G
    8. *************************** 1. row ***************************
    9. EXPLAIN: -> Aggregate: count(0) (actual time=22993.098..22993.099 rows=1 loops=1)
    10. -> Inner hash join (t3.c1 = t1.c1) (cost=9952535443663536.00 rows=9952435908880402) (actual time=14489.176..21737.032 rows=1000000 loops=1)
    11. -> Table scan on t3 (cost=0.00 rows=998412) (actual time=0.103..3973.892 rows=1000000 loops=1)
    12. -> Hash
    13. -> Inner hash join (t2.c1 = t1.c1) (cost=99682753413.67 rows=99682653660) (actual time=5663.592..12236.984 rows=1000000 loops=1)
    14. -> Table scan on t2 (cost=0.01 rows=998412) (actual time=0.067..3364.105 rows=1000000 loops=1)
    15. -> Hash
    16. -> Table scan on t1 (cost=100539.40 rows=998412) (actual time=0.133..3395.799 rows=1000000 loops=1)
    17. 1 row in set (23.22 sec)
    18. mysql> SELECT COUNT(*)
    19. -> FROM t1
    20. -> JOIN t2
    21. -> ON (t1.c1 = t2.c1)
    22. -> JOIN t3
    23. -> ON (t2.c1 = t3.c1);
    24. +----------+
    25. | COUNT(*) |
    26. +----------+
    27. | 1000000 |
    28. +----------+
    29. 1 row in set (12.98 sec)

    实际运行花费了 12.98 秒。这个时候如果使用 block nested loop:

    1. mysql> EXPLAIN FORMAT=TREE
    2. -> SELECT /*+ NO_HASH_JOIN(t1, t2, t3) */ COUNT(*)
    3. -> FROM t1
    4. -> JOIN t2
    5. -> ON (t1.c1 = t2.c1)
    6. -> JOIN t3
    7. -> ON (t2.c1 = t3.c1)\G
    8. *************************** 1. row ***************************
    9. EXPLAIN: <not executable by iterator executor>
    10. 1 row in set (0.00 sec)
    11. SELECT /*+ NO_HASH_JOIN(t1, t2, t3) */ COUNT(*)
    12. FROM t1
    13. JOIN t2
    14. ON (t1.c1 = t2.c1)
    15. JOIN t3
    16. ON (t2.c1 = t3.c1);

    EXPLAIN 显示无法使用 hash join。查询跑了几十分钟也没有出结果,其中一个 CPU 使用率到了 100%;因为一直在执行嵌套循环(1000000 的 3 次方)。
    再看有索引时的 block nested loop 方法,增加索引:

    1. mysql> CREATE index idx1 ON t1(c1);
    2. Query OK, 0 rows affected (7.39 sec)
    3. Records: 0 Duplicates: 0 Warnings: 0
    4. mysql> CREATE index idx2 ON t2(c1);
    5. Query OK, 0 rows affected (6.77 sec)
    6. Records: 0 Duplicates: 0 Warnings: 0
    7. mysql> CREATE index idx3 ON t3(c1);
    8. Query OK, 0 rows affected (7.23 sec)
    9. Records: 0 Duplicates: 0 Warnings: 0

    查看执行计划并运行相同的查询语句:

    1. mysql> EXPLAIN ANALYZE
    2. -> SELECT COUNT(*)
    3. -> FROM t1
    4. -> JOIN t2
    5. -> ON (t1.c1 = t2.c1)
    6. -> JOIN t3
    7. -> ON (t2.c1 = t3.c1)\G
    8. *************************** 1. row ***************************
    9. EXPLAIN: -> Aggregate: count(0) (actual time=47684.034..47684.035 rows=1 loops=1)
    10. -> Nested loop inner join (cost=2295573.22 rows=998412) (actual time=0.116..46363.599 rows=1000000 loops=1)
    11. -> Nested loop inner join (cost=1198056.31 rows=998412) (actual time=0.087..25788.696 rows=1000000 loops=1)
    12. -> Filter: (t1.c1 is not null) (cost=100539.40 rows=998412) (actual time=0.050..5557.847 rows=1000000 loops=1)
    13. -> Index scan on t1 using idx1 (cost=100539.40 rows=998412) (actual time=0.043..3253.769 rows=1000000 loops=1)
    14. -> Index lookup on t2 using idx2 (c1=t1.c1) (cost=1.00 rows=1) (actual time=0.012..0.015 rows=1 loops=1000000)
    15. -> Index lookup on t3 using idx3 (c1=t1.c1) (cost=1.00 rows=1) (actual time=0.012..0.015 rows=1 loops=1000000)
    16. 1 row in set (47.68 sec)
    17. mysql> SELECT COUNT(*)
    18. -> FROM t1
    19. -> JOIN t2
    20. -> ON (t1.c1 = t2.c1)
    21. -> JOIN t3
    22. -> ON (t2.c1 = t3.c1);
    23. +----------+
    24. | COUNT(*) |
    25. +----------+
    26. | 1000000 |
    27. +----------+
    28. 1 row in set (19.56 sec)

    实际运行花费了 19.56 秒。所以在这个场景中的测试结果如下:

    Hash Join(无索引) Block Nested Loop(无索引) Block Nested Loop(有索引)
    12.98s 未返回 19.56s

    再增加一个 Oracle 12c 中无索引时 hash join 结果:1.282 s。
    再增加一个 PostgreSQL 11.5 中无索引时 hash join 结果:6.234 s。
    再增加一个 SQL 2017 中无索引时 hash join 结果:5.207 s。