美文网首页
MySQL之explain

MySQL之explain

作者: 郭之源 | 来源:发表于2018-04-29 00:03 被阅读10次

    在工作中,我们用于捕捉性能问题最常用的就是打开慢查询,定位执行效率差的SQL,那么当我们定位到一个SQL以后还不算完事,我们还需要知道该SQL的执行计划,比如是全表扫描,还是索引扫描,这些都需要通过EXPLAIN去完成。EXPLAIN命令是查看优化器如何决定执行查询的主要方法。可以帮助我们深入了解MySQL的基于开销的优化器,还可以获得很多可能被优化器考虑到的访问策略的细节,以及当运行SQL语句时哪种策略预计会被优化器采用。

    mysql>explain select d1.age, t2.id from (select age,name from t1 where id in (1,2))d1, t2 where d1.age=t2.age group by d1.age, t2.id order by t2.id;
    +----+-------------+------------+-------+---------------+---------+---------+--------+------+---------------------------------+
    | id | select_type | table      | type  | possible_keys | key     | key_len | ref    | rows | Extra                           |
    +----+-------------+------------+-------+---------------+---------+---------+--------+------+---------------------------------+
    |  1 | PRIMARY     | <derived2> | ALL   | NULL          | NULL    | NULL    | NULL   |    2 | Using temporary; Using filesort |
    |  1 | PRIMARY     | t2         | ref   | age           | age     | 5       | d1.age |    1 | Using where; Using index        |
    |  2 | DERIVED     | t1         | range | PRIMARY       | PRIMARY | 4       | NULL   |    2 | Using where                     |
    +----+-------------+------------+-------+---------------+---------+---------+--------+------+---------------------------------+
    3 rows in set (0.00 sec)
    

    1. id

    表示查询中执行select子句或操作表的顺序,越大越先执行,相同从上到下执行

    2. select_type

    标识查询中每个select子句的简单复杂程度

    • a.SIMPLE:查询中不包含子查询或UNION查询
    • b.PRIMARY:标记包含子查询的外层查询
    • c.SUBQUERY:在select或where中的子查询
    • d.DERIVED:在from中包含的子查询;子查询中使用union的外层查询
    • e.UNION:第二个select出现在union之后
    • f.UNION RESULT:从union表中获取结果的select

    SUBQUERY和UNION和可以标记为DEPENDENT和UNCACHEABLE
    DEPENDENT意味着select依赖外层查询中发现的数据
    UNCACHEABLE意味着select中的默写特性阻止结果被缓存于一个item_cache中

    3. table

    就是使用的表,这个表可以是用户创建的也可以是临时表或查询结果集(简单)

    Example:
    mysql> explain select d1.name, ( select id from t3) d2 from (select id,name from t1 where name='')d1 union (select name,id from t2);
    +----+--------------+------------+-------+---------------+---------+---------+------+------+-----------------+
    | id | select_type  | table      | type  | possible_keys | key     | key_len | ref  | rows | Extra           |
    +----+--------------+------------+-------+---------------+---------+---------+------+------+-----------------+
    |  1 | PRIMARY      | <derived3> | ALL   | NULL          | NULL    | NULL    | NULL |    2 | NULL            |
    |  3 | DERIVED      | t1         | ALL   | NULL          | NULL    | NULL    | NULL |    1 | Using where     |
    |  2 | SUBQUERY     | t3         | index | NULL          | PRIMARY | 4       | NULL |    1 | Using index     |
    |  4 | UNION        | t2         | ALL   | NULL          | NULL    | NULL    | NULL |    1 | NULL            |
    | NULL | UNION RESULT | <union1,4> | ALL   | NULL          | NULL    | NULL    | NULL | NULL | Using temporary |
    +----+--------------+------------+-------+---------------+---------+---------+------+------+-----------------+
    5 rows in set (0.00 sec)
    

    执行顺序:第四行->第二行->第三行->第一行->第五行
    第一行:id列为1,表示第一个select,select_type为primary表示该查询为外层查询,table列被标记为<derived3>,表示查询结果来自一个衍生表,其中3衍生表来自第三个select查询,即id为3的select
    第二行:id为3,表示第三个select,包含在from中所以是derived
    第三行:select列表的子查询,select_type为subquery,是整张表的第二个select
    第四行:select_type为union,说明第四个select是union里的第二个select,最先执行
    第五行:代表从union的临时表中读取行的阶段,table列的<union1,4>表示用第一个和第四个select的结果进行union

    4. type(重要且不容易)

    表示MySQL在表中找到所需行的方式,又称"访问类型",常见类型如下:
    ALL<index<range<ref<eq_ref<const<sysstem<NULL
    从左到右,性能从最差到最好
    a.ALL:FULL Table Scan,MySQL将遍历全表以找到匹配的行

    mysql> explain select * from t1 where email = '';
    +----+-------------+-------+------+---------------+------+---------+------+------+-------------+
    | id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra       |
    +----+-------------+-------+------+---------------+------+---------+------+------+-------------+
    |  1 | SIMPLE      | t1    | ALL  | NULL          | NULL | NULL    | NULL |    1 | Using where |
    +----+-------------+-------+------+---------------+------+---------+------+------+-------------+
    1 row in set (0.00 sec)
    

    b.index:Full Index Scan,遍历索引树

    mysql> explain select id from t1;
    +----+-------------+-------+-------+---------------+---------------+---------+------+------+-------------+
    | id | select_type | table | type  | possible_keys | key           | key_len | ref  | rows | Extra       |
    +----+-------------+-------+-------+---------------+---------------+---------+------+------+-------------+
    |  1 | SIMPLE      | t1    | index | NULL          | t1_name_index | 257     | NULL |    1 | Using index |
    +----+-------------+-------+-------+---------------+---------------+---------+------+------+-------------+
    1 row in set (0.00 sec)
    

    c.range:索引范围扫描,对索引的扫描开始于某一点,返回匹配值域的行。一般where子句使用索引字段betwwen、< 、>、in()和OR列表时候type会是range

    mysql> explain select * from t1 where id in (1,4);
    +----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
    | id | select_type | table | type  | possible_keys | key     | key_len | ref  | rows | Extra       |
    +----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
    |  1 | SIMPLE      | t1    | range | PRIMARY       | PRIMARY | 4       | NULL |    2 | Using where |
    +----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
    1 row in set (0.00 sec)
    
    mysql> explain select * from t1 where id between 1 and 4;
    +----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
    | id | select_type | table | type  | possible_keys | key     | key_len | ref  | rows | Extra       |
    +----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
    |  1 | SIMPLE      | t1    | range | PRIMARY       | PRIMARY | 4       | NULL |    1 | Using where |
    +----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
    1 row in set (0.00 sec)
    
    mysql> explain select * from t1 where id <1;
    +----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
    | id | select_type | table | type  | possible_keys | key     | key_len | ref  | rows | Extra       |
    +----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
    |  1 | SIMPLE      | t1    | range | PRIMARY       | PRIMARY | 4       | NULL |    1 | Using where |
    +----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
    1 row in set (0.00 sec)
    
    mysql> explain select * from t1 where id =10 or id= 20;
    +----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
    | id | select_type | table | type  | possible_keys | key     | key_len | ref  | rows | Extra       |
    +----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
    |  1 | SIMPLE      | t1    | range | PRIMARY       | PRIMARY | 4       | NULL |    2 | Using where |
    +----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
    1 row in set (0.00 sec)
    
    mysql> explain select * from t1 where phone like '1831131%';
    +----+-------------+-------+-------+-----------------+-----------------+---------+------+------+-----------------------+
    | id | select_type | table | type  | possible_keys   | key             | key_len | ref  | rows | Extra                 |
    +----+-------------+-------+-------+-----------------+-----------------+---------+------+------+-----------------------+
    |  1 | SIMPLE      | t1    | range | t1_phone_unique | t1_phone_unique | 767     | NULL |    1 | Using index condition |
    +----+-------------+-------+-------+-----------------+-----------------+---------+------+------+-----------------------+
    1 row in set (0.00 sec)
    

    d.ref:使用非唯一索引; 唯一索引的前缀扫描,返回匹配某个单独值的记录行(不知道啥意思,看懂的告诉我,哈哈)

    mysql> explain select * from t1 where name='yyyy';
    +----+-------------+-------+------+---------------+---------------+---------+-------+------+-----------------------+
    | id | select_type | table | type | possible_keys | key           | key_len | ref   | rows | Extra                 |
    +----+-------------+-------+------+---------------+---------------+---------+-------+------+-----------------------+
    |  1 | SIMPLE      | t1    | ref  | t1_name_index | t1_name_index | 257     | const |    1 | Using index condition |
    +----+-------------+-------+------+---------------+---------------+---------+-------+------+-----------------------+
    1 row in set (0.00 sec)
    

    e.eq_ref:类似ref,多表连接时使用唯一索引或主键连接

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

    f.const、system:当MySQL对查询某部分进行优化,并扎UN哈UN成一个常量时,使用这些类型访问。如将主键置于where列表中,MySQL就能将改查询转化成为一个常量。

    mysql> explain select * from (select * from t1 where id=1)b1;
    +----+-------------+------------+--------+---------------+---------+---------+-------+------+-------+
    | id | select_type | table      | type   | possible_keys | key     | key_len | ref   | rows | Extra |
    +----+-------------+------------+--------+---------------+---------+---------+-------+------+-------+
    |  1 | PRIMARY     | <derived2> | system | NULL          | NULL    | NULL    | NULL  |    1 | NULL  |
    |  2 | DERIVED     | t1         | const  | PRIMARY       | PRIMARY | 4       | const |    1 | NULL  |
    +----+-------------+------------+--------+---------------+---------+---------+-------+------+-------+
    

    2 rows in set (0.00 sec)
    注:system是const类型的特例,当查询的表只有一行的情况下,使用system

    g.NULL:MySQL在优化过程中分析语句,有时候甚至不用访问表或索引,例如从一个索引列中选取最小的值可以通过单独的索引查找完成

    mysql> explain select * from (select min(id) from t1)b1;
    +----+-------------+------------+--------+---------------+------+---------+------+------+------------------------------+
    | id | select_type | table      | type   | possible_keys | key  | key_len | ref  | rows | Extra                        |
    +----+-------------+------------+--------+---------------+------+---------+------+------+------------------------------+
    |  1 | PRIMARY     | <derived2> | system | NULL          | NULL | NULL    | NULL |    1 | NULL                         |
    |  2 | DERIVED     | NULL       | NULL   | NULL          | NULL | NULL    | NULL | NULL | Select tables optimized away |
    +----+-------------+------------+--------+---------------+------+---------+------+------+------------------------------+
    2 rows in set (0.00 sec)
    

    5.possible_keys(不重要)
    显示MySQL可能使用那个索引,但实际不一定使用

    6.key (重要)
    显示实际使用的索引,如果没有使用索引则为null

    mysql> explain select id from t1;
    +----+-------------+-------+-------+---------------+-----------------+---------+------+------+-------------+
    | id | select_type | table | type  | possible_keys | key             | key_len | ref  | rows | Extra       |
    +----+-------------+-------+-------+---------------+-----------------+---------+------+------+-------------+
    |  1 | SIMPLE      | t1    | index | NULL          | t1_phone_unique | 767     | NULL |    1 | Using index |
    +----+-------------+-------+-------+---------------+-----------------+---------+------+------+-------------+
    1 row in set (0.00 sec)
    
    mysql> explain select id,name from t1;
    +----+-------------+-------+-------+---------------+---------------+---------+------+------+-------------+
    | id | select_type | table | type  | possible_keys | key           | key_len | ref  | rows | Extra       |
    +----+-------------+-------+-------+---------------+---------------+---------+------+------+-------------+
    |  1 | SIMPLE      | t1    | index | NULL          | t1_name_index | 767     | NULL |    1 | Using index |
    +----+-------------+-------+-------+---------------+---------------+---------+------+------+-------------+
    1 row in set (0.00 sec)
    
    1. key_len(重要)
      表示索引中使用的字节数,可通过该列计算查询中使用的索引的长度(key_len显示的值为索引字段的最大可能长度,并非实际使用长度,
      即key_len是根据表定义计算而得,不是通过表内检索出的)

    2. ref
      表示上述表的连接匹配条件,即哪些列或常量被用于查找索引列上的值

    3. rows(重要)
      表示MySQL根据表统计信息及索引选用情况,估算的找到所需的记录所需要读取的行数

    mysql> explain select * from t1 , t2 where t1.id=t2.id and t2.name='atlas';
    +----+-------------+-------+--------+---------------+---------+---------+------------+------+-------------+
    | id | select_type | table | type   | possible_keys | key     | key_len | ref        | rows | Extra       |
    +----+-------------+-------+--------+---------------+---------+---------+------------+------+-------------+
    |  1 | SIMPLE      | t1    | ALL    | PRIMARY       | NULL    | NULL    | NULL       |    1 | NULL        |
    |  1 | SIMPLE      | t2    | eq_ref | PRIMARY       | PRIMARY | 4       | test.t1.id |    1 | Using where |
    +----+-------------+-------+--------+---------------+---------+---------+------------+------+-------------+
    2 rows in set (0.00 sec)
    
    1. Extra
      包含不适合在其他列中显示但十分重要的额外信息
      a. Using index
      该值表示相应的select操作中使用了覆盖索引(Covering Index)
    mysql>explain select id from t1;
    +----+-------------+-------+-------+---------------+------+---------+------+------+-------------+
    | id | select_type | table | type  | possible_keys | key  | key_len | ref  | rows | Extra       |
    +----+-------------+-------+-------+---------------+------+---------+------+------+-------------+
    |  1 | SIMPLE      | t1    | index | NULL          | age  | 5       | NULL |    4 | Using index |
    +----+-------------+-------+-------+---------------+------+---------+------+------+-------------+
    1 row in set (0.00 sec)
    

    覆盖索引(Covering Index)
    MySQL可以利用索引返回select列表中的字段,而不必根据索引再次读取数据文件
    包含所有满足查询需要的数据的索引称为覆盖索引(Covering Index)
    注意:如果要使用覆盖索引,一定要注意select列表中只取出需要的列,不可select *,因为如果将所有字段一起做索引会导致索引文件过大,查询性能下降

    b. Using where
    表示mysql服务器将在存储引擎检索行后再进行过滤。许多where条件里涉及索引中的列,当(并且如果)它读取索引时,就能被存储引擎检验,因此不是所有带where字句的查询都会显示"Using where"。有时"Using where"的出现就是一个暗示:查询可受益与不同的索引。

    mysql>explain select id,name from t1 where id<4;
    +----+-------------+-------+-------+---------------+------+---------+------+------+--------------------------+
    | id | select_type | table | type  | possible_keys | key  | key_len | ref  | rows | Extra                    |
    +----+-------------+-------+-------+---------------+------+---------+------+------+--------------------------+
    |  1 | SIMPLE      | t1    | index | PRIMARY       | name | 63      | NULL |    4 | Using where; Using index |
    +----+-------------+-------+-------+---------------+------+---------+------+------+--------------------------+
    1 row in set (0.00 sec)
    

    c. Using temporary
    表示MySQL需要使用临时表来存储结果集,常见于排序和分组查询

    这个值表示使用了内部临时(基于内存的)表。一个查询可能用到多个临时表。有很多原因都会导致MySQL在执行查询期间创建临时表。两个常见的原因是在来自不同表的上使用了DISTINCT,或者使用了不同的ORDER BY和GROUP BY列。可以强制指定一个临时表使用基于磁盘的MyISAM存储引擎。这样做的原因主要有两个:
    1)内部临时表占用的空间超过min(tmp_table_size,max_heap_table_size)系统变量的限制
    2)使用了TEXT/BLOB 列

    mysql>explain select id from t1 where id in (1,2) group by age,name;
    +----+-------------+-------+-------+---------------+---------+---------+------+------+----------------------------------------------+
    | id | select_type | table | type  | possible_keys | key     | key_len | ref  | rows | Extra                                        |
    +----+-------------+-------+-------+---------------+---------+---------+------+------+----------------------------------------------+
    |  1 | SIMPLE      | t1    | range | PRIMARY       | PRIMARY | 4       | NULL |    2 | Using where; Using temporary; Using filesort |
    +----+-------------+-------+-------+---------------+---------+---------+------+------+----------------------------------------------+
    1 row in set (0.00 sec)
    

    d. Using filesort
    MySQL中无法利用索引完成的排序操作称为“文件排序”

    mysql>explain select id,age from t1 order by name; 
    +----+-------------+-------+------+---------------+------+---------+------+------+----------------+
    | id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra          |
    +----+-------------+-------+------+---------------+------+---------+------+------+----------------+
    |  1 | SIMPLE      | t1    | ALL  | NULL          | NULL | NULL    | NULL |    4 | Using filesort |
    +----+-------------+-------+------+---------------+------+---------+------+------+----------------+
    1 row in set (0.00 sec)
    
    mysql>explain select id,age from t1 order by age; 
    +----+-------------+-------+-------+---------------+------+---------+------+------+-------------+
    | id | select_type | table | type  | possible_keys | key  | key_len | ref  | rows | Extra       |
    +----+-------------+-------+-------+---------------+------+---------+------+------+-------------+
    |  1 | SIMPLE      | t1    | index | NULL          | age  | 5       | NULL |    4 | Using index |
    +----+-------------+-------+-------+---------------+------+---------+------+------+-------------+
    1 row in set (0.00 sec)
    

    e. Using join buffer
    改值强调了在获取连接条件时没有使用索引,并且需要连接缓冲区来存储中间结果。如果出现了这个值,那应该注意,根据查询的具体情况可能需要添加索引来改进能。

    mysql>explain select t1.name from t1 inner join t2 on t1.name=t2.name;
    +----+-------------+-------+-------+---------------+------+---------+--------------+------+--------------------------+
    | id | select_type | table | type  | possible_keys | key  | key_len | ref          | rows | Extra                    |
    +----+-------------+-------+-------+---------------+------+---------+--------------+------+--------------------------+
    |  1 | SIMPLE      | t1    | index | name          | name | 63      | NULL         |    4 | Using index              |
    |  1 | SIMPLE      | t2    | ref   | name          | name | 63      | test.t1.name |    2 | Using where; Using index |
    +----+-------------+-------+-------+---------------+------+---------+--------------+------+--------------------------+
    2 rows in set (0.00 sec)
    
    mysql>alter table t1 drop key name;                                   
    Query OK, 0 rows affected (0.02 sec)
    Records: 0  Duplicates: 0  Warnings: 0
    
    mysql>alter table t2 drop key name; 
    Query OK, 0 rows affected (0.02 sec)
    Records: 0  Duplicates: 0  Warnings: 0
    
    mysql>explain select t1.name from t1 inner join t2 on t1.name=t2.name;
    +----+-------------+-------+------+---------------+------+---------+------+------+--------------------------------+
    | id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra                          |
    +----+-------------+-------+------+---------------+------+---------+------+------+--------------------------------+
    |  1 | SIMPLE      | t1    | ALL  | NULL          | NULL | NULL    | NULL |    4 |                                |
    |  1 | SIMPLE      | t2    | ALL  | NULL          | NULL | NULL    | NULL |    4 | Using where; Using join buffer |
    +----+-------------+-------+------+---------------+------+---------+------+------+--------------------------------+
    2 rows in set (0.00 sec)
    

    f. Impossible where
    这个值强调了where语句会导致没有符合条件的行。

    mysql>EXPLAIN SELECT * FROM t1 WHERE 1=2;
    +----+-------------+-------+------+---------------+------+---------+------+------+------------------+
    | id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra            |
    +----+-------------+-------+------+---------------+------+---------+------+------+------------------+
    |  1 | SIMPLE      | NULL  | NULL | NULL          | NULL | NULL    | NULL | NULL | Impossible WHERE |
    +----+-------------+-------+------+---------------+------+---------+------+------+------------------+
    1 row in set (0.00 sec)
    

    h. Select tables optimized away
    这个值意味着仅通过使用索引,优化器可能仅从聚合函数结果中返回一行.

    mysql>explain select max(id) from t1;
    +----+-------------+-------+------+---------------+------+---------+------+------+------------------------------+
    | id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra                        |
    +----+-------------+-------+------+---------------+------+---------+------+------+------------------------------+
    |  1 | SIMPLE      | NULL  | NULL | NULL          | NULL | NULL    | NULL | NULL | Select tables optimized away |
    +----+-------------+-------+------+---------------+------+---------+------+------+------------------------------+
    1 row in set (0.00 sec)
    

    I. Index merges
    当MySQL 决定要在一个给定的表上使用超过一个索引的时候,就会出现以下格式中的一个,详细说明使用的索引以及合并的类型。
    Using sort_union(...)
    Using union(...)
    Using intersect(...)

    总结:
    • EXPLAIN不会告诉你关于触发器、存储过程的信息或用户自定义函数对查询的影响情况
    • EXPLAIN不考虑各种Cache
    • EXPLAIN不能显示MySQL在执行查询时所作的优化工作
    • 部分统计信息是估算的,并非精确值
    • EXPALIN只能解释SELECT操作,其他操作要重写为SELECT后查看执行计划。

    参考自Explain 命令详解

    相关文章

      网友评论

          本文标题:MySQL之explain

          本文链接:https://www.haomeiwen.com/subject/xounlftx.html