当前位置:Gxlcms > 数据库问题 > 深入Mysql

深入Mysql

时间:2021-07-01 10:21:17 帮助过:4人阅读

 

8.2.1.7 Nested-Loop Join Algorithms 嵌套循环连接算法

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

  • Nested-Loop Join Algorithm

  • Block Nested-Loop Join Algorithm

 

简单的NLJ算法

读取行,在一个循环内,从第一个表每次读一行x,并传递x到内部嵌套的循环(被join的下一个表格的处理)。

这个过程被重复多次,每次处理一个被join的表格。

??本质就是嵌套循环,一个表一个循环。层层嵌套。

例子:3个表关联。所以嵌套3层。

Table   Join Type
t1      range
t2      ref
t3      ALL

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
    }
  }
}

问题? Join Type类型不理解

https://dev.mysql.com/doc/refman/8.0/en/explain-output.html#jointype_all

 

Block Nested-Loop Join Algorithm

优化的算法。

 


 

 

8.8.2 EXPLAIN Output Format

explain声明提供了MYsql如何执行一个声明的详细信息。

比如select声明,explain会返回:每个表一行信息。

Mysql使用nested-loop join方法来处理所有的join连接。因此,mysql会从第一个表读一行数据,然后在第二个表找到一个匹配的行数据,在然后去往第三个表。。。当所有的表都被处理,Mysql会输出被选择的列,然后返回到上一个表,寻找更多的匹配行。

??上面的过程,就是嵌套循环处理的过程。

  • EXPLAIN Output Columns

  • EXPLAIN Join Types

  • EXPLAIN Extra Information

  • EXPLAIN Output Interpretation

 

explain 输出列的解释:

下面的图,显示了explain声明中常用的输出列的信息:

  • id,      查询中select声明的序列号。
  • select_type: select类型
  • estimate:    被检测的row的数量估值。
  • filtered:       Percentage of rows filtered by table condition, 通过筛选条件,被检索的行的百分比。
  • extra:      额外的信息。

技术图片

 

 

explain用JSON格式来描述:

  • id列,在json中是select_id属性

技术图片

 

 

每一个输出列都提供了一个表格的信息。简单的说明见:Table 8.1, “EXPLAIN Output Columns”

详细解释

select_type:

  • simple:         没有使用UNION或子查询
  • primary:   Outermost SELECT
  • UNION:    在一个UNION内的第2个及之后的select声明
  • Union result: 一个UNION的结果
  • subquery:  子查询中的第一个select
  • derived:   衍生表
    • dependent derived:  衍生表,依赖于另一个表
  • 其他:具体见https://dev.mysql.com/doc/refman/8.0/en/explain-output.html#explain-output-column-table

 

key: 

key/index,Mysql实际决定使用的。

ref:

尚未理解。

rows:

Mysql估计执行查询时会被检测的行的数量。使用InnoDB引擎,这是一个估计值。

filtered:

由表的筛选条件而被检索的row的数量占表格所有行的比例。

最大值是100,表示没有进行筛选。

 

EXPLAIN Join Types

深入Mysql

标签:信息   sql   读取   方法   tis   tle   优化   height   result   

人气教程排行