提示:文章先作为初版,等后续时间充足后,补充更深的内容
MySql执行计划
一、简介
在企业的应用场景中,为了知道优化SQL语句的执行,需要查看SQL语句的具体执行过程,以加快SQL语句的执行效率。
可以使用explain+SQL语句来模拟优化器执行SQL查询语句,从而知道mysql是如何处理sql语句的。官网地址: 官方地址网站
二、执行计划包含信息
Column | Meaning |
---|---|
id | The SELECT identifier |
select_type | The SELECT type 【查询类型】 |
table | The table for the output row【表名】 |
partitions | The matching partitions 【匹配的分区信息】 |
type | The join type【针对但表的查询方式】 |
possible_keys | The possible indexes to choose【可能用到的索引】 |
key | The index actually chosen【实际上使用的索引】 |
key_len | The length of the chosen key【实际使用到的索引长度】 |
ref | The columns compared to the index【当使用索引列等值查询时,与索引列进行等值匹配的对象信息】 |
rows | Estimate of rows to be examined【预估的需要读取的记录条数】 |
filtered | Percentage of rows filtered by table condition【某个表经过搜索条件过滤后剩余记录条数的百分比】 |
extra | Additional information【一些额外的信息,排序等】 |
id
select查询的序列号,包含一组数字,表示查询中执行select子句或者操作表的顺序id号分为三种情况:
1、如果id相同,那么执行顺序从上到下
explain select * from emp e join dept d on e.deptno = d.deptno join sa1grade sg on e.sa1between sg.losal and sg.hisal;
2、如果id不同,如果是子查询,id的序号会递增,id值越大优先级越高,越先被执行
explain select * from emp e where e.deptno in (select d.deptno from dept d where d.dname 'SALES");
3、id相同和不同的,同时存在:相同的可以认为是一组,从上往下顺序执行,在所有组中,id值越大,优先级越高,越先执行
explain select from emp e join dept d on e.deptno = d.deptno join salgrade sg on e.sa1between sg.losal and sg.hisa1 where e.deptno in (select d.deptno from dept d where
d.dname = 'SALES');
type
type显示的是访问类型,访问类型表示我是以何种方式去访问我们的数据,最容易想的是全表扫描,直接暴力的遍历一张表去寻找需要的数据,效率非常低下,访问的类型有很多,效率从最好到最坏依次是:
system > const > eq_ref > ref > fulltext > ref_or_null > index_merge > unique_subquery > index_subquery >range > index > ALL
key
执行过程中到底用到了那个索引。
优化器->推荐索引(可能不符合)【forceIndex强制索引修改】
rows
进行检索的时候,读取数据行数的预估量
extra
额外的信息:执行SQL用到的技术点-索引覆盖、索引下推等