id
- id相同时,执行顺序由上至下
- 如果是子查询,id的序号会递增,id值越大优先级越高,越先被执行
- id如果相同,可以认为是一组,从上往下顺序执行;在所有组中,id值越大,优先级越高,越先执行
select_type
- SIMPLE(简单SELECT,不使用UNION或子查询等)
- PRIMARY(子查询中最外层查询,查询中若包含任何复杂的子部分,最外层的select被标记为PRIMARY)
- UNION(UNION中的第二个或后面的SELECT语句)
- DEPENDENT UNION(UNION中的第二个或后面的SELECT语句,取决于外面的查询)
- UNION RESULT(UNION的结果,union语句中第二个select开始后面所有select)
- SUBQUERY(子查询中的第一个SELECT,结果不依赖于外部查询)
- DEPENDENT SUBQUERY(子查询中的第一个SELECT,依赖于外部查询)
- DERIVED(派生表的SELECT, FROM子句的子查询)
- UNCACHEABLE SUBQUERY(一个子查询的结果不能被缓存,必须重新评估外链接的第一行)
table
查询的表名称,可能是别名、(临时表)
type
从上到下性能更好
- ALL:遍历全表获取对应数据
- index: 遍历索引来获取对应数据
- range:只检索给定范围的行,使用一个索引来选择行 比如 > < 等操作
- ref: 非唯一性索引扫描,返回匹配某个单独值的所有行,单独操作 = id
- eq_ref: 主键或唯一性索引扫描
- const、system: 当MySQL对查询某部分进行优化,并转换为一个常量时,使用这些类型访问。如将主键置于where列表中,MySQL就能将该查询转换为一个常量,system是const类型的特例,当查询的表只有一行的情况下,使用system
- NULL: MySQL在优化过程中分解语句,执行时甚至不用访问表或索引,例如从一个索引列里选取最小值可以通过单独索引查找完成。
possible_keys
查询可能使用到的索引
key
查询最终使用的索引,如果为NULL表示没有使用索引
key_len
表示索引中使用的字节数,可通过该列计算查询中使用的索引的长度不损失精确性的情况下,长度越短越好。key_len显示的值为索引字段的最大可能长度,并非实际使用长度,即key_len是根据表定义计算而得,不是通过表内检索出的。
比如单列索引的情况下,key_len为单列索引长度,如果为多列索引,只有where条件中使用到的会被计算进去,而培训和分组即使有使用也不会被计算。
ref
当使用常量等值查询,显示const,
当关联查询时,会显示相应关联表的关联字段
如果查询条件使用了表达式、函数,或者条件列发生内部隐式转换,可能显示为func
其他情况null
rows
估算的找到所需的记录所需要读取的行数
extra
- Distinct : 一旦MYSQL找到了与行相联合匹配的行,就不再搜索了
- Range checked for each: 没有找到理想的索引,因此对于从前面表中来的每一个行组合,MYSQL检查使用哪个索引,并用它来从表中返回行。这是使用索引的最慢的连接之一
- Using join buffer:在我们联表查询的时候,如果表的连接条件没有用到索引,需要有一个连接缓冲区来存储中间结果
- Using filesort: 表示无法利用索引进行排序操作,发生了硬盘或内存排序。一般由order by 或 group by触发的;典型的情况,排序的字段不是驱动表的字段,则会使用临时表将数据都添加进去,最后进行排序,如果数据大则硬盘排序,如果小则内存排序.
- Using temporary : 使用了临时表,通常和Using filesort一起发生. 典型情况:使用了派生表
- Using index : 覆盖索引直接从索引中返回数据,而无需回表
- Using where: 提取所有数据又进行了一次条件过滤
- Using index condition: 查找使用了索引,但是需要回表查询数据
- Using index for group-by:使用了松散索引扫描
实例
CREATE TABLE `account` (
`id` int(8) unsigned NOT NULL AUTO_INCREMENT,
`money` int(8) NOT NULL,
`stock` int(8) NOT NULL DEFAULT '0',
`name` varchar(8) NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
UNIQUE KEY `uq_name` (`name`) USING BTREE,
UNIQUE KEY `uq_stock` (`stock`) USING BTREE,
KEY `idx_m_s` (`money`,`stock`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8mb4;
insert into account values(3,100,3,3),(4,2,4,4),(5,3,5,5),(6,4,6,6),(7,5,7,7),(8,6,8,8),(9,7,9,9);
typ = null
EXPLAIN
SELECT 1000 * 1000;
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------+
| 1 | SIMPLE | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | No tables used |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------+
typ = const
EXPLAIN
SELECT * FROM account where stock = 9;
+----+-------------+---------+------------+-------+---------------+----------+---------+-------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+---------+------------+-------+---------------+----------+---------+-------+------+----------+-------+
| 1 | SIMPLE | account | NULL | const | uq_stock | uq_stock | 4 | const | 1 | 100.00 | NULL |
+----+-------------+---------+------------+-------+---------------+----------+---------+-------+------+----------+-------+
typ = eq_ref
EXPLAIN
SELECT * FROM account a
LEFT JOIN account b ON a.id = b.id
where b.stock > 7;
+----+-------------+-------+------------+--------+------------------+----------+---------+--------------+------+----------+-----------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+--------+------------------+----------+---------+--------------+------+----------+-----------------------+
| 1 | SIMPLE | b | NULL | range | PRIMARY,uq_stock | uq_stock | 4 | NULL | 2 | 100.00 | Using index condition |
| 1 | SIMPLE | a | NULL | eq_ref | PRIMARY | PRIMARY | 4 | tz_test.b.id | 1 | 100.00 | NULL |
+----+-------------+-------+------------+--------+------------------+----------+---------+--------------+------+----------+-----------------------+
typ = ref
EXPLAIN
SELECT * FROM account where money = 10;
+----+-------------+---------+------------+------+---------------+---------+---------+-------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+---------+------------+------+---------------+---------+---------+-------+------+----------+-------+
| 1 | SIMPLE | account | NULL | ref | idx_m_s | idx_m_s | 4 | const | 1 | 100.00 | NULL |
+----+-------------+---------+------------+------+---------------+---------+---------+-------+------+----------+-------+
typ = rang / extra = using index condition
EXPLAIN
SELECT * FROM account where stock in (3,9);
+----+-------------+---------+------------+-------+---------------+----------+---------+------+------+----------+-----------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+---------+------------+-------+---------------+----------+---------+------+------+----------+-----------------------+
| 1 | SIMPLE | account | NULL | range | uq_stock | uq_stock | 4 | NULL | 2 | 100.00 | Using index condition |
+----+-------------+---------+------------+-------+---------------+----------+---------+------+------+----------+-----------------------+
typ = rang / extra = using index;using where
EXPLAIN
SELECT stock FROM account where stock in (3,9);
+----+-------------+---------+------------+-------+---------------+----------+---------+------+------+----------+--------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+---------+------------+-------+---------------+----------+---------+------+------+----------+--------------------------+
| 1 | SIMPLE | account | NULL | range | uq_stock | uq_stock | 4 | NULL | 2 | 100.00 | Using where; Using index |
+----+-------------+---------+------------+-------+---------------+----------+---------+------+------+----------+--------------------------+
typ = index / extra = using index
EXPLAIN
SELECT stock FROM account;
+----+-------------+---------+------------+-------+---------------+----------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+---------+------------+-------+---------------+----------+---------+------+------+----------+-------------+
| 1 | SIMPLE | account | NULL | index | NULL | uq_stock | 4 | NULL | 7 | 100.00 | Using index |
+----+-------------+---------+------------+-------+---------------+----------+---------+------+------+----------+-------------+
typ = ALL
EXPLAIN
SELECT * FROM account;
+----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------+
| 1 | SIMPLE | account | NULL | ALL | NULL | NULL | NULL | NULL | 7 | 100.00 | NULL |
+----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------+