Mysql 的 explain 详解

2021-04-19  本文已影响0人  yzw12138

explain 基本作用
1、explain 作用和 describe 一致;
2、explian tbl_name 的作用同 show columns from tbl_name 是一致的;
3、如果在 select 语句之前加入该关键字,则可以模拟优化器执行 sql,从而得知 Mysql 如何处理这条 sql 语句的,帮助我们分析查询语句提高性能;

mysql> explain `order`;
+---------+-------------+------+-----+---------+----------------+
| Field   | Type        | Null | Key | Default | Extra          |
+---------+-------------+------+-----+---------+----------------+
| id      | int(11)     | NO   | PRI | NULL    | auto_increment |
| orderNo | varchar(45) | YES  | UNI | NULL    |                |
| price   | int(11)     | YES  |     | NULL    |                |
| status  | tinyint(4)  | YES  |     | NULL    |                |
| date    | datetime    | YES  |     | NULL    |                |
| method  | tinyint(4)  | YES  |     | NULL    |                |
+---------+-------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)

mysql> describe `order`;
+---------+-------------+------+-----+---------+----------------+
| Field   | Type        | Null | Key | Default | Extra          |
+---------+-------------+------+-----+---------+----------------+
| id      | int(11)     | NO   | PRI | NULL    | auto_increment |
| orderNo | varchar(45) | YES  | UNI | NULL    |                |
| price   | int(11)     | YES  |     | NULL    |                |
| status  | tinyint(4)  | YES  |     | NULL    |                |
| date    | datetime    | YES  |     | NULL    |                |
| method  | tinyint(4)  | YES  |     | NULL    |                |
+---------+-------------+------+-----+---------+----------------+

explain 分析 select 语句结果

mysql> explain select * from `order`;
+----+-------------+-------+------------+------+---------------+------+---------+------+---------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows    | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+---------+----------+-------+
|  1 | SIMPLE      | order | NULL       | ALL  | NULL          | NULL | NULL    | NULL | 4854583 |   100.00 | NULL  |
+----+-------------+-------+------------+------+---------------+------+---------+------+---------+----------+-------+

explain 结果分析

id 是一组数字,表示查询过程中操作表的顺序,共分为三种情况:
1)id 相同,执行顺序自上而下;

mysql> explain select t1.*
    -> from orders t1, order_detail t2, seller t3
    -> where t1.orderNo = t2.orderNo and t2.sellerId = t3.sellerId;
+----+-------------+-------+------------+------+---------------+------+---------+------+---------+----------+----------------------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows    | filtered | Extra                                              |
+----+-------------+-------+------------+------+---------------+------+---------+------+---------+----------+----------------------------------------------------+
|  1 | SIMPLE      | t3    | NULL       | ALL  | NULL          | NULL | NULL    | NULL |       3 |   100.00 | NULL                                               |
|  1 | SIMPLE      | t2    | NULL       | ALL  | NULL          | NULL | NULL    | NULL |       5 |    20.00 | Using where; Using join buffer (Block Nested Loop) |
|  1 | SIMPLE      | t1    | NULL       | ALL  | NULL          | NULL | NULL    | NULL | 4854583 |   100.00 | Using where; Using join buffer (Block Nested Loop) |
+----+-------------+-------+------------+------+---------------+------+---------+------+---------+----------+----------------------------------------------------+
3 rows in set, 1 warning (0.02 sec)

2)id 不同,如果是子查询,id的序号会递增,id值越大优先级越高,越先被执行;

mysql> explain select * from orders where orderNo = ( select orderNo from order_detail where sellerId = (select sellerId from seller where id = 1));
+----+-------------+--------------+------------+-------+---------------+---------+---------+-------+---------+----------+-------------+
| id | select_type | table        | partitions | type  | possible_keys | key     | key_len | ref   | rows    | filtered | Extra       |
+----+-------------+--------------+------------+-------+---------------+---------+---------+-------+---------+----------+-------------+
|  1 | PRIMARY     | orders       | NULL       | ALL   | NULL          | NULL    | NULL    | NULL  | 4854583 |   100.00 | Using where |
|  2 | SUBQUERY    | order_detail | NULL       | ALL   | NULL          | NULL    | NULL    | NULL  |       5 |    20.00 | Using where |
|  3 | SUBQUERY    | seller       | NULL       | const | PRIMARY       | PRIMARY | 4       | const |       1 |   100.00 | NULL        |
+----+-------------+--------------+------------+-------+---------------+---------+---------+-------+---------+----------+-------------+
3 rows in set, 1 warning (0.01 sec)

