设为首页 加入收藏

TOP

mysql explain type(一)
2015-11-21 02:08:25 来源: 作者: 【 】 浏览:1
Tags:mysql explain type
explain执行计划中type字段分为以下几种:
ALL ? ? ? ?INDEX ? ? ? ?RANGE ? ? ? ?REF ? ? ? ?EQ_REF ? ? ? ?CONST,SYSTEM ? ? ? ?NULL
从左至右,性能从最差到最好
?
type = ALL,全表扫描,MYSQL扫描全表来找到匹配的行
(因为film表中rating不是索引)
mysql> explain extended select * from film where rating > 9\G
*************************** 1. row ***************************
? ? ? ? ? ?id: 1
? select_type: SIMPLE
? ? ? ? table: film
? ? ? ? ?type: ALL
possible_keys: NULL
? ? ? ? ? key: NULL
? ? ? key_len: NULL
? ? ? ? ? ref: NULL
? ? ? ? ?rows: 1024
? ? ?filtered: 100.00
? ? ? ? Extra: Using where
1 row in set, 1 warning (0.00 sec)
?
type = index,索引全扫描,MYSQL遍历整个索引来查找匹配的行。(虽然where条件中没有用到索引,但是要取出的列title是索引包含的列,所以只要全表扫描索引即可,直接使用索引树查找数据)
mysql> explain select title from film\G
*************************** 1. row ***************************
? ? ? ? ? ?id: 1
? select_type: SIMPLE
? ? ? ? table: film
? ? ? ? ?type: index
possible_keys: NULL
? ? ? ? ? key: idx_title
? ? ? key_len: 767
? ? ? ? ? ref: NULL
? ? ? ? ?rows: 1024
? ? ? ? Extra: Using index
1 row in set (0.00 sec)
?
type = range ,索引范围扫描,常见于<、<=、>、>=、between等操作符(因为customer_id是索引,所以只要查找索引的某个范围即可,通过索引找到具体的数据)
mysql> explain select * from payment where customer_id > 300 and customer_id < 350\G
*************************** 1. row ***************************
? ? ? ? ? ?id: 1
? select_type: SIMPLE
? ? ? ? table: payment
? ? ? ? ?type: range
possible_keys: idx_fk_customer_id
? ? ? ? ? key: idx_fk_customer_id
? ? ? key_len: 2
? ? ? ? ? ref: NULL
? ? ? ? ?rows: 1294
? ? ? ? Extra: Using where
1 row in set (0.01 sec)
?
type = ref ,使用非唯一性索引或者唯一索引的前缀扫描,返回匹配某个单独值的记录行。
?
(1)使用非唯一性索引customer_id单表查询
mysql> explain select * from payment where customer_id = 350\G
*************************** 1. row ***************************
? ? ? ? ? ?id: 1
? select_type: SIMPLE
? ? ? ? table: payment
? ? ? ? ?type: ref
possible_keys: idx_fk_customer_id
? ? ? ? ? key: idx_fk_customer_id
? ? ? key_len: 2
? ? ? ? ? ref: const
? ? ? ? ?rows: 23
? ? ? ? Extra:
1 row in set (0.00 sec)
?
(2)使用非唯一性索引联表查询(由于customer_id在a表中不是主键,是普通索引(非唯一),所以是ref)
mysql> explain select b.*, a.* from payment a ,customer b where a.customer_id = b.customer_id\G
*************************** 1. row ***************************
? ? ? ? ? ?id: 1
? select_type: SIMPLE
? ? ? ? table: b
? ? ? ? ?type: ALL
possible_keys: PRIMARY
? ? ? ? ? key: NULL
? ? ? key_len: NULL
? ? ? ? ? ref: NULL
? ? ? ? ?rows: 541
? ? ? ? Extra:
*************************** 2. row ***************************
? ? ? ? ? ?id: 1
? select_type: SIMPLE
? ? ? ? table: a
? ? ? ? ?type: ref
possible_keys: idx_fk_customer_id
? ? ? ? ? key: idx_fk_customer_id
? ? ? key_len: 2
? ? ? ? ? ref: sakila.b.customer_id
? ? ? ? ?rows: 14
? ? ? ? Extra:
2 rows in set (0.00 sec) ?
?
type = eq_ref,相对于ref来说就是使用的是唯一索引,对于每个索引键值,只有唯一的一条匹配记录(在联表查询中使用primary key或者unique key作为关联条件)
(在film和film_text中film_id都是主键,即都是唯一索引)
mysql> explain select * from film a ,film_text b where a.film_id = b.film_id\G
*************************** 1. row ***************************
? ? ? ? ? ?id: 1
? select_type: SIMPLE
? ? ? ? table: b
? ? ? ? ?type: ALL
possible_keys: PRIMARY
? ? ? ? ? key: NULL
? ? ? key_len: NULL
? ? ? ? ? ref: NULL
? ? ? ? ?rows: 1000
? ? ? ? Extra:
*************************** 2. row ***************************
? ? ? ? ? ?id: 1
? select_type: SIMPLE
? ? ? ? ta
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇MySql性能调优(五)采用合适的锁.. 下一篇再议Seconds_Behind_Master

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: