在MySQL 8.0.18之前,表的join方式只有嵌套循环(nested loop)这一种方式;
8.0.18推出了hash join的方式以替代嵌套循环。
hash join的原理概括为:
选择占用空间较小的表t1(不一定是行数)作为驱动表,计算其join字段的hash值,在内存中build一个hash table,将t1的join字段的hash值存放至hash table。然后对被驱动表t2的join字段计算hash值,并与内存中的hash table进行查找匹配。
使hash join方式生效的前提是用于join的字段上没有索引
且在8.0.18中,还需要一个对等的条件(table1.a=table2.a),才能满足hash join。
原文如下:
Beginning with MySQL 8.0.18, MySQL employs a hash join for any query for which each join has an equi-join condition, and in which there are no indexes that can be applied to any join conditions。
当有一个或多个索引可用于单表谓词时,也可以使用散列连接。
A hash join can also be used when there are one or more indexes that can be used for single-table predicates.
在8.0.20中,取消了对等条件的约束,可以全面支持non-equi-join,Semijoin,Antijoin,Left outer join/Right outer join。
原文转载:理论学习 | MySQL 8.0.18 +的hash join学习_db_murphy的博客-优快云博客