Mysql join 的本质,Nested Loop Join 和 Hash Join

本文介绍了MySQL中JOIN操作的两种主要实现方式:Nested Loop Join(NLJ)和Hash Join。NLJ包括简单NLJ和Block Nested-Loop Join(BNL),其中BNL通过缓冲行来减少内部循环的读取次数。从MySQL 8.0.18开始,引入了Hash Join,对于等值连接条件且不使用索引的查询,其速度通常快于NLJ,并且可以通过join_buffer_size变量控制内存使用。当内存不足时,MySQL会使用磁盘文件处理。
摘要由CSDN通过智能技术生成

Mysql 在不同版本对join的实现有不同的实现方式,mysql5.7之前,是通过Nested Loop join方式实现的,在mysql 8以后对这种嵌入循环查询的方式采用hash join的算法进行了优化。
注:如下引用内容均摘抄与Mysql官网

Nested Loop Join

MySQL executes joins between tables using a nested-loop algorithm or variations on it.

MySQL使用嵌套循环算法或它的变体来执行表之间的连接。
有如下两种算法:

  • Nested-Loop Join Algorithm

    A simple nested-loop join (NLJ) algorithm reads rows from the first table in a loop one at a time, passing each row to a nested loop that processes the next table in the join. This process is repeated as many times as there remain tables to be joined.
    一个简单的嵌套循环联接(NLJ)算法一次从循环中的第一个表读取一行,将每一行传递到一个嵌套循环,该循环处理联接中的下一个表。只要还有需要连接的表,这个过程就会重复多次。

    如果使用简单的NLJ算法,连接处理如下:

    for each row in t1 matching range {
         
    	  for each row in t2 matching reference key {
         
    	    for each row in t3 {
         
    	      if row satisfies join conditions, send to client
    	    }
      }
    }
    

    因为NLJ算法一次只将一行从外部循环传递到内部循环,所以它通常读取在内部循环中处理的表多次。
    分析 t1 join t2 :

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值