ain select sum(amount) from customer a,payment b where 1=1 anda.customer_id=b.customer_id and email='JANE.BENNETT@sakilacustomer.org'\G;
*************************** 1. row***************************
id: 1
select_type: SIMPLE
table: a
type: ALL
possible_keys: PRIMARY
key: NULL
key_len: NULL
ref: NULL
rows: 599
Extra: Using where
*************************** 2. row***************************
id: 1
select_type: SIMPLE
table: b
type: ref
possible_keys: idx_fk_customer_id
key: idx_fk_customer_id
key_len: 2
ref: sakila.a.customer_id
rows: 13
Extra: NULL
2 rows in set (0.00 sec)
?
ERROR:
No query specified
解释:
select_type:表示select的类型,常见的取值有SIMLE(简单表,不使用表连接或者子查询)
PRIMARY(主查询,即外层的查询)、UNION(UNION中第二个或者后面的查询语句)、SUBQUERY(子查询中的第一个SELECT)
table:输出结果集的表
type:表示MySQL在表中找到所需行的方式,或者叫访问类型,常见类型如下:
ALL:全表扫描,遍历所有行来找到匹配行
index:索引全扫描,遍历整个索引来查询匹配的行
range:索引范围扫描,常见于<、<=、>、>=、between
ref:使用非唯一索引扫描或唯一索引的前缀扫描,返回匹配某个单独值的记录行
eq_ref:使用的索引是唯一索引,对于每个索引键值,表中只有一条记录匹配(多表一般使用parimary key 和unique index作为关联条件)
const/system:单表中最多有一行匹配,查询速度非常迅速,所以这个匹配行中其他列的值可以被优化器在当前查询中当作常量来处理。
NULL:mysql不用访问表或者索引,直接就能得到结果。
ref_or_null:与ref类似,区别在于条件中包含对NULL的查询。
index_merge:索引合并优化
unique_subquery:in的后面是一个查询主键字段的子查询
index_subquery:与uniue_subquery类似,区别在于IN的后面是查询非唯一索引字段的子查询。
possible_keys:表示查询时可能使用的索引。
key:表示实际使用的索引
key_len:使用到索引字段的长度
rows:扫描行的数量
extra:执行情况的说明和描述,包含不适合在其他列中显示,但是对执行计划非常重要的额外信息。
4. 通过explain extended和show warnings查看优化器执行情况
mysql>explain extended select * from t99 where 1=1 and id=10414\G;
*************************** 1. row***************************
id: 1
select_type: SIMPLE
table: t99
type: const
possible_keys: PRIMARY
key: PRIMARY
key_len: 4
ref: const
rows: 1
filtered: 100.00
Extra: NULL
1 row in set, 1 warning (0.00 sec)
?
ERROR:
No query specified
通过showwarnings来查执行计划
mysql>show warnings\G;
*************************** 1. row***************************
Level: Note
Code: 1003
Message: /* select#1 */ select '10414' AS`id`,'rtkit:x:499:497:RealtimeKit:/proc:/sbin/nologin' AS `name` from`test2`.`t99` where 1
1 row in set (0.00 sec)
?
ERROR:
No query specified
可以看到执行计划会把1=1去掉,并且在explain extended多了一个filered字段。
5. 通过explain partitions来查看分区内容
mysql>explain partitions select * from emp1 where id=24088\G;
*************************** 1. row***************************
id: 1
select_type: SIMPLE
table: emp1
partitions: p0
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 69667
Extra: Using where
1 row in set (0.00 sec)
?
ERROR:
No query specified
?