3)id相同不同,同时存在。id 相同的认为一组自上而下顺序执行,id 不同,id 值越大越先执行;

mysql> explain select t1.* from (
    -> select orderNo
    -> from order_detail
    -> where id = 1) t2, orders t1
    -> where t1.orderNo = t2.orderNo;
+----+-------------+--------------+--------+---------------+---------+---------+------+------+-------------+
| id | select_type | table        | type   | possible_keys | key     | key_len | ref  | rows | Extra       |
+----+-------------+--------------+--------+---------------+---------+---------+------+------+-------------+
|  1 | PRIMARY     | <derived2>   | system | NULL          | NULL    | NULL    | NULL |    1 |             |
|  1 | PRIMARY     | t1           | ALL    | NULL          | NULL    | NULL    | NULL |   50 | Using where |
|  2 | DERIVED     | order_detail | const  | PRIMARY       | PRIMARY | 4       |      |    1 |             |
+----+-------------+--------------+--------+---------------+---------+---------+------+------+-------------+
3 rows in set (0.00 sec)

1) simple 简单 sql 查询,不包含子查询或者 union

mysql> explain select * from orders;
+----+-------------+--------+------+---------------+------+---------+------+------+-------+
| id | select_type | table  | type | possible_keys | key  | key_len | ref  | rows | Extra |
+----+-------------+--------+------+---------------+------+---------+------+------+-------+
|  1 | SIMPLE      | orders | ALL  | NULL          | NULL | NULL    | NULL |   50 |       |
+----+-------------+--------+------+---------------+------+---------+------+------+-------+
1 row in set (0.00 sec)

2)primary 查询过程中包含其他子部分

mysql> explain select * from (select orderNo from order_detail where id = 1) t;
+----+-------------+--------------+--------+---------------+---------+---------+------+------+-------+
| id | select_type | table        | type   | possible_keys | key     | key_len | ref  | rows | Extra |
+----+-------------+--------------+--------+---------------+---------+---------+------+------+-------+
|  1 | PRIMARY     | <derived2>   | system | NULL          | NULL    | NULL    | NULL |    1 |       |
|  2 | DERIVED     | order_detail | const  | PRIMARY       | PRIMARY | 4       |      |    1 |       |
+----+-------------+--------------+--------+---------------+---------+---------+------+------+-------+
2 rows in set (0.00 sec)

3) subquery 子查询的第一个 select

mysql> explain select * from orders where orderNo = (select orderNo from order_detail where id = 1);
+----+-------------+--------------+-------+---------------+---------+---------+-------+------+-------------+
| id | select_type | table        | type  | possible_keys | key     | key_len | ref   | rows | Extra       |
+----+-------------+--------------+-------+---------------+---------+---------+-------+------+-------------+
|  1 | PRIMARY     | orders       | ALL   | NULL          | NULL    | NULL    | NULL  |   50 | Using where |
|  2 | SUBQUERY    | order_detail | const | PRIMARY       | PRIMARY | 4       | const |    1 |             |
+----+-------------+--------------+-------+---------------+---------+---------+-------+------+-------------+
2 rows in set (0.01 sec)

4)union union中的从第二个之后的 select;union result union的结果

mysql> explain select * from orders where id = 1
    -> union
    -> select * from orders where id = 2;
