MySQL 提供了一个 EXPLAIN 命令, 它可以对 SELECT 语句的执行计划进行分析, 并输出 SELECT 执行的详细信息, 以供开发人员针对性优化。
查看该SQL语句有没有使用上了索引,有没有做全表扫描,这都可以通过explain命令来查看。
可以通过explain命令深入了解MySQL的基于开销的优化器,还可以获得很多可能被优化器考虑到的访问策略的细节,以及当运行SQL语句时哪种策略预计会被优化器采用。
EXPLAIN 命令用法十分简单, 在 SELECT 语句前加上 explain 就可以了, 例如:
参数说明
id
每个 SELECT语句都会自动分配的一个唯一标识符.
select_type
查询类型,主要用于区别普通查询、联合查询(union、union all)、子查询等复杂查询。
table
显示的查询表名,如果查询使用了别名,那么这里显示的是别名
type
可以判断是否使用到索引
possible_keys
此次查询中可能选用的索引,一个或多个
key
查询真正使用到的索引, select_type为index_merge时,这里可能出现两个以上的索引,其他的select_type这里只 会出现一个。
key_len
用于处理查询的索引长度,如果是单列索引,那就整个索引长度算进去,如果是多列索引,那么查询不一定都能使用到所有的列,具体使用到了多少个列的索引,这里就会计算进去,没有使用到的列,这里不会计算进去。留意下这个列的值,算一下你的多列索引总长度就知道有没有使用到所有的列了。另外,key_len只计算where条件用到的索引长度,而排序和分组就算用到了索引,也不会计算到key_len中。ref
如果是使用的常数等值查询,这里会显示const如果是连接查询,被驱动表的执行计划这里会显示驱动表的关联字段如果是条件使用了表达式或者函数,或者条件列发生了内部隐式转换,这里可能显示为funcrows
这里是执行计划中估算的扫描行数,不是精确值(InnoDB不是精确的值,MyISAM是精确的值,主要原因是InnoDB 里面使用了MVCC并发机制)
extra
这个列包含不适合在其他列中显示但十分重要的额外的信息
每个SELECT语句都会自动分配的一个唯一标识符
表示查询中操作表的顺序,有三种情况: id相同: 执行顺序由上到下id不同 : 如果是子查询,id号会自增,id越大,优先级越高id相同的、不同的 同时存在 id 列为null的就表示这个是一个结果集,不需要使用它来进行查询。查询类型,主要用于区别普通查询、联合查询(union、 union all) 、子查询等复杂查询
表示不需要union 操作或者不包含子查询的简单select查询。有连接查询时,外层的查询为simple,且只有一个
explain select * from tuser;一个需要union操作或者含有子查询的select,位于最外层的单位查询的select_type即为primary。且只有一个
explain select (select name from tuser) from tuser;除了from子句中包含的子查询外,其他地方出现的子查询都可能是subquery
explain select * from tuser where id = (select max(id) from tuser);与dependent union类似,表示这个subquery的查询要受到外部表查询的影响
explain select id, name , (select name from tdep a where a.id = b.dep) from tuser b;union连接的两个select查询,第一个查询是PRIMARY,除了第一个表外,第二个以后的表select_type都是union
explain select * from tuser where sex = '1' union select * from tuser where sex = '2';与union一样,出现在union 或union all语句中,但是这个查询要受到外部查询的影响
explain select * from tuser where sex in (select sex from tuser where sex = '1' union select sex from tuser where sex = '2');包含union的结果集,在union和union all 语句中,因为它不需要参与查询,所以id字段为null
explain select * from tuser where sex = '1' union select * from tuser where sex = '2';from子句中出现的子查询,也叫做派生表,其他数据库中可能叫做内联视图或嵌套select
explain select * from (select * from tuser where sex = '1') b;除了ALL之外,其他的type都可以使用到索引,除了index_merge之外,其他的type只可以用到一个索引
注意事项: 最少要索引使用到range级别表中只有一行数据或者是空表
explain select * from (select * from tuser where id = 1) a;使用唯一索引或者主键,返回记录一定是1行记录的等值where条件时,通常type是const。其他数据库也叫做唯一索 引扫描
explain select * from tuser where id = 1; explain select * from tuser where loginname = '1';关键字:连接字段主键或者唯一性索引
此类型通常出现在多表的 join 查询, 表示对于前表的每一个结果, 都只能匹配到后表的一行结果. 并且查询的比较操作通常是 ‘=’, 查询效率较高.
explain select a.id from tuser a left join tdep b on a.dep = b.id;针对非唯一性索引,使用等值(=) 查询 非主键。或者是使用了最左前缀规则索引的查询。
# 非唯一索引 explain select * from tuser where dep = 1; # 等值非主键连接 explain select a.id from tuser a left join tdep b on a.name = b.name; # 最左前缀 explain select * from tuser where name = 'zhaoyun';全文索引检索,全文索引的优先级很高,若全文索引和普通索引同时存在时,mysql不管代价,优先选择使用全文索引
explain select * from taddr where match(addr) against('bei');与ref方法类似,只是增加了null值的比较。实际用的不多
用于where中的in形式子查询,子查询返回不重复值唯一值
用于in形式子查询使用到了辅助索引或者in常数列表,子查询可能返回重复值,可以使用索引将子查询去重。
索引范围扫描,常见于使用>,<,is null, between, in, like 等运算符的查询中
## tuser表至少要有一条数据id大于1 explain select id from tuser where id > 1; # like前缀索引 explain select * from tuser where name like 'z%';表示查询使用了两个以上的索引,最后取交集或者并集,常见and、or的条件使用了不同的索引
关键字: 条件时出现在索引树中的节点的。可能没有完全匹配索引。
索引全表扫描,把索引从头到尾扫一遍,常见于使用索引列就可以处理不需要读取数据文件的查询、可以使用索引排序或者分组的查询。
# 单索引 explain select loginname from tuser; # 组合索引 explain select age from tuser;这个就是全表扫描数据文件,然后再在server层进行过滤返回符合要求的记录。
explain select * from tuser;在select部分使用了distinct关键字
不带from子句的查询或者from dual查询
即,一般连接查询是先查询内表,再查询外表,反连接就是先查询外表,再查询内表。
查询时不需要回表查询,直接通过索引就可以获取查询的数据。
表示相应的SELECT查询中使用到了覆盖索引(Covering Index),避免访问表的数据行,效率不错!如果同时出现Using Where ,说明索引被用来执行查找索引键值如果没有同时出现Using Where ,表明索引用来读取数据而非执行查找动作。 explain select name, age, sex from tuser; 表示存储引擎返回的记录并不是所有的都满足查询条件,需要在server层进行过滤。
# 查询条件无索引 explain select * from tuser where address = 'beijing'; # 索引失效 explain select * from tuser where age = 1; # 索引失效 explain select * from tuser where id in (1,2); 查询条件中分为限制条件和检查条件,5.6之前,存储引擎只能根据限制条件扫描数据并返回,然后server层根 据检查条件进行过滤再返回真正符合查询的数据。5.6.x之后支持ICP特性,可以把检查条件也下推到存储引擎 层,不符合检查条件和限制条件的数据,直接不读取,这样就大大减少了存储引擎扫描的记录数量。extra列显 示using index condition(索引下推,会用到索引)
explain select * from tuser where name = 'asd';