上一篇:https://blog.csdn.net/qq_40673786/article/details/90039032
索引类型 primary key ,unique ,normal ,full text
以下是表的连接类型type从最优到最差【查询优化级别type至少达到range】
system>const>eq_ref>ref>ref_or_null>index_merge>unique_subquery
>index_subquery>range>index>all
- const
数据表只有一个匹配行,适用于索引类型为primary或unique索引
示例
EXPLAIN select * from test where id=“1” #primary
EXPLAIN select * from test where name=“a” #name建立了unique索引
- eq_ref
对于每个来自于前面的表的行的组合,从该表中读取一行,当一个索引的所有部分都在查询中使用且索引类型为primary或unique时,即可使用这种类型
EXPLAIN SELECT * from test t,test_copy te where t.id=te.id # eq_ref primary
- ref
对于每个来自于前面的表的行的组合,从该表中读取一行,当一个索引的所有部分都在查询中使用且索引类型不为primary或unique时,即可使用这种类型
EXPLAIN select * from test where name=“a” #name索引为normal类型
- ref_or_null
此类型类似于ref,区别在于行数据为空也可以获取
EXPLAIN select name from test where name=“a” or name IS NULL
- index_merge
此类型表示使用了索引合并优化方案
- unique_subquery
子查询索引字段的类型为unique或primary时,此处为id字段【建立了主键】
EXPLAIN select name from test where id IN (select id from test)
- index_subquery
子查询字段索引类型为normal的子查询
EXPLAIN select * from test where name IN (select name from test) #name索引类型为normal
- range
只检索给定范围的行,用索引来选择行
, < ,>=, <=, in , <>, between and ,is null等
EXPLAIN select * from test where id>3
- index
与all相似,index扫描索引文件而all扫描数据文件
EXPLAIN SELECT age from test where age<>1
扫描二维码关注公众号,回复: 6182925 查看本文章
- all
全表扫描
EXPLAIN select * from test