+----+--------------+------------+-------+---------------+---------+---------+-------+------+-------+
| id | select_type  | table      | type  | possible_keys | key     | key_len | ref   | rows | Extra |
+----+--------------+------------+-------+---------------+---------+---------+-------+------+-------+
|  1 | PRIMARY      | orders     | const | PRIMARY       | PRIMARY | 4       | const |    1 |       |
|  2 | UNION        | orders     | const | PRIMARY       | PRIMARY | 4       | const |    1 |       |
| NULL | UNION RESULT | <union1,2> | ALL   | NULL          | NULL    | NULL    | NULL  | NULL |       |
+----+--------------+------------+-------+---------------+---------+---------+-------+------+-------+
3 rows in set (0.00 sec)

5)derived(衍生) 在 from 包含的子查询,MySQL会递归执行这些子查询,把结果放在临时表里;(3中有derived)

查询结果数据引用的表

1)system 表只有一行记录(等于系统表),平时很少出现,时 const 的一个特例;

2)const 表中最多只有一行匹配行,用于查询条件中的列是 primary key 或者 unique 索引的列;

mysql> show index from orders;
+--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table  | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| orders |          0 | PRIMARY  |            1 | id          | A         |          50 |     NULL | NULL   |      | BTREE      |         |               |
| orders |          0 | un_index |            1 | orderNo     | A         |          50 |     NULL | NULL   | YES  | BTREE      |         |               |
+--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)

mysql> explain select * from orders where orderNo = 'dac4c327-9810-11eb-af55-000c296fcfe0';
+----+-------------+--------+-------+---------------+----------+---------+-------+------+-------+
| id | select_type | table  | type  | possible_keys | key      | key_len | ref   | rows | Extra |
+----+-------------+--------+-------+---------------+----------+---------+-------+------+-------+
|  1 | SIMPLE      | orders | const | un_index      | un_index | 48      | const |    1 |       |
+----+-------------+--------+-------+---------------+----------+---------+-------+------+-------+
1 row in set (0.00 sec)

3)eq_ref 唯一性索引,对于每个索引键,表中只有一条记录与之匹配,常见于主键或唯一索引扫描;

mysql> explain select * from orders t1, order_detail t2 where t1.id = t2.id;
+----+-------------+-------+--------+---------------+---------+---------+------------+------+-------+
| id | select_type | table | type   | possible_keys | key     | key_len | ref        | rows | Extra |
+----+-------------+-------+--------+---------------+---------+---------+------------+------+-------+
|  1 | SIMPLE      | t2    | ALL    | PRIMARY       | NULL    | NULL    | NULL       |    4 |       |
|  1 | SIMPLE      | t1    | eq_ref | PRIMARY       | PRIMARY | 4       | test.t2.id |    1 |       |
+----+-------------+-------+--------+---------------+---------+---------+------------+------+-------+
2 rows in set (0.00 sec)

4)ref 非唯一索引,返回匹配结果的所有行;

mysql> create index in_or on orders(orderNo);
Query OK, 0 rows affected (0.45 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> explain select * from orders where orderNo = 'dac4c327-9810-11eb-af55-000c296fcfe0';
+----+-------------+--------+------+---------------+-------+---------+-------+------+-------------+
| id | select_type | table  | type | possible_keys | key   | key_len | ref   | rows | Extra       |
+----+-------------+--------+------+---------------+-------+---------+-------+------+-------------+
|  1 | SIMPLE      | orders | ref  | in_or         | in_or | 48      | const |    1 | Using where |
+----+-------------+--------+------+---------------+-------+---------+-------+------+-------------+
1 row in set (0.05 sec)

5)range 只检索给定范围的行,使用一个索引来选择行。key列显示使用了哪个索引
一般就是在你的where语句中出现了between、<、>、in等的查询
这种范围扫描索引扫描比全表扫描要好,因为他只需要开始索引的某一点,而结束语另一点,不用扫描全部索引

mysql> explain select * from orders where id > 34;
+----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+
| id | select_type | table  | type  | possible_keys | key     | key_len | ref  | rows | Extra       |
+----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+
|  1 | SIMPLE      | orders | range | PRIMARY       | PRIMARY | 4       | NULL |    4 | Using where |
+----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+
1 row in set (0.00 sec)

6)index 从索引文件中读取数据,因为索引文件一般情况下比数据文件要下因此,速度更快;

mysql> explain select orderNo from orders;
+----+-------------+--------+-------+---------------+-------+---------+------+------+-------------+
| id | select_type | table  | type  | possible_keys | key   | key_len | ref  | rows | Extra       |
+----+-------------+--------+-------+---------------+-------+---------+------+------+-------------+
|  1 | SIMPLE      | orders | index | NULL          | un_or | 48      | NULL |    4 | Using index |
+----+-------------+--------+-------+---------------+-------+---------+------+------+-------------+
1 row in set (0.03 sec)

7)all 遍历全表

mysql> explain select * from orders;
+----+-------------+--------+------+---------------+------+---------+------+------+-------+
| id | select_type | table  | type | possible_keys | key  | key_len | ref  | rows | Extra |
+----+-------------+--------+------+---------------+------+---------+------+------+-------+
|  1 | SIMPLE      | orders | ALL  | NULL          | NULL | NULL    | NULL |    4 |       |
+----+-------------+--------+------+---------------+------+---------+------+------+-------+
1 row in set (0.00 sec)

查询语句涉及的字段上若存在索引,则会被列出,但不一定在查询时使用到,如果为 null 则表示没有用到索引;

mysql> explain select * from orders where orderNo = 'dac4c327-9810-11eb-af55-000c296fcfe0' and id =51;
+----+-------------+--------+-------+------------------+---------+---------+-------+------+-------+
| id | select_type | table  | type  | possible_keys    | key     | key_len | ref   | rows | Extra |
+----+-------------+--------+-------+------------------+---------+---------+-------+------+-------+
|  1 | SIMPLE      | orders | const | PRIMARY,un_index | PRIMARY | 4       | const |    1 |       |
+----+-------------+--------+-------+------------------+---------+---------+-------+------+-------+
1 row in set (0.00 sec)

查询过程中实际用到的索引,为 null 时表示没有用到索引;

表示索引的字节数,key_len 显示的长度表示索引的最大长度,并不是实际使用长度,在不损失精度的情况下,长度越小越好;

显示索引那一列被使用了,如果可能的话,是一个常数。那些列或常量被用于查找索引列上的值

大致估算执行查询语句找到结果所需要读取的行数

重要的额外信息
1) Using filesort 表示 mysql 会用外部的一个索引排序,而不是按照表内的索引顺序进行读取;

mysql> explain select * from orders order by orderNo;
+----+-------------+--------+------+---------------+------+---------+------+------+----------------+
| id | select_type | table  | type | possible_keys | key  | key_len | ref  | rows | Extra          |
+----+-------------+--------+------+---------------+------+---------+------+------+----------------+
|  1 | SIMPLE      | orders | ALL  | NULL          | NULL | NULL    | NULL |    4 | Using filesort |
+----+-------------+--------+------+---------------+------+---------+------+------+----------------+
1 row in set (0.00 sec)

2) Using temporary 使用临时表存储中加数据,常用于 order by 和 group by;

mysql> explain select * from orders group by orderNo order by id desc;
+----+-------------+--------+-------+---------------+---------+---------+------+------+-----------------+
| id | select_type | table  | type  | possible_keys | key     | key_len | ref  | rows | Extra           |
+----+-------------+--------+-------+---------------+---------+---------+------+------+-----------------+
|  1 | SIMPLE      | orders | index | NULL          | PRIMARY | 4       | NULL |    4 | Using temporary |
+----+-------------+--------+-------+---------------+---------+---------+------+------+-----------------+
1 row in set (0.00 sec)
  1. Using index 使用覆盖索引
mysql> explain select orderNo from orders;
+----+-------------+--------+-------+---------------+-------+---------+------+------+-------------+
| id | select_type | table  | type  | possible_keys | key   | key_len | ref  | rows | Extra       |
+----+-------------+--------+-------+---------------+-------+---------+------+------+-------------+
|  1 | SIMPLE      | orders | index | NULL          | un_or | 48      | NULL |    4 | Using index |
+----+-------------+--------+-------+---------------+-------+---------+------+------+-------------+
1 row in set (0.00 sec)
上一篇下一篇

猜你喜欢

热点阅读