zoukankan      html  css  js  c++  java
  • mysql explain 命令讲解

    explian命令可以显示select语句的执行计划

    explain的结果中每行对应select语句中的一个表,输出结果中的顺序是按照语句处理表的顺序。 mysql使用嵌套循环来处理所有的join连接。 当使用了关键字extended后,explain可以查看到"show warnings"语句的内容,以及被过滤的列。

    关键字"extented"和"partitions"不能一起使用,在5.6.5之后,这两个关键字都不可以和"format"一起使用。

    mysql> explain select * from emp ,dept where emp.deptno=dept.deptno;
    +----+-------------+-------+--------+---------------+------+---------+------+------+-------+
    | id | select_type | table | type   | possible_keys | key  | key_len | ref  | rows | Extra |
    +----+-------------+-------+--------+---------------+------+---------+------+------+-------+
    |  1 | SIMPLE      | emp   | system | NULL          | NULL | NULL    | NULL |    1 |       |
    |  1 | SIMPLE      | dept  | system | NULL          | NULL | NULL    | NULL |    1 |       |
    +----+-------------+-------+--------+---------------+------+---------+------+------+-------+
    2 rows in set (0.00 sec)
    
    mysql> explain select * from dept,emp where emp.deptno=dept.deptno;
    +----+-------------+-------+--------+---------------+------+---------+------+------+-------+
    | id | select_type | table | type   | possible_keys | key  | key_len | ref  | rows | Extra |
    +----+-------------+-------+--------+---------------+------+---------+------+------+-------+
    |  1 | SIMPLE      | dept  | system | NULL          | NULL | NULL    | NULL |    1 |       |
    |  1 | SIMPLE      | emp   | system | NULL          | NULL | NULL    | NULL |    1 |       |
    +----+-------------+-------+--------+---------------+------+---------+------+------+-------+
    2 rows in set (0.00 sec)
    
    mysql> explain select * from (select * from ( select * from emp where id=1) a) b;
    +----+-------------+------------+--------+---------------+------+---------+------+------+-------+
    | id | select_type | table      | type   | possible_keys | key  | key_len | ref  | rows | Extra |
    +----+-------------+------------+--------+---------------+------+---------+------+------+-------+
    |  1 | PRIMARY     | <derived2> | system | NULL          | NULL | NULL    | NULL |    1 |       |
    |  2 | DERIVED     | <derived3> | system | NULL          | NULL | NULL    | NULL |    1 |       |
    |  3 | DERIVED     | emp        | system | NULL          | NULL | NULL    | NULL |    1 |       |
    +----+-------------+------------+--------+---------------+------+---------+------+------+-------+
    3 rows in set (0.01 sec)
    
    mysql>
    

    1.id (JSON name: select_id)
     select标识符,在查询中该值是顺序的数字。如果该行是其它行union的结果,该值可以为null。


    2.select_type (JSON name: none)

    select_type的取值列表

    取值 json name 说明
    simple   简单的select查询(没有union、没有子查询)
    primary   最外层的查询
    union   union中的第二个或后面的select语句
    dependent union dependent (true) union中的第二个或后面的select语句,对外查询有依赖
    union result union_result union的结果集
    subquery   子查询中的第一个查询
    dependent subquery dependent (true) 子查询中的第一个查询,对外层查询有依赖
    derived   派生表的select(from子句的子查询)
    materialized materialized_from_subquery 物化的子查询
    uncacheable subquery cacheable (false) 结果不能被缓存的子查询,外层查询需要使用的时候都要重新执行一次
    uncacheable union cacheable (false) union中的第二个或者后面的不能被缓存的子查询

    3.table (JSON name: table_name)  

    <unionM,N>:表示是M行和N行结果的union;<derivedN>:表示派生自N行的结果;<subqueryN>: 引用N行的物化的子查询

    4.partitions (JSON name: partitions)
     查询涉及的分区


    5.type (JSON name: access_type)
     join的类型

    6.possible_keys

    possible_keys列指出MySQL可以使用的索引。注意,该列完全独立于EXPLAIN输出所示的表的次序。这意味着在possible_keys中的某些键实际上不能按生成的表次序使用。

    如果该列是NULL,则没有相关的索引。在这种情况下,可以通过检查WHERE子句看是否它引用某些列或适合索引的列来提高你的查询性能。如果是这样,创造一个适当的索引并且再次用EXPLAIN检查查询

    7.key (JSON name: key)

    显示MySQL实际决定使用的键(索引)。如果没有选择索引,键是NULL。

    要想强制MySQL使用或不使用possible_keys列中的索引,在查询中使用FORCE INDEX、USE INDEX或者IGNORE INDEX。

    8.key_len (JSON name: key_length)

    key_len列显示MySQL决定使用的键长度。如果键是NULL,则长度为NULL。 使用的索引的长度。在不损失精确性的情况下,长度越短越好

    9.ref (JSON name: ref)

    ref列显示使用哪个列或常数与key一起从表中选择行。

    10.rows (JSON name: rows)

    rows列显示MySQL认为它执行查询时必须检查的行数。

    11.filtered (JSON name: filtered)

    表的行数的过滤的百分比

    12. Extra

    包含MySQL执行查询使用的其它信息:

    看到 Using filesort 和 Using temporary 的时候,查询就需要优化了。

  • 相关阅读:
    PHP 命名空间
    使用 htaccess 重写 url,隐藏查询字符串
    HTML 长文本换行
    Mac OS X 上的Apache配置
    无法debug断点跟踪JDK源代码——missing line number attributes的解决方法
    根据多条件删除还能这样写
    wm_concat()函数
    spring 事务-使用@Transactional 注解(事务隔离级别)
    spring 中常用的两种事务配置方式以及事务的传播性、隔离级别
    oracle 中SQL 语句开发语法 SELECT INTO含义
  • 原文地址:https://www.cnblogs.com/abclife/p/5530061.html
Copyright © 2011-2022 走看看