explain是sql优化时的重要工具
使用方法
在要分析的sql语句签名添加 explain ,例如
explain select * from user;
结果项说明
id 查询的编号
select_type
查询类型,包含以下值
simple 不含子查询
primary 含子查询或派生查询
subquery 非from子查询
derived from型子查询
union
union result
table
查询针对的表,可以是表名或别名、执行结果、null、derived
possible_key
可能被用到的key
key
最终被使用的key
key_len
key的长度(越短越好)
type
查询的方式,重要指标,从最好到最差的连接类型为
system > const > eq_ref > ref > fulltext > ref_or_null > index_merge > unique_subquery > index_subquery > range > index > ALL
all 表示进行了全表扫描,语句很失败
index 比all好一点,扫描所有的索引节点,相当于index_all,好在了在索引文件上按顺序扫描
range 范围查询
ref 引用,索引定位到了某个范围
eq_ref 通过索引直接定位到了某一行
const system null 查询优化到常量级别,甚至不需要查找时间,一般按主键查询时,易出现const和system,直接查询某个表达式,不经过表时,出现NULL
ref
连接查询时,前后表的引用关系
rows
估计的扫描的行数
extra
using index 使用索引
using where 使用where条件
using temperay 说明建立了临时表
using file sort 产生文件排序
range checked for each record 遍历每条记录进行检查