271_DBA_执行计划_id_table_select_type
DBA_Explain执行计划详解
格式:Explain + SQL
列名 |
描述 |
Id |
查询中, 每个select关键字对应一个唯一ID |
Select Type |
Select 关键字对应的查询类型 |
Table |
表名 |
partitions |
匹配的分区信息 |
Type |
针对单表的访问方法 |
Possible_keys |
可能用到的索引 |
Key |
实际使用的索引 |
Key_len |
实际使用的索引长度 |
Ref |
当使用索引列等值查询时, 与索引列进行等值匹配的对象信息 |
Rows |
预估需要读取的记录条数 |
Filtered |
针对预估需要读取的记录, 经过搜索条件过滤后剩余记录条数的百分比 |
Extra |
一些额外的信息 |
Table
无论查询多么复杂,包括多个表,最终的执行都是针对单个表,每个执行计划对应着对这个单表的执行计划
Id
查询都是 select 开头, 有几个 select 就有几个 id ,
连接查询只有一个select , 即使 from 后面跟N个表,也只会有一个ID,出现在前面的就是驱动表, 后面为被驱动表
explain select * from s1 INNER JOIN s2
嵌套查询会有多个select,就会有多个ID (如果子查询被优化器转成了semi join 那么仅有一个id)
explain select * from s1 where commen_field in (select s2.commen_field from s2 ) or key3="a"
Select type
大查询被转换为小查询, 每个select关键字代表一个小的查询,且被定义了一个select_type属性
名称 |
描述 |
备注 |
Simple |
Simple select (Not using UNION or subqueries) |
不包含union或子查询的select |
Primary |
Outermost Select
|
对于包含union 或者子查询的大查询,它由几个小查询主从,最左边查询就是primary |
Union |
Second or later Select statement in a UNION |
除了最左边的查询, 其余小查询就是 union |
Union Result |
Result of UNION |
|
SUBQUERY |
First SELECT IN Subquery |
|
DEPENDENT SUBQUERY |
First SELECT in Subquery, dependent on Outer query |
|
DEPENDENT UNION |
Second or later select statement in UNION, dependent on outer query |
|
DERIVED |
Derived table |
|
MATERIALIZED |
Materialized subquery |
|
Uncacheable subquery |
A subquery for which the result cannot be cached and must be re-evaluated for each row of outer query |
|
Uncacheable union |
The second or later select in a union that belongs to an unacheable subquery |
|
1 primary union Union result
对于包含union 或者子查询的大查询,它由几个小查询主从,最左边查询就是primary ,其余小查询就是 union,
Primary Union s1就是 primary, s2 就是union
Union result : MySQL 选择使用临时表来完成union查询的去重工作, 针对临时表去重查询 就是 union result
Explain select * from s1 union select * from s2;
2 Subquery
如果子查询不能转为semi join ,且子查询为不相关子查询且优化器决定将子查询物化来执行查询 就是 subquery
注意:由于被物化,所以subquery 只需要执行一遍
explain select * from s1 where key1 in (select s2.key1 from s2 ) or key3="a"
3 Dependent subquery
不能转为semi join 且被转为相关子查询,则子查询第一个select 就是 dependent subquery
注意: dependent subquery 子查询可能会执行多次
explain select * from s1 where key1 in (select s2.key1 from s2 where s1.key2= s2.key2 ) or key3="a"
4 Dependent union
包含union 和 union all 的大查询中, 各个小查询要依赖外层查询,除了做左侧小子查询外 其余的小查询就是 dependent union
explain select * from s1 where key1 in (select s2.key1 from s2 where key1 = "a" union select key1 from s1 WHERE key1 ="b" )
子查询包括 union, select s2.key1 from s2 where key1 = "a" 是第一个子查询 所以 type 为dependent subquery
其余子查询类型: select key1 from s1 WHERE key1 ="b" 类型 是 Dependent union
5 DERIVED
在包含派生表的查询中, 如果以物化派生表方式进行查询,派生表的select_type 就是 DERIVED
explain select * from (select key1, count(*) as c from s1 GROUP BY key1 ) as drrived_s1 where c > 1
Id为2的记录代表子查询的执行方式是 DERIVED , 代表物化, id 为1 代表外层查询
6 MATERIALIZED
当查询优化器执行子查询时, 选择子查询物化后与外层查询进行连接查询,对应子查询就是MATERIALIZED
EXPLAIN select * from s1 where key1 in (select key1 from s2 )
Id为2 其实是 select key1 from s2 结果集物化, 外层select 与结果集进行连接查询
- 点赞
- 收藏
- 关注作者
评论(0)