expalin精讲
id字段1. id相同
执行顺序从上至下2. id不同
例子:
explain select subject.* from subject,student_score,teacher where subject.id = student_id and subject.teacher_id = teacher.id;
读取顺序:subject > teacher > student_score
如果是子查询,id的序号会递增,id的值越大优先级越高,越先被执行3. id相同又不同
例子:
explain select score.* from student_score as score where subject_id = (select id from subject where teacher_id = (select id from teacher where id = 2));
读取顺序:teacher > subject > student_score
id如果相同,可以认为是一组,从上往下顺序执行select_type字段1. SIMPLE
在所有组中,id值越大,优先级越高,越先执行
例子:
explain select subject.* from subject left join teacher on subject.teacher_id = teacher.id
-> union
-> select subject.* from subject right join teacher on subject.teacher_id = teacher.id;
读取顺序:2.teacher > 2.subject > 1.subject > 1.teacher
简单查询,不包含子查询或Union查询2. PRIMARY
例子:
explain select subject.* from subject,student_score,teacher where subject.id = student_id and subject.teacher_id = teacher.id;
查询中若包含任何复杂的子部分,最外层查询则被标记为主查询3. SUBQUERY
例子:
explain select score.* from student_score as score where subject_id = (select id from subject where teacher_id = (select id from teacher where id = 2));
在select或where中包含子查询4. DERIVED
例子:
explain select score.* from student_score as score where subject_id = (select id from subject where teacher_id = (select id from teacher where id = 2));
在FROM列表中包含的子查询被标记为DERIVED(衍生),MySQL5. UNION
会递归执行这些子查询,把结果放在临时表中
备注:
MySQL5.7+ 进行优化了,增加了derived_merge(派生合并),默认开启,可加快查询效率
若第二个select出现在uion之后,则被标记为UNION6. UNION RESULT
例子:
explain select subject.* from subject left join teacher on subject.teacher_id = teacher.id
-> union
-> select subject.* from subject right join teacher on subject.teacher_id = teacher.id;
从UNION表获取结果的selecttype字段
例子:
explain select subject.* from subject left join teacher on subject.teacher_id = teacher.id
-> union
-> select subject.* from subject right join teacher on subject.teacher_id = teacher.id;
NULL>system>const>eq_ref>ref>fulltext>ref_or_null>index_merge>unique_subquery>index_subquery>range>index>ALL //最好到最差1. NULL
备注:掌握以下10种常见的即可
NULL>system>const>eq_ref>ref>ref_or_null>index_merge>range>index>ALL
MySQL能够在优化阶段分解查询语句,在执行阶段用不着再访问表或索引2. system
例子:
explain select min(id) from subject;
表只有一行记录(等于系统表),这是const类型的特列,平时不大会出现,可以忽略3. const
表示通过索引一次就找到了,const用于比较primary key或uique索引,因为只匹配一行数据,所以很快,如主键置于where列表中,MySQL就能将该查询转换为一个常量4. eq_ref
例子:
explain select * from teacher where teacher_no = 'T2010001';
唯一性索引扫描,对于每个索引键,表中只有一条记录与之匹配,常见于主键或唯一索引扫描5. ref
例子:
explain select subject.* from subject left join teacher on subject.teacher_id = teacher.id;
非唯一性索引扫描,返回匹配某个单独值的所有行6. ref_or_null
本质上也是一种索引访问,返回所有匹配某个单独值的行
然而可能会找到多个符合条件的行,应该属于查找和扫描的混合体
例子:
explain select subject.* from subject,student_score,teacher where subject.id = student_id and subject.teacher_id = teacher.id;
类似ref,但是可以搜索值为NULL的行7. index_merge
例子:
explain select * from teacher where name = 'wangsi' or name is null;
表示使用了索引合并的优化方法8. range
例子:
explain select * from teacher where id = 1 or teacher_no = 'T2010001' .
只检索给定范围的行,使用一个索引来选择行,key列显示使用了哪个索引9. index
一般就是在你的where语句中出现between、<>、in等的查询。
例子:
explain select * from subject where id between 1 and 3;
Full index Scan,Index与All区别:index只遍历索引树,通常比All快10. ALL
因为索引文件通常比数据文件小,也就是虽然all和index都是读全表,但index是从索引中读取的,而all是从硬盘读的。
例子:
explain select id from subject;
Full Table Scan,将遍历全表以找到匹配行table字段
例子:
explain select * from subject;
数据来自哪张表possible_keys字段
显示可能应用在这张表中的索引,一个或多个key字段
查询涉及到的字段若存在索引,则该索引将被列出,但不一定被实际使用
NULL,则没有使用索引key_len字段
查询中若使用了覆盖索引(查询的列刚好是索引),则该索引仅出现在key列表
_len显示的值为索引字段最大的可能长度,并非实际使用长度ref字段
即key_len是根据定义计算而得,不是通过表内检索出的
显示索引的哪一列被使用了,如果可能的话,是一个常数,哪些列或常量被用于查找索引列上的值rows字段
根据表统计信息及索引选用情况,大致估算出找到所需的记录所需读取的行数partitions字段
匹配的分区filtered字段
查询的表行占表的百分比Extra字段
包含不适合在其它列中显示但十分重要的额外信息1. Using filesort
说明MySQL会对数据使用一个外部的索引排序,而不是按照表内的索引顺序进行读取2. Using temporary
MySQL中无法利用索引完成的排序操作称为“文件排序”
例子:
explain select * from subject order by name;
使用了临时表保存中间结果,MySQL在对结果排序时使用临时表,常见于排序order by 和分组查询group by3. Using index
例子:
explain select subject.* from subject left join teacher on subject.teacher_id = teacher.id
-> union
-> select subject.* from subject right join teacher on subject.teacher_id = teacher.id;
表示相应的select操作中使用了覆盖索引(Covering Index),避免访问了表的数据行,效率不错!4. Using where
如果同时出现using where,表明索引被用来执行索引键值的查找
如果没有同时出现using where,表明索引用来读取数据而非执行查找动作
例子:
explain select subject.* from subject,student_score,teacher where subject.id = student_id and subject.teacher_id = teacher.id;
备注:
覆盖索引:select的数据列只用从索引中就能够取得,不必读取数据行,MySQL可以利用索引返回select列表中的字段,而不必根据索引再次读取数据文件,即查询列要被所建的索引覆盖
使用了where条件5. Using join buffer
例子:
explain select subject.* from subject,student_score,teacher where subject.id = student_id and subject.teacher_id = teacher.id;
使用了连接缓存6. impossible where
例子:
explain select student.*,teacher.*,subject.* from student,teacher,subject;
where子句的值总是false,不能用来获取任何元组7. distinct
例子:
explain select * from teacher where name = 'wangsi' and name = 'lisi';
一旦mysql找到了与行相联合匹配的行,就不再搜索了8. Select tables optimized away
例子:
explain select distinct teacher.name from teacher left join subject on teacher.id = subject.teacher_id;
SELECT操作已经优化到不能再优化了(MySQL根本没有遍历表或索引就返回数据了)使用的数据表
例子:
explain select min(id) from subject;
create table subject(
-> id int(10) auto_increment,
-> name varchar(20),
-> teacher_id int(10),
-> primary key (id),
-> index idx_teacher_id (teacher_id));//学科表
create table teacher(
-> id int(10) auto_increment,
-> name varchar(20),
-> teacher_no varchar(20),
-> primary key (id),
-> unique index unx_teacher_no (teacher_no(20)));//教师表
create table student(
-> id int(10) auto_increment,
-> name varchar(20),
-> student_no varchar(20),
-> primary key (id),
-> unique index unx_student_no (student_no(20)));//学生表
create table student_score(
-> id int(10) auto_increment,
-> student_id int(10),
-> subject_id int(10),
-> score int(10),
-> primary key (id),
版权声明
本文仅代表作者观点,不代表博信信息网立场。