type 访问类型表示mysql中找到所需要行的方式,
或者叫做访问类型,常见的有all,index,range,ref,eq_ref,const,system,null
性能从左到右,左边最差,右边最好

1.type = ALL ,全表扫描,mysql遍历全表来找到匹配的行

  1. EXPLAIN SELECT * from film WHERE rating > 9;

执行结果:
image.png

2.type = index ,索引全扫描,Mysql遍历整个索引来查找匹配的行

  1. EXPLAIN SELECT * from film WHERE rating > 9;

执行结果:
image.png

3. type = range,索引范围扫描, 常见于 <,<=,>,>=,between等操作符

  1. EXPLAIN SELECT * from payment WHERE customer_id >= 300 and customer_id <= 350;

执行结果:
image.png

4.type = ref 使用非唯一索引扫描或者唯一索引前缀扫描,返回匹配某个单值的记录行,ref经常出现在join操作中

  1. EXPLAIN SELECT * from payment WHERE customer_id = 350;

执行结果:

image.png

5.type = eq_ref ,类似于ref,区别就在使用的索引是唯一索引,对于索引的,多表中使用primary key或者unique index作为关联条件

  1. EXPLAIN SELECT * from film a LEFT JOIN film_text b on a.film_id = b.film_id;
  2. EXPLAIN SELECT b.*,a.* from payment a, customer b WHERE a.customer_id = b.customer_id;

执行结果1:
image.png

执行结果2:
image.png

6. type =const/system ,单表最多有一个行匹配,查询非常快,根据主键primary key或者唯一索引unique index进行查询

  1. EXPLAIN SELECT * from (SELECT * from customer WHERE email = 'MARY.SMITH@sakilacustomer.org') tab -- email UNIQUE index

执行结果:
image.png

7.type = null ,mysql 不用访问表或者索引,直接就可以得到结果

  1. EXPLAIN SELECT 1 from dual WHERE 1;

执行结果:
image.png
总结:
1.tyoe属性还有其他值,
ref_or_null 和ref类型,区别在于包含了对null的查询
index_merge:索引合并并优化
unique_subquery: in 后面是一个查询主键关键字段的子查询
index_subquery: in后面非唯一索引字段的子查询
2.除了type还有
possible_keys:查询可能用到的索引
key:实际使用到的索引
key_len:索引字段长度
rows:扫描的行数
extra:执行情况的说明和描述,执行计划额外的信息在这里显示