原文地址:https://www.douyacun.com/article/87ab8a19da4bdd941a21bdbd0c295b3b
mysql> explain select * from media where id = 100\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: media
partitions: NULL
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)
列的含义:
explain select m.target_id, (
select d.content_after from m_log d
where d.target_id = m.target_id
and d.target_type = 5
and d.create_time <= "2019-11-13 07:59:30"
and d.create_time >= "2019-10-01 00:00:00"
order by d.id desc limit 0,1
) as 变更后金额
from `m_log` m
where m.target_type = 5
and m.target_id in (select child from `relation` where parent = 1698747)
and m.create_time <= "2019-11-13 07:59:30"
and m.create_time >= "2019-10-01 00:00:00"
group by m.target_id;
id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
---|---|---|---|---|---|---|---|---|---|
1 | PRIMARY | ALL | NULL | NULL | NULL | NULL | NULL | Using temporary; Using filesort | |
1 | PRIMARY | m | ref | create_time,idx_target_id | idx_target_id | 4 | 2 | Using where | |
3 | MATERIALIZED | relation | ref | child,idx_par_st | idx_par_st | 4 | const | 14 | Using index |
2 | DEPENDENT SUBQUERY | d | ref | create_time,idx_target_id | idx_target_id | 4 | func | 2 | Using where; Using filesort |
const
type 的特殊情况。const
类型explain SELECT * from foo where id = 1;
id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
---|---|---|---|---|---|---|---|---|---|
1 | SIMPLE | foo | const | PRIMARY | PRIMARY | 4 | const | Directly search via Primary Index |
=
,查询效率较高explain select * from u, `u_ocpc` where u.id = u_ocpc.u_id and u.id in (2657099,2657010,2656981)
id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
---|---|---|---|---|---|---|---|---|---|
1 | SIMPLE | u_ocpc | range | PRIMARY | PRIMARY | 4 | NULL | 3 | Using where |
1 | SIMPLE | u | eq_ref | PRIMARY | PRIMARY | 4 | adv.unit_ocpc.unit_id | 1 | NULL |
explain select * from `config` C left JOIN `dark` D on D.medias = C.value where C.group_id = 1;
id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
---|---|---|---|---|---|---|---|---|---|
1 | SIMPLE | C | ref | config_group_id_key | config_group_id_key | 4 | const | 12 | NULL |
1 | SIMPLE | D | ALL | NULL | NULL | NULL | NULL | 4 | Using where; Using join buffer (Block Nested Loop) |
unique_subquery:使用了in的子查询,而且子查询是主键或者唯一索引
range: 索引范围查询, 这个类型通常出现在 =, <>, >, >=, <, <=, IS NULL, <=>, BETWEEN, IN()
explain select * from media where id between 2 and 10;
id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
---|---|---|---|---|---|---|---|---|---|
1 | SIMPLE | idea | range | PRIMARY | PRIMARY | 4 | NULL | 2 | Using where |
explain select * from creative WHERE target_url = 'http://cdn.yuming.com//allsites/1647060/3ea6c76347dd4c7167b9c1c83f069003/index_1738893.html?r=9223'
id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
---|---|---|---|---|---|---|---|---|---|
1 | SIMPLE | idea | ALL | NULL | NULL | NULL | NULL | 3222483 | Using where |
表示在mysql查询时,能够是用到的索引,即使这些索引在possible_key中出现,但是并不是此索引会真正被MYSQL使用到,具体用到使用了哪些索引,由KEY决定
MySQL 在当前查询时所真正使用到的索引.
索引的字节数,这个字段可以评估组合索引是否完全被使用, 或只有最左部分字段被使用到.
rows 也是一个重要的字段. MySQL 查询优化器根据统计信息, 估算 SQL 要查找到结果集需要扫描读取的数据行数
sql最重要的评价字段