1、使用
EXPLAIN SELECT * FROM index_a;
2、说明
#table 表名
#type 访问类型
好至差:system > const > eq_ref > ref(查询:最好) > fulltext > ref_or_null > index_marge > unique_subquery > index_subquery > range(查询:至少) > index > all
#possible_keys 查询的字段上若存在索引,则将索引列出,一个或多个,但不一定在查询时实际使用;
#key 实际使用的索引,若为NULL,则没有使用索引 force/use index or ignore index 强制使用索引或者忽略索引
EXPLAIN SELECT * FROM index_a WHERE username = '李四';
EXPLAIN SELECT * FROM index_a force index(username_password) WHERE username = '李四';
EXPLAIN SELECT * FROM index_a use index(username_password) WHERE username = '李四';
EXPLAIN SELECT * FROM index_a ignore index(username, username_password) WHERE username = '李四';
#ley_len 使用的索引长度,越短越好。(索引字段的个数,74指1个,78指2个,140指3个)
#ref 显示使用索引的是哪个字段,可以是一个const常量;
#rows 检索的数据行数,越小越好;
#extra
#distinct:第一个匹配行
#not exists:优化了left join
#range checked for each record:没有找到理想的索引
#using index、where used
#system 系统查询,指表中只有一行数据,这是const的特例,平时不会出现,因为只有一行信息就不叫大数据了,所以意义 不大;
#const 常量查询,表示通过索引Index一次就找到了,用于比较 primary key=常量 和 unique=常量 这种索引;
EXPLAIN SELECT * FROM index_a WHERE id = 1;
#eq_ref 唯一性索引扫描,对于每个索引键,表中只对应一行数据,常见于主键
EXPLAIN SELECT * FROM index_a a, index_b b WHERE a.id = b.id;
#ref 非唯一索引扫描,对于每个索引键,表中可能对应多行数据;
EXPLAIN SELECT * FROM index_b WHERE username = '张三';
#range:范围查询,where后面的列表中是between、<、>、in、like等的查询;
EXPLAIN SELECT * FROM index_a WHERE id BETWEEN 1 AND 3;
#index 全索引扫描,FULL INDEX SCAN,只遍历索引树,通常比ALL快;(index与ALL都是全表查询,但index是从索引中 读取,ALL是从全表中读取);
#all:全表扫描,FULL TABLE SCAN,遍历全表找到匹配的行;速度最慢,避免使用;
EXPLAIN SELECT * FROM index_a WHERE username = '张三';
#using temporary:使用临时表(需要优化)
#using filesort:使用文件排序(需要优化)。。。