分析语句

explain是mysql中的一个指令,可以用来分析sql语句的执行计划,检测有没有使用到索引。

例如:explain select * from mvs;

select_type搜索的类型
table 搜索的表名
type 搜索的类型
possible_keys 可能用到的索引
key实际用到的索引
key_len 索引的长度
ref关联的字段
rows 扫描的行数
Extra 额外的信息
type 搜索类型

性能由高到低顺序

NULL > system > const > eq_ref > ref> range > index > ALL

NULL 不使用索引直接可以获得结果

explain select count(*) from mvs

system 表中满足条件的记录最多一条

explain select  from (select * from mvs limit 1 ) as a ;

const 表中满足条件的记录最多一条通常会出现在主键和unique索引中

explain select * from mvs where id =1;

eq_ref 表中某一列的值关联另一个表主键列的值,通常出现在联表查询中

explain select * from mvs left join types on types.id = mvs.type id;

ref 通过普通索引查询

explain select * from mvs where title =爱';

range索引范围查询

explain select * from mvs where id < 1000:

index 索引扫描

explain select id from mvs;

all 全表扫描

explain select * from mvs;