文章目录
Explain1. 定义(是什么)2. 作用(能干嘛)3. 使用(怎么玩)4. 各字段解释Explain
1. 定义(是什么)
使用EXPLAIN关键字可以模拟优化器执行SQL查询语句,从而知道MySQL是如何处理你的SQL语句的。分析你的查询语句或是表结构的性能瓶颈。
官网介绍:/doc/refman/8.0/en/using-explain.html。
2. 作用(能干嘛)
表的读取顺序(id)数据读取操作的操作类型(select_type)哪些索引可以使用(possible_keys)哪些索引被实际使用(key)表之间的应用(ref)每张表有多少行被优化器查询(rows)3. 使用(怎么玩)
Explain+SQL语句(+\G),执行后会得到如下信息:
4. 各字段解释
id
select查询的序列号,包含一组数字,表示查询中执行select子句或操作表的顺序,存在三种情况如下所示,为了显而易见,我们这里创建三个表及其索引并添加一些数据。
create table student (id int primary key,name varchar(20)); create table clazz (id int primary key, student_id int,name varchar(20)); create table subject(id int primary key,student_id int, name varchar(20));insert into student values(1,'Mary'),(2,'Lee'),(3,'Wang'); insert into clazz values(1,1,'2101'),(2,2,'2101'),(3,3,'2102');insert into subject values(1,1,'Math'),(2,2,'Music'),(3,3,'Music');create index ids_clazz_student_id on clazz(student_id);create index ids_clazz_name on clazz(name);create index ids_subject_student_id on subject(student_id);create index ids_subject_name on subject(name);
id相同,执行顺序由上至下
explain select student.*from clazz,student,subjectwhere clazz.student_id = student.id and clazz.student_id = subject.student_id and clazz.name = '2101';
如图所示,代表mysql操作表的顺序为clazz、subject、student。
id不同,如果是子查询,id的序号会递增,id值越大优先级越高,越先被执行
explain select student.*from studentwhere id = (select student_idfrom clazzwhere clazz.student_id = ( select student_idfrom subjectwhere subject.name = 'Math'));
如上所示,id值不同,mysql操作表的顺序为subject、clazz、student。
id既存在相同也存在不相同,首先对于不相同的id,id值大的先执行,然后id相同的按顺序从上往下执行
explain select student.* from(select student_idfrom subjectwhere subject.name = 'Music') cla,studentwhere cla.student_id = student.id;
如上所示,mysql操作表顺序为subject、<derived2>
、student,<derived2>
代表它是在from里衍生出来的表,它的命名是from里的表的select_type+id(本例就是subject)。
select_type
查询的类型,主要是用于区别普通查询、联合查询、子查询等的复杂查询
SIMPLE:简单的select查询,查询中不包含子查询或者UNION。
explain select * from student;
PRIMARY:查询中包含任何复杂的子部分,最外层查询则被标记为PRIMARY。
SUBQUERY:在SELECT或者WHERE中包含了子查询。
DERIVED:在FROM列表中包含的子查询被标记为DERIVED(衍生)。MySQL会递归执行这些子查询,把结果放在临时表里。
explain select student.* from (select student_id from clazz where name = '2101') cla,student where id = (select student_id from subject where name = 'Math');
上例用来区分PRIMARY、SUBQUERY、DERIVED。
UNION:若第二个SELECT出现在UNION之后,则被标记为UNION;若UNION包含在FROM子句的子查询中,外层SELECT将被标记为:DERIVED。
UNION RESULT:从UNION表中获取结果的SELECT。
explain select * from student left join clazz on student.id = clazz.student_id union select * from student right join clazz on student.id = clazz.student_id;
上列用来区分UNION、UNION RESULT
table
显示这一行的数据是关于哪些表的。
当 from 子句中有子查询时,table列是 格式,表示当前查询依赖 id=N 的查询,于是先执行 id=N 的查询。当有 union 时,UNION RESULT 的 table 列的值为 <union1,2>,1和2表示参与 union 的 select 行id。
type
访问类型排序,是较为重要的一个指标,结果值从最好到最坏依次是:
system>const>eq_ref>ref>fulltext>ref_or_null>index_merge>unique_subquery>index_subquery>range>index>All
常见的有system>const>eq_ref>ref>range>index>All
system:表只有一行记录(等于系统表),这是const类型的特例,平时不会出现,这个也可以忽略不计。
const:表示通过索引一次就找到了,const用于比较primary key或则unique索引。因为只匹配一行数据,所以很快。如将主键置于where列表中,MySQL就能将该查询转换为一个常量。
explain select * from (select * from student where id = 1)st;
如上,对于from中的子查询利用主键id去找只匹配一条数据,其type为const,因此生成的虚拟表st就只有一条数据,所以对于外层查询器type为system。
eq_ref:唯一性索引扫描,对于每个索引键,表中只有一条记录与之匹配。常见于主键或唯一索引扫描。
explain select * from student,subject where student.id = subject.student_id and subject.name = 'Math';
如上,对于student表主键索引id,只有一条数据匹配。
ref:非唯一性索引扫描,返回匹配某个单独值的所有行。本质上也是一种索引访问,它返回所有匹配某个单独值的行,然而,它可能会找到多个符合条件的行,所以它应该属于查找和扫描的混合体。
create index ids_subject_name on subject(name); # 为subject表的name字段建立索引explain select * from subject where name = 'Math';
ref相较于eq_ref,其匹配的值可能有多个。
range:只检索给定范围的行,使用一个索引来选择行。key列显示使用了哪个索引。一般就是在你的where语句中出现了between、<、>、in等的查询。这种范围扫描索引扫描比全表扫描要好,因为它只需要开始于索引的某一点,而结束于另一点,不会扫描全部索引。
explain select * from subject where id in (1,2);
index:Full Index Scan,index与All区别为index类型只遍历索引树。这通常比All快,因为索引文件通常比数据文件小。(也就是说虽然all和index都是读全表,但index是从索引中读取的,而all是从硬盘中读的)
explain select id from subject;
all:Full Table Scan,将遍历全表以找到匹配的行。
explain select * from subject;
一般来说,得保证查询至少达到range级别,最好能达到ref。
possible_keys
显示可能应用在这张表中的索引,一个或多个。查询涉及到的字段上若存在索引,则该索引将被列出。但不一定被查询实际使用。
key
实际使用的索引。如果为NULL,则没有使用索引。查询中若使用了覆盖索引,则该索引仅出现在key列表中,不会出现在possible_keys列表中。(覆盖索引:查询的字段都在建立的复合索引的字段中,此时possible_keys为NULL,key为建立的复合索引)
explain select * from subject;
覆盖索引例子:
explain select name from subject;
key_len
表示索引中使用的字节数,可通过该列计算查询中使用的索引的长度。在不损失精确性的情况下,长度越短越好。key_len显示的值为索引字段的最大可能长度,并非实际使用长度,即key_len是根据表定义计算而得,不是通过表内检索出的。
explain select clazz.id,clazz.student_id,subject.* from subject inner join clazz on subject.student_id = clazz.student_id;
上述查询中student_id为int类型,占4个字节,又因为允许为空,需要多1个字节,总共占5个字节。
ref
显示索引的哪一列被使用了,如果可能的话,是一个常数。哪些列或常量被用于查找索引列上的值。查询中与其它表关联的字段,外键关系建立索引。
explain select * from subject,clazz where subject.student_id = clazz.student_id and subject.name = 'Math';
上图显示该查询语句中subject表的name字段匹配了一个常量,即‘Math’,clazz表的student_id字段匹配了db02数据库中的subject表的student_id字段。
rows
根据表统计信息及索引选用情况,大致估算出找到所需的记录所需要读取的行数。
explain select * from student,subject where student.id = subject.student_id and subject.name = 'Math';
上述查询对俩个表都只用了一行就找到,是最优的,一般来说,rows值越小越好。
Extra
包含不适合在其他列中显示但十分重要的额外信息。
Using filesort:说明mysql会对数据使用一个外部的索引排序,而不是按照表内的索引顺序进行读取。MySQL中无法利用索引完成的排序操作成为“文件排序”。
为了举例,我们先建表、建索引、添加数据;
create table tenant (id int primary key,name varchar(20),age int,phone char(11));create index idx_tenant_name_age_phone on tenant(name,age,phone);insert into tenant values(1,'z3',20,'15292258596'),(2,'l4',19,'19973886766'),(3,'w5',21,'15084769739');
接着执行explain
explain select * from tenant where name = 'w5' order by phone;
我们建立了一个name、age、phone的复合索引,但是上述sql语句并没有按顺序使用复合索引的字段,导致了索引失效。我们稍微修改一下sql语句,就会大有不同:
explain select * from tenant where name = 'w5' order by age,phone;
Using temporary:使用了临时表保存中间结果,MySQL在对查询结果排序时使用临时表。常见于排序order by和分组查询group by。
explain select * from tenant where name in ('z3','l4','w5') group by age;
优化:
explain select * from tenant where name in ('z3','l4','w5') group by name,age;
Using index:表示相应的select操作中使用了覆盖索引(Covering Index),避免访问了表的数据行,效率不错!如果同时出现using where,表明索引被用来执行索引键值的查找;如果没有同时出现using where,表明索引用来读取数据而非执行查找动作。
explain select age,name from tenant;
explain select age,phone from tenant where name like '1%';
Using where:表明使用了where过滤。
Using join buffer:使用了连接缓存。
这里我们使用前面的student、clazz、subject三个表
explain select * from student inner join clazz on student.name = clazz.name inner join subject on clazz.name = subject.name;
impossible where:where子句的值总是false,不能用来获取任何元组。(查询语句中where的条件不可能被满足,恒为False)
explain select * from tenant where name = '1' and name = '2';
select tables optimized away:在没有GROUPBY子句的情况下,基于索引优化MIN/MAX操作或者对于MyISAM存储引擎优化COUNT(*)操作,不必等到执行阶段再进行计算,查询执行计划生成的阶段即完成优化。
distinct:优化distinct操作,在找到第一匹配的元组后即停止找相同值的动作。