<dl id="vorq8"><menu id="vorq8"></menu></dl>

<sup id="vorq8"></sup>
<sup id="vorq8"></sup>
<sup id="vorq8"></sup>

<optgroup id="vorq8"><address id="vorq8"></address></optgroup>

    <sup id="vorq8"><menu id="vorq8"><small id="vorq8"></small></menu></sup>

    <div id="vorq8"><tr id="vorq8"><object id="vorq8"></object></tr></div>

    当前位置:  首页  >  PHP教程  >  PHP 应用  >  代码收藏

    mysqlexplain用法详解

    mysqlexplain可以用来分析你要查询的sql语句的各项参数值,下面我们有详细的实例有需要的可以看看。

    mysql explain 可以用来分析你要查询的sql语句的各项参数值,下面我们有详细的实例有需要的可以看看。

    代码如下

    EXPLAIN table == DESC table == SHOW COLUMNS FORM table

    EXPLAIN [EXTENDED|PARTITIONS] SELECT... --显示该语句将使用哪一个索引以及?#38382;?#36827;行多表查询与使用到的表顺序

    代码如下

    > EXPLAIN SELECT * FROM BOOKS WHERE BOOK_ID=1;

    +----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+

    | id | _type | table | type | possible_keys | key | key_len | ref | rows | Extra |

    +----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+

    | 1 | SIMPLE | BOOKS | const | PRIMARY | PRIMARY | 4 | const | 1 | |

    +----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+

    1 row in set (0.00 sec)


    POSSIBLE_KEYS字段列举出了用于查找数据的索引,而KEY字段指示我们实际上用到了XX索引。如果POSSIBLE_KEYS字段的值显示NULL,那么说明没有用到索引。


    SELECT_TYPE

    SIMPLE 指示简单SELECT语句,没有子查询或者UNION

    PRIMARY 当使用子查询时,这是主要的SELECT语句

    UNION 当使用子查询时,这是主要的SELECT语句

    DEPENDENT UNION 当使用UNION时,这并不是第一个SELECT语句,取决于主查询

    UNION RESULT UINON查询

    SUBQUERY 子查询中的第一个SELECT语句

    DEPENDENT SUBQUERY 子查询中的第一个SELECT语句,取决于主查询

    DERIVED 来自于子查询的表

    UNCACHEABLE SUBQUERY 指示子查询中的结果不能缓存,因此必须对主查询中的每一行重新评价

    UNCACHEABLE UNION 指示子查询的UNION中,结果不能缓存,因此必须对主查询中的每一行重新评价


    这是在官网上的说明

    代码如下
    EXPLAIN Syntax
    EXPLAIN [EXTENDED] SELECT select_options
    Or:
    EXPLAIN tbl_name

    The EXPLAIN statement can be used either as a way to obtain information about how MySQL executes a statement, or as a synonym for DESCRIBE:
    When you precede a SELECT statement with the keyword EXPLAIN, MySQL displays information from the optimizer about the query execution plan. That is, MySQL explains how it would process the statement, including information about how tables are joined and in which order. EXPLAIN EXTENDED can be used to provide additional information.


    例如:

    代码如下
    mysql> explain select * from (select * from ( select * from t3 where id=3952602) a) b;
    +----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+
    | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
    +----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+
    | 1 | PRIMARY | | system | NULL | NULL | NULL | NULL | 1 | |
    | 2 | DERIVED | | system | NULL | NULL | NULL | NULL | 1 | |
    | 3 | DERIVED | t3 | const | PRIMARY,idx_t3_id | PRIMARY | 4 | | 1 | |
    +----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+

    很显然这条SQL是从里向外的执行,就是从id=3 向上执行.

    2. select_type

    就是select类型,可以有以下几种

    (1) SIMPLE
    简单SELECT(不使用UNION或子查询等) 例如:

    代码如下
    mysql> explain select * from t3 where id=3952602;
    +----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------+
    | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
    +----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------+
    | 1 | SIMPLE | t3 | const | PRIMARY,idx_t3_id | PRIMARY | 4 | const | 1 | |
    +----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------+

    (2). PRIMARY

    我的理解是最外层的select.例如:

    代码如下

    mysql> explain select * from (select * from t3 where id=3952602) a ;
    +----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+
    | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
    +----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+
    | 1 | PRIMARY | | system | NULL | NULL | NULL | NULL | 1 | |
    | 2 | DERIVED | t3 | const | PRIMARY,idx_t3_id | PRIMARY | 4 | | 1 | |
    +----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+

    (3).UNION

    UNION中的第二个或后面的SELECT语句.例如

    代码如下
    mysql> explain select * from t3 where id=3952602 union all select * from t3 ;
    +----+--------------+------------+-------+-------------------+---------+---------+-------+------+-------+
    | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
    +----+--------------+------------+-------+-------------------+---------+---------+-------+------+-------+
    | 1 | PRIMARY | t3 | const | PRIMARY,idx_t3_id | PRIMARY | 4 | const | 1 | |
    | 2 | UNION | t3 | ALL | NULL | NULL | NULL | NULL | 1000 | |
    |NULL | UNION RESULT | | ALL | NULL | NULL | NULL | NULL | NULL | |
    +----+--------------+------------+-------+-------------------+---------+---------+-------+------+-------+

    (4).DEPENDENT UNION

    UNION中的第二个或后面的SELECT语句,取决于外面的查询

    代码如下

    mysql> explain select * from t3 where id in (select id from t3 where id=3952602 union all select id from t3) ;
    +----+--------------------+------------+--------+-------------------+---------+---------+-------+------+--------------------------+
    | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
    +----+--------------------+------------+--------+-------------------+---------+---------+-------+------+--------------------------+
    | 1 | PRIMARY | t3 | ALL | NULL | NULL | NULL | NULL | 1000 | Using where |
    | 2 | DEPENDENT SUBQUERY | t3 | const | PRIMARY,idx_t3_id | PRIMARY | 4 | const | 1 | Using index |
    | 3 | DEPENDENT UNION | t3 | eq_ref | PRIMARY,idx_t3_id | PRIMARY | 4 | func | 1 | Using where; Using index |
    |NULL | UNION RESULT | | ALL | NULL | NULL | NULL | NULL | NULL | |
    +----+--------------------+------------+--------+-------------------+---------+---------+-------+------+--------------------------+

    (4).UNION RESULT

    UNION的结果。

    代码如下

    mysql> explain select * from t3 where id=3952602 union all select * from t3 ;
    +----+--------------+------------+-------+-------------------+---------+---------+-------+------+-------+
    | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
    +----+--------------+------------+-------+-------------------+---------+---------+-------+------+-------+
    | 1 | PRIMARY | t3 | const | PRIMARY,idx_t3_id | PRIMARY | 4 | const | 1 | |
    | 2 | UNION | t3 | ALL | NULL | NULL | NULL | NULL | 1000 | |
    |NULL | UNION RESULT | | ALL | NULL | NULL | NULL | NULL | NULL | |
    +----+--------------+------------+-------+-------------------+---------+---------+-------+------+-------+

    (5).SUBQUERY

    子查询中的第一个SELECT.

    代码如下

    mysql> explain select * from t3 where id = (select id from t3 where id=3952602 ) ;
    +----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------------+
    | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
    +----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------------+
    | 1 | PRIMARY | t3 | const | PRIMARY,idx_t3_id | PRIMARY | 4 | const | 1 | |
    | 2 | SUBQUERY | t3 | const | PRIMARY,idx_t3_id | PRIMARY | 4 | | 1 | Using index |
    +----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------------+

    (6). DEPENDENT SUBQUERY

    子查询中的第一个SELECT,取决于外面的查询

    代码如下

    mysql> explain select id from t3 where id in (select id from t3 where id=3952602 ) ;
    +----+--------------------+-------+-------+-------------------+---------+---------+-------+------+--------------------------+
    | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
    +----+--------------------+-------+-------+-------------------+---------+---------+-------+------+--------------------------+
    | 1 | PRIMARY | t3 | index | NULL | PRIMARY | 4 | NULL | 1000 | Using where; Using index |
    | 2 | DEPENDENT SUBQUERY | t3 | const | PRIMARY,idx_t3_id | PRIMARY | 4 | const | 1 | Using index |
    +----+--------------------+-------+-------+-------------------+---------+---------+-------+------+--------------------------+


    (7).DERIVED

    派生表的SELECT(FROM子句的子查询)

    代码如下

    mysql> explain select * from (select * from t3 where id=3952602) a ;
    +----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+
    | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
    +----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+
    | 1 | PRIMARY | | system | NULL | NULL | NULL | NULL | 1 | |
    | 2 | DERIVED | t3 | const | PRIMARY,idx_t3_id | PRIMARY | 4 | | 1 | |
    +----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+


    3.table

    显示这一行的数据是关于哪张表的.
    有时不是真实的表名字,看到的是derivedx(x是个数字,我的理解是第几步执行的结果)

    代码如下
    mysql> explain select * from (select * from ( select * from t3 where id=3952602) a) b;
    +----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+
    | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
    +----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+
    | 1 | PRIMARY | | system | NULL | NULL | NULL | NULL | 1 | |
    | 2 | DERIVED | | system | NULL | NULL | NULL | NULL | 1 | |
    | 3 | DERIVED | t3 | const | PRIMARY,idx_t3_id | PRIMARY | 4 | | 1 | |

    +----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+

    4.type

    这列很重要,显示了连接使用了哪种类别,有无使用索引.
    从最好到最差的连接类型为const、eq_reg、ref、range、indexhe和ALL

    (1).system

    这是const联接类型的一个特例。表仅有一?#26032;?#36275;条件.如下(t3表上的id是 primary key)

    代码如下

    mysql> explain select * from (select * from t3 where id=3952602) a ;
    +----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+
    | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
    +----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+
    | 1 | PRIMARY | | system | NULL | NULL | NULL | NULL | 1 | |
    | 2 | DERIVED | t3 | const | PRIMARY,idx_t3_id | PRIMARY | 4 | | 1 | |
    +----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+

    (2).const

    表最多有一个匹配行,它将在查询开始时被读取。因为仅有一行,在这行的列值可被优化器剩余部分认为是常数。const表很快,因为它们只读取一次!

    const用于用常数值比较PRIMARY KEY或UNIQUE索引的所有部分时。在下面的查询中,tbl_name可以用于const表:

    代码如下
    SELECT * from tbl_name WHERE primary_key=1;
    SELECT * from tbl_name WHERE primary_key_part1=1和 primary_key_part2=2;

    例如:

    代码如下
    mysql> explain select * from t3 where id=3952602;
    +----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------+
    | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
    +----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------+
    | 1 | SIMPLE | t3 | const | PRIMARY,idx_t3_id | PRIMARY | 4 | const | 1 | |
    +----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------+


    (3). eq_ref

    对于每个来自于前面的表的行组合,从该表中读取一?#23567;?#36825;可能是最好的联接类型,除了const类型。它用在一个索引的所有部分被联接使用并且索引是UNIQUE或PRIMARY KEY。

    eq_ref可以用于使用= 操作符比较的带索引的?#23567;?#27604;较值可以为常量或一个使用在该表前面所读取的表的列的表达式。

    在下面的例子中,MySQL可以使用eq_ref联接来处理ref_tables:

    代码如下

    SELECT * FROM ref_table,other_table
    WHERE ref_table.key_column=other_table.column;

    SELECT * FROM ref_table,other_table
    WHERE ref_table.key_column_part1=other_table.column
    AND ref_table.key_column_part2=1;

    例如

    代码如下

    mysql> create unique index idx_t3_id on t3(id) ;
    Query OK, 1000 rows affected (0.03 sec)
    Records: 1000 Duplicates: 0 Warnings: 0

    mysql> explain select * from t3,t4 where t3.id=t4.accountid;
    +----+-------------+-------+--------+-------------------+-----------+---------+----------------------+------+-------+
    | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
    +----+-------------+-------+--------+-------------------+-----------+---------+----------------------+------+-------+
    | 1 | SIMPLE | t4 | ALL | NULL | NULL | NULL | NULL | 1000 | |
    | 1 | SIMPLE | t3 | eq_ref | PRIMARY,idx_t3_id | idx_t3_id | 4 | dbatest.t4.accountid | 1 | |
    +----+-------------+-------+--------+-------------------+-----------+---------+----------------------+------+-------+

    (4).ref

    对于每个来自于前面的表的行组合,所有有匹配索引值的行将从这张表中读取。如果联接只使用键的最左边的前缀,或如果键不是UNIQUE或PRIMARY KEY(换句话说,如果联接不能基于关键字选择单个行的话),则使用ref。如果使用的键仅仅匹配少量行,该联接类型是不错的。

    ref可以用于使用=或<=>操作符的带索引的?#23567;?/p>

    在下面的例子中,MySQL可以使用ref联接来处理ref_tables:

    代码如下

    SELECT * FROM ref_table WHERE key_column=expr;

    SELECT * FROM ref_table,other_table
    WHERE ref_table.key_column=other_table.column;

    SELECT * FROM ref_table,other_table
    WHERE ref_table.key_column_part1=other_table.column
    AND ref_table.key_column_part2=1;

    例如:

    mysql> drop index idx_t3_id on t3;
    Query OK, 1000 rows affected (0.03 sec)
    Records: 1000 Duplicates: 0 Warnings: 0

    mysql> create index idx_t3_id on t3(id) ;
    Query OK, 1000 rows affected (0.04 sec)
    Records: 1000 Duplicates: 0 Warnings: 0

    mysql> explain select * from t3,t4 where t3.id=t4.accountid;
    +----+-------------+-------+------+-------------------+-----------+---------+----------------------+------+-------+
    | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
    +----+-------------+-------+------+-------------------+-----------+---------+----------------------+------+-------+
    | 1 | SIMPLE | t4 | ALL | NULL | NULL | NULL | NULL | 1000 | |
    | 1 | SIMPLE | t3 | ref | PRIMARY,idx_t3_id | idx_t3_id | 4 | dbatest.t4.accountid | 1 | |
    +----+-------------+-------+------+-------------------+-----------+---------+----------------------+------+-------+
    2 rows in set (0.00 sec)

    (5). ref_or_null

    该联接类型如同ref,但是添加了MySQL可以专门搜索包含NULL值的?#23567;?#22312;解决子查询中经常使用该联接类型的优化。

    在下面的例子中,MySQL可以使用ref_or_null联接来处理ref_tables:

    代码如下

    SELECT * FROM ref_table
    WHERE key_column=expr OR key_column IS NULL;

    (6). index_merge

    该联接类型表示使用了索引合并优化方法。在这种情况下,key列包含了使用的索引的清单,key_len包含了使用的索引的最长的关键元素。

    例如:

    代码如下
    mysql> explain select * from t4 where id=3952602 or accountid=31754306 ;
    +----+-------------+-------+-------------+----------------------------+----------------------------+---------+------+------+------------------------------------------------------+
    | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
    +----+-------------+-------+-------------+----------------------------+----------------------------+---------+------+------+------------------------------------------------------+
    | 1 | SIMPLE | t4 | index_merge | idx_t4_id,idx_t4_accountid | idx_t4_id,idx_t4_accountid | 4,4 | NULL | 2 | Using union(idx_t4_id,idx_t4_accountid); Using where |
    +----+-------------+-------+-------------+----------------------------+----------------------------+---------+------+------+------------------------------------------------------+
    1 row in set (0.00 sec)

    (7). unique_subquery

    该类型替换了下面?#38382;?#30340;IN子查询的ref:

    value IN (SELECT primary_key FROM single_table WHERE some_expr)
    unique_subquery是一个索引查找函数,可以完全替换子查询,效率更高。

    (8).index_subquery

    该联接类型类似于unique_subquery。可以替换IN子查询,但只适合下?#34892;问?#30340;子查询中的非唯一索引:

    代码如下

    value IN (SELECT key_column FROM single_table WHERE some_expr)

    (9).range

    只检索给定?#27573;?#30340;行,使用一个索引来选择?#23567;ey列显示使用了哪个索引。key_len包含所使用索引的最长关键元素。在该类型中ref列为NULL。

    当使用=、<>、>、>=、<、<=、IS NULL、<=>、BETWEEN或者IN操作符,用常量比较关键字列时,可以使用range

    代码如下

    mysql> explain select * from t3 where id=3952602 or id=3952603 ;
    +----+-------------+-------+-------+-------------------+-----------+---------+------+------+-------------+
    | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
    +----+-------------+-------+-------+-------------------+-----------+---------+------+------+-------------+
    | 1 | SIMPLE | t3 | range | PRIMARY,idx_t3_id | idx_t3_id | 4 | NULL | 2 | Using where |
    +----+-------------+-------+-------+-------------------+-----------+---------+------+------+-------------+
    1 row in set (0.02 sec)

    吐了个 "CAO" !
    扫码关注 PHP1 官方微信号
    PHP1.CN | 中国最专业的PHP中文社区 | PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | PHP问答
    Copyright ? 1998 - 2020 PHP1.CN. All Rights Reserved PHP1.CN 第一PHP社区 版权所有
         
    pc蛋蛋
    <dl id="vorq8"><menu id="vorq8"></menu></dl>

    <sup id="vorq8"></sup>
    <sup id="vorq8"></sup>
    <sup id="vorq8"></sup>

    <optgroup id="vorq8"><address id="vorq8"></address></optgroup>

      <sup id="vorq8"><menu id="vorq8"><small id="vorq8"></small></menu></sup>

      <div id="vorq8"><tr id="vorq8"><object id="vorq8"></object></tr></div>

      <dl id="vorq8"><menu id="vorq8"></menu></dl>

      <sup id="vorq8"></sup>
      <sup id="vorq8"></sup>
      <sup id="vorq8"></sup>

      <optgroup id="vorq8"><address id="vorq8"></address></optgroup>

        <sup id="vorq8"><menu id="vorq8"><small id="vorq8"></small></menu></sup>

        <div id="vorq8"><tr id="vorq8"><object id="vorq8"></object></tr></div>

        吉林时时彩彩票平台 中央教育一台福彩开奖 广西快三8月17日 中彩网双色球首页 直通车 竞彩篮球大小分加时算不算 福彩快3 2000年辽宁35选7走势图 安徽十一选五专家推荐 河北开乐彩开奖结果 体彩江苏7位数24期预测 福建时时彩如何购买 吉林快三历史遗漏 顶尖高手两码中特 福彩中奖后