zoukankan      html  css  js  c++  java
  • MySQL explain 详解

    通过 explain 命令获取 select 语句的执行计划,通过 explain 我们可以知道以下信息:表的读取顺序,数据读取操作的类型,哪些索引可以使用,哪些索引实际使用了,表之间的引用,每张表有多少行被优化器查询等信息。

    explain 信息有10列,分别是id,select_type,table,type,possible_keys,key,key_len,ref,row,Extra

    id:select标识符,select查询序列号;
    1. id相同时,执行顺序由上至下
    2. 如果是子查询,id的序号会递增,id值越大优先级越高,越先被执行
    3. id如果相同,可以认为是一组,从上往下顺序执行;在所有组中,id值越大,优先级越高,越先执行

    select_type:表示每个select子句的类型

    (1) SIMPLE(简单SELECT,不使用UNION或子查询等)
    (2) PRIMARY(子查询中最外层查询,查询中若包含任何复杂的子部分,最外层的select被标记为PRIMARY)
    (3) UNION(UNION中的第二个或后面的SELECT语句)
    (4) DEPENDENT UNION(UNION中的第二个或后面的SELECT语句,取决于外面的查询)
    (5) UNION RESULT(UNION的结果,union语句中第二个select开始后面所有select)
    (6) SUBQUERY(子查询中的第一个SELECT,结果不依赖于外部查询)
    (7) DEPENDENT SUBQUERY(子查询中的第一个SELECT,依赖于外部查询)
    (8) DERIVED(派生表的SELECT, FROM子句的子查询)
    (9) UNCACHEABLE SUBQUERY(一个子查询的结果不能被缓存,必须重新评估外链接的第一行)

    select_type的说明
    UNION:
    当通过union来连接多个查询结果时,第二个之后的select其select_type为UNION

    [yoon]> explain select * from tb1 where id=1 union select * from tb1 where id=2;
    +----+--------------+------------+-------+---------------+---------+---------+-------+------+-----------------+
    | id | select_type  | table      | type  | possible_keys | key     | key_len | ref   | rows | Extra           |
    +----+--------------+------------+-------+---------------+---------+---------+-------+------+-----------------+
    |  1 | PRIMARY      | tb1        | const | PRIMARY       | PRIMARY | 8       | const |    1 | NULL            |
    |  2 | UNION        | tb1        | const | PRIMARY       | PRIMARY | 8       | const |    1 | NULL            |
    | NULL | UNION RESULT | <union1,2> | ALL   | NULL          | NULL    | NULL    | NULL  | NULL | Using temporary |
    +----+--------------+------------+-------+---------------+---------+---------+-------+------+-----------------+

    DEPENDENT UNION与DEPENDENT SUBQUERY:
    当union作为子查询时,其中第二个union的select_type就是DEPENDENT UNION。第一个子查询的select_type则是DEPENDENT SUBQUERY。

    [yoon]> explain select * from tb1 where id in (select id from tb1 where id=7 union select id from tb1 where id=9);
    +----+--------------------+------------+-------+---------------+---------+---------+-------+------+-----------------+
    | id | select_type        | table      | type  | possible_keys | key     | key_len | ref   | rows | Extra           |
    +----+--------------------+------------+-------+---------------+---------+---------+-------+------+-----------------+
    |  1 | PRIMARY            | tb1        | ALL   | NULL          | NULL    | NULL    | NULL  |   16 | Using where     |
    |  2 | DEPENDENT SUBQUERY | tb1        | const | PRIMARY       | PRIMARY | 8       | const |    1 | Using index     |
    |  3 | DEPENDENT UNION    | tb1        | const | PRIMARY       | PRIMARY | 8       | const |    1 | Using index     |
    | NULL | UNION RESULT       | <union2,3> | ALL   | NULL          | NULL    | NULL    | NULL  | NULL | Using temporary |
    +----+--------------------+------------+-------+---------------+---------+---------+-------+------+-----------------+

    SUBQUERY:
    子查询中的第一个select其select_type为SUBQUERY。

    [yoon]> explain select * from tb1 where id = (select id from tb1 where id=7);
    +----+-------------+-------+-------+---------------+---------+---------+-------+------+-------------+
    | id | select_type | table | type  | possible_keys | key     | key_len | ref   | rows | Extra       |
    +----+-------------+-------+-------+---------------+---------+---------+-------+------+-------------+
    |  1 | PRIMARY     | tb1   | const | PRIMARY       | PRIMARY | 8       | const |    1 | NULL        |
    |  2 | SUBQUERY    | tb1   | const | PRIMARY       | PRIMARY | 8       | const |    1 | Using index |
    +----+-------------+-------+-------+---------------+---------+---------+-------+------+-------------+

    DERIVED:
    当子查询是from子句时,其select_type为DERIVED

    [yoon]> explain select * from (select id from tb1 where id=7) a;
    +----+-------------+------------+--------+---------------+---------+---------+-------+------+-------------+
    | 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     | tb1        | const  | PRIMARY       | PRIMARY | 8       | const |    1 | Using index |
    +----+-------------+------------+--------+---------------+---------+---------+-------+------+-------------+

    table:输出行所引用的表,也有可能是表的别名

    type:对表的访问类型
    常用的类型有: ALL、index、range、 ref、eq_ref、const、system、NULL(从左到右,性能从差到好)

    1.system:表仅有一行(=系统表)。这是const联接类型的一个特例。
    2.const:表最多有一个匹配行,它将在查询开始时被读取。因为仅有一行,在这行的列值可被优化器剩余部分认为是常数。const表很快,因为它们只读取一次!
    3.eq_ref:对于每个来自于前面的表的行组合,从该表中读取一行。这可能是最好的联接类型,除了const类型。
    4.ref:对于每个来自于前面的表的行组合,所有有匹配索引值的行将从这张表中读取。
    5.ref_or_null:该联接类型如同ref,但是添加了MySQL可以专门搜索包含NULL值的行。
    6.index_merge:该联接类型表示使用了索引合并优化方法。
    7.unique_subquery:该类型替换了下面形式的IN子查询的ref: value IN (SELECT primary_key FROM single_table WHERE some_expr) unique_subquery是一个索引查找函数,可以完全替换子查询,效率更高。
    8.index_subquery:该联接类型类似于unique_subquery。可以替换IN子查询,但只适合下列形式的子查询中的非唯一索引: value IN (SELECT key_column FROM single_table WHERE some_expr)
    9.range:只检索给定范围的行,使用一个索引来选择行。
    10.index:该联接类型与ALL相同,除了只有索引树被扫描。这通常比ALL快,因为索引文件通常比数据文件小。
    11.ALL:对于每个来自于先前的表的行组合,进行完整的表扫描。
    12.NULL: MySQL在优化过程中分解语句,执行时甚至不用访问表或索引,例如从一个索引列里选取最小值可以通过单独索引查找完成

    type的说明:
    1.system,const
    第一行的type就是为system,第二行是const,这两种联接类型是最快的。

    2.eq_ref
    在t_order表中的order_id是主键,t_order_ext表中的order_id也是主键,该表可以认为是订单表的补充信息表,他们的关系是1对1,在下面的例子中可以看到b表的连接类型是eq_ref,这是极快的联接类型.

    [yoon]> explain select * from t_order a,t_order_ext b where a.order_id=b.order_id; 
    +----+-------------+-------+--------+---------------+---------+---------+-----------------+------+-------------+ 
    | id | select_type | table | type   | possible_keys | key     | key_len | ref             | rows | Extra       | 
    +----+-------------+-------+--------+---------------+---------+---------+-----------------+------+-------------+ 
    |  1 | SIMPLE      | b     | ALL    | order_id      | NULL    | NULL    | NULL            |    1 |             | 
    |  1 | SIMPLE      | a     | eq_ref | PRIMARY       | PRIMARY | 4       | test.b.order_id |    1 | Using where | 
    +----+-------------+-------+--------+---------------+---------+---------+-----------------+------+-------------+ 

    3.ref
    下面的例子在上面的例子上略作了修改,加上了条件。此时b表的联接类型变成了ref。因为所有与a表中order_id=100的匹配记录都将会从b表获取。这是比较常见的联接类型。

    [yoon]> explain select * from t_order a,t_order_ext b where a.order_id=b.order_id and a.order_id=100; 
    +----+-------------+-------+-------+---------------+----------+---------+-------+------+-------+ 
    | id | select_type | table | type  | possible_keys | key      | key_len | ref   | rows | Extra | 
    +----+-------------+-------+-------+---------------+----------+---------+-------+------+-------+ 
    |  1 | SIMPLE      | a     | const | PRIMARY       | PRIMARY  | 4       | const |    1 |       | 
    |  1 | SIMPLE      | b     | ref   | order_id      | order_id | 4       | const |    1 |       | 
    +----+-------------+-------+-------+---------------+----------+---------+-------+------+-------+ 

    4.ref_or_null
    user_id字段是一个可以为空的字段,并对该字段创建了一个索引。在下面的查询中可以看到联接类型为ref_or_null,这是mysql为含有null的字段专门做的处理。在我们的表设计中应当尽量避免索引字段为NULL,因为这会额外的耗费mysql的处理时间来做优化。

    [yoon]> explain select * from t_order where user_id=100 or user_id is null; 
    +----+-------------+---------+-------------+---------------+---------+---------+-------+-------+-------------+ 
    | id | select_type | table   | type        | possible_keys | key     | key_len | ref   | rows  | Extra       | 
    +----+-------------+---------+-------------+---------------+---------+---------+-------+-------+-------------+ 
    |  1 | SIMPLE      | t_order | ref_or_null | user_id       | user_id | 5       | const | 50325 | Using where | 
    +----+-------------+---------+-------------+---------------+---------+---------+-------+-------+-------------+ 

    5.index_merge
    经常出现在使用一张表中的多个索引时。mysql会将多个索引合并在一起,如下例:

    [yoon]> explain select * from t_order where order_id=100 or user_id=10; 
    +----+-------------+---------+-------------+-----------------+-----------------+---------+------+------+-------------------------------------------+ 
    | id | select_type | table   | type        | possible_keys   | key             | key_len | ref  | rows | Extra                                     | 
    +----+-------------+---------+-------------+-----------------+-----------------+---------+------+------+-------------------------------------------+ 
    |  1 | SIMPLE      | t_order | index_merge | PRIMARY,user_id | PRIMARY,user_id | 4,5     | NULL |    2 | Using union(PRIMARY,user_id); Using where | 
    +----+-------------+---------+-------------+-----------------+-----------------+---------+------+------+-------------------------------------------+ 

    6.unique_subquery
    该联接类型用于替换value IN (SELECT primary_key FROM single_table WHERE some_expr)这样的子查询的ref。注意ref列,其中第二行显示的是func,表明unique_subquery是一个函数,而不是一个普通的ref。

    [yoon]> explain select * from t_order where order_id in (select order_id from t_order where user_id=10); 
    +----+--------------------+---------+-----------------+-----------------+---------+---------+------+--------+-------------+ 
    | id | select_type        | table   | type            | possible_keys   | key     | key_len | ref  | rows   | Extra       | 
    +----+--------------------+---------+-----------------+-----------------+---------+---------+------+--------+-------------+ 
    |  1 | PRIMARY            | t_order | ALL             | NULL            | NULL    | NULL    | NULL | 100649 | Using where | 
    |  2 | DEPENDENT SUBQUERY | t_order | unique_subquery | PRIMARY,user_id | PRIMARY | 4       | func |      1 | Using where | 
    +----+--------------------+---------+-----------------+-----------------+---------+---------+------+--------+-------------+ 

    7.index_subquery
    该联接类型与上面的太像了,唯一的差别就是子查询查的不是主键而是非唯一索引。

    [yoon]> explain select * from t_order where user_id in (select user_id from t_order where order_id>10); 
    +----+--------------------+---------+----------------+-----------------+---------+---------+------+--------+--------------------------+ 
    | id | select_type        | table   | type           | possible_keys   | key     | key_len | ref  | rows   | Extra                    | 
    +----+--------------------+---------+----------------+-----------------+---------+---------+------+--------+--------------------------+ 
    |  1 | PRIMARY            | t_order | ALL            | NULL            | NULL    | NULL    | NULL | 100649 | Using where              | 
    |  2 | DEPENDENT SUBQUERY | t_order | index_subquery | PRIMARY,user_id | user_id | 5       | func |  50324 | Using index; Using where | 
    +----+--------------------+---------+----------------+-----------------+---------+---------+------+--------+--------------------------+ 

    8.range
    按指定的范围进行检索,很常见。

    [yoon]> explain select * from t_order where user_id in (100,200,300); 
    +----+-------------+---------+-------+---------------+---------+---------+------+------+-------------+ 
    | id | select_type | table   | type  | possible_keys | key     | key_len | ref  | rows | Extra       | 
    +----+-------------+---------+-------+---------------+---------+---------+------+------+-------------+ 
    |  1 | SIMPLE      | t_order | range | user_id       | user_id | 5       | NULL |    3 | Using where | 
    +----+-------------+---------+-------+---------------+---------+---------+------+------+-------------+ 

    9.index
    在进行统计时非常常见,此联接类型实际上会扫描索引树,仅比ALL快些。

    [yoon]> explain select count(*) from t_order; 
    +----+-------------+---------+-------+---------------+---------+---------+------+--------+-------------+ 
    | id | select_type | table   | type  | possible_keys | key     | key_len | ref  | rows   | Extra       | 
    +----+-------------+---------+-------+---------------+---------+---------+------+--------+-------------+ 
    |  1 | SIMPLE      | t_order | index | NULL          | user_id | 5       | NULL | 100649 | Using index | 
    +----+-------------+---------+-------+---------------+---------+---------+------+--------+-------------+ 

    10.ALL
    完整的扫描全表,最慢的联接类型,尽可能的避免。

    [yoon]> explain select * from t_order; 
    +----+-------------+---------+------+---------------+------+---------+------+--------+-------+ 
    | id | select_type | table   | type | possible_keys | key  | key_len | ref  | rows   | Extra | 
    +----+-------------+---------+------+---------------+------+---------+------+--------+-------+ 
    |  1 | SIMPLE      | t_order | ALL  | NULL          | NULL | NULL    | NULL | 100649 |       | 
    +----+-------------+---------+------+---------------+------+---------+------+--------+-------+ 

    possible_keys
    指出MySQL能使用哪个索引在表中找到记录,查询涉及到的字段上若存在索引,则该索引将被列出,但不一定被查询使用(该查询可以利用的索引,如果没有任何索引显示 null)

    Key
    key列显示MySQL实际决定使用的键(索引),必然包含在possible_keys中

    如果没有选择索引,键是NULL。要想强制MySQL使用或忽视possible_keys列中的索引,在查询中使用FORCE INDEX、USE INDEX或者IGNORE INDEX。

    key_len
    表示索引中使用的字节数,可通过该列计算查询中使用的索引的长度(key_len显示的值为索引字段的最大可能长度,并非实际使用长度,即key_len是根据表定义计算而得,不是通过表内检索出的)

    不损失精确性的情况下,长度越短越好

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

    rows
    显示MySQL认为它执行查询时检查的行数。多行之间的同组数据相乘可以估算要处理的行数,不同组的相加

    Extra
    该列包含MySQL解决查询的详细信息

    Distinct:MySQL发现第1个匹配行后,停止为当前的行组合搜索更多的行。
    Not exists:MySQL能够对查询进行LEFT JOIN优化,发现1个匹配LEFT JOIN标准的行后,不再为前面的的行组合在该表内检查更多的行。
    range checked for each record (index map: #):MySQL没有发现好的可以使用的索引,但发现如果来自前面的表的列值已知,可能部分索引可以使用。
    Using filesort:MySQL需要额外的一次传递,以找出如何按排序顺序检索行。
    Using index:从只使用索引树中的信息而不需要进一步搜索读取实际的行来检索表中的列信息。
    Using temporary:为了解决查询,MySQL需要创建一个临时表来容纳结果。
    Using where:WHERE 子句用于限制哪一个行匹配下一个表或发送到客户。
    Using sort_union(...), Using union(...), Using intersect(...):这些函数说明如何为index_merge联接类型合并索引扫描。
    Using index for group-by:类似于访问表的Using index方式,Using index for group-by表示MySQL发现了一个索引,可以用来查 询GROUP BY或DISTINCT查询的所有列,而不要额外搜索硬盘访问实际的表。

    extra的说明

    1.Distinct

    MySQL发现第1个匹配行后,停止为当前的行组合搜索更多的行。对于此项没有找到合适的例子,求指点。

    2.Not exists

    因为b表中的order_id是主键,不可能为NULL,所以mysql在用a表的order_id扫描t_order表,并查找b表的行时,如果在b表发现一个匹配的行就不再继续扫描b了,因为b表中的order_id字段不可能为NULL。这样避免了对b表的多次扫描。

    mysql> explain select count(1) from t_order a left join t_order_ext b on a.order_id=b.order_id where b.order_id is null;  
    +----+-------------+-------+-------+---------------+--------------+---------+-----------------+--------+--------------------------------------+ 
    | id | select_type | table | type  | possible_keys | key          | key_len | ref             | rows   | Extra                                | 
    +----+-------------+-------+-------+---------------+--------------+---------+-----------------+--------+--------------------------------------+ 
    |  1 | SIMPLE      | a     | index | NULL          | express_type | 1       | NULL            | 100395 | Using index                          | 
    |  1 | SIMPLE      | b     | ref   | order_id      | order_id     | 4       | test.a.order_id |      1 | Using where; Using index; Not exists | 
    +----+-------------+-------+-------+---------------+--------------+---------+-----------------+--------+--------------------------------------+ 

    3.Range checked for each record

    这种情况是mysql没有发现好的索引可用,速度比没有索引要快得多。

    mysql> explain select * from t_order t, t_order_ext s where s.order_id>=t.order_id and s.order_id<=t.order_id and t.express_type>5; 
    +----+-------------+-------+-------+----------------------+--------------+---------+------+------+------------------------------------------------+ 
    | id | select_type | table | type  | possible_keys        | key          | key_len | ref  | rows | Extra                                          | 
    +----+-------------+-------+-------+----------------------+--------------+---------+------+------+------------------------------------------------+ 
    |  1 | SIMPLE      | t     | range | PRIMARY,express_type | express_type | 1       | NULL |    1 | Using where                                    | 
    |  1 | SIMPLE      | s     | ALL   | order_id             | NULL         | NULL    | NULL |    1 | Range checked for each record (index map: 0x1) | 
    +----+-------------+-------+-------+----------------------+--------------+---------+------+------+------------------------------------------------+ 

    4.Using filesort

    在有排序子句的情况下很常见的一种情况。此时mysql会根据联接类型浏览所有符合条件的记录,并保存排序关键字和行指针,然后排序关键字并按顺序检索行。

    mysql> explain select * from t_order order by express_type; 
    +----+-------------+---------+------+---------------+------+---------+------+--------+----------------+ 
    | id | select_type | table   | type | possible_keys | key  | key_len | ref  | rows   | Extra          | 
    +----+-------------+---------+------+---------------+------+---------+------+--------+----------------+ 
    |  1 | SIMPLE      | t_order | ALL  | NULL          | NULL | NULL    | NULL | 100395 | Using filesort | 
    +----+-------------+---------+------+---------------+------+---------+------+--------+----------------+ 

    5.Using index

    这是性能很高的一种情况。当查询所需的数据可以直接从索引树中检索到时,就会出现。上面的例子中有很多这样的例子,不再多举例了。

    6.Using temporary

    发生这种情况一般都是需要进行优化的。mysql需要创建一张临时表用来处理此类查询。

    mysql> explain select * from t_order a left join t_order_ext b on a.order_id=b.order_id group by b.order_id; 
    +----+-------------+-------+------+---------------+----------+---------+-----------------+--------+---------------------------------+ 
    | id | select_type | table | type | possible_keys | key      | key_len | ref             | rows   | Extra                           | 
    +----+-------------+-------+------+---------------+----------+---------+-----------------+--------+---------------------------------+ 
    |  1 | SIMPLE      | a     | ALL  | NULL          | NULL     | NULL    | NULL            | 100395 | Using temporary; Using filesort | 
    |  1 | SIMPLE      | b     | ref  | order_id      | order_id | 4       | test.a.order_id |      1 |                                 | 
    +----+-------------+-------+------+---------------+----------+---------+-----------------+--------+---------------------------------+ 

    7.Using where

    当有where子句时,extra都会有说明。

    8.Using sort_union(...)/Using union(...)/Using intersect(...)

    下面的例子中user_id是一个检索范围,此时mysql会使用sort_union函数来进行索引的合并。而当user_id是一个固定值时,请参看上面type说明5.index_merge的例子,此时会使用union函数进行索引合并。

    mysql> explain select * from t_order where order_id=100 or user_id>10; 
    +----+-------------+---------+-------------+-----------------+-----------------+---------+------+------+------------------------------------------------+ 
    | id | select_type | table   | type        | possible_keys   | key             | key_len | ref  | rows | Extra                                          | 
    +----+-------------+---------+-------------+-----------------+-----------------+---------+------+------+------------------------------------------------+ 
    |  1 | SIMPLE      | t_order | index_merge | PRIMARY,user_id | user_id,PRIMARY | 5,4     | NULL |    2 | Using sort_union(user_id,PRIMARY); Using where | 
    +----+-------------+---------+-------------+-----------------+-----------------+---------+------+------+------------------------------------------------+ 

    9.对于Using intersect的例子可以参看下例,user_id与express_type发生了索引交叉合并。

    mysql> explain select * from t_order where express_type=1 and user_id=100; 
    +----+-------------+---------+-------------+----------------------+----------------------+---------+------+------+----------------------------------------------------+ 
    | id | select_type | table   | type        | possible_keys        | key                  | key_len | ref  | rows | Extra                                              | 
    +----+-------------+---------+-------------+----------------------+----------------------+---------+------+------+----------------------------------------------------+ 
    |  1 | SIMPLE      | t_order | index_merge | user_id,express_type | user_id,express_type | 5,1     | NULL |    1 | Using intersect(user_id,express_type); Using where | 
    +----+-------------+---------+-------------+----------------------+----------------------+---------+------+------+----------------------------------------------------+ 

    10.Using index for group-by

    表明可以在索引中找到分组所需的所有数据,不需要查询实际的表。

    mysql> explain select user_id from t_order group by user_id; 
    +----+-------------+---------+-------+---------------+---------+---------+------+------+--------------------------+ 
    | id | select_type | table   | type  | possible_keys | key     | key_len | ref  | rows | Extra                    | 
    +----+-------------+---------+-------+---------------+---------+---------+------+------+--------------------------+ 
    |  1 | SIMPLE      | t_order | range | NULL          | user_id | 5       | NULL |    3 | Using index for group-by | 
    +----+-------------+---------+-------+---------------+---------+---------+------+------+--------------------------+ 
  • 相关阅读:
    css3 动画+动画库
    垃圾回收 及 内存泄漏
    做菜体会
    微信小程序
    微信公众号2
    JavaScript 原生事件
    Bootstrap框架
    Less 和 Sass
    HTML标签总结
    表单元素
  • 原文地址:https://www.cnblogs.com/hankyoon/p/12656587.html
Copyright © 2011-2022 走看看