概念:
explain显示了MySQL如何使用索引来处理select语句以及连接表,我们可以根据explain的结果来优化我们的sql。
结果:
id
select_type
说明: 查询的类型
SIMPLE 普通查询
table
说明: 扫描的表。
type
说明:type是一个很重要的指标,type从好到差依次为:
system > const > eq_ref > ref > fulltext > ref_or_null > index_merge > unique_subquery > index_subquery > range > index > ALL
const 使用唯一索引,扫描一次就找到了对应的记录。
eg:根据主键查询数据
ref 使用非唯一索引进行扫描。
eg:select * from t_message t where t.owner ='[email protected]' (注:在owner列上建立了非唯一索引)
range 使用索引进行范围查询,即:在索引列上进行 LIKE、BETWEEN、IN、>= 等操作
eg:select * from t_message t where t.owner like 'jack%' (注:在owner列上建立了非唯一索引)
index 按照索引的顺序进行全表扫描。注:index与ALL相比唯一的优势就是:查询出来的数据是按照一定顺序(即:索引的顺序)排列的。
eg:select * from t_message order by id (注:id为主键)
ALL 全表(顺序)扫描,即:不使用索引,直接读取表上的数据。
eg:select * from t_message order by title
eg:select * from t_message
possible_keys
说明:可以使用的索引。
key
说明:实际使用的索引。
Null 表示没有使用索引。
PRIMARY 表示使用了主键。
key_len
说明:实际使用的索引的长度,从这个指标可以判断出复合索引中的哪些列被使用了。
ref
const 使用索引进行等值查询。
eg:select * from t_message where owner='jxn' (注:在owner列上建立了索引)
rows
说明:执行该sql时,MySQL查询了多少行。
Extra
说明:表示其它的一些信息。
Using index 只从索引树中查询信息,即该sql只查询索引列的值。
eg:select owner from t_message (注:在owner列上建立了索引)
Using index condition 查询非索引列的数据时,查询条件中使用了索引。
Using where 使用了where语句。
Using filesort 排序时没有根据索引来排序。