<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://jackxiang.com/post//</link>
<title><![CDATA[[转]Mysql Explain 详解]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[WEB2.0]]></category>
<pubDate>Tue, 09 Sep 2008 06:49:10 +0000</pubDate> 
<guid>http://jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	Mysql Explain 详解<br/><br/>mysql&gt; explain select relation.id as id, relation.infoid as infoid from relation left join information on relation.infoid=information.infoid where relation.owneruid = 1 &#92;G<br/>*************************** 1. row ***************************<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; id: 1<br/>&nbsp;&nbsp;select_type: SIMPLE<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;table: relation<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; type: ALL<br/>possible_keys: NULL<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;key: NULL<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;key_len: NULL<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ref: NULL<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rows: 50<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Extra: Using where<br/>*************************** 2. row ***************************<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; id: 1<br/>&nbsp;&nbsp;select_type: SIMPLE<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;table: information<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; type: eq_ref<br/>possible_keys: PRIMARY<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;key: PRIMARY<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;key_len: 4<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ref: public.relation.infoid<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rows: 1<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Extra: Using index<br/>2 rows in set (0.00 sec)<br/><br/><br/>一.语法<br/><br/>explain &lt; table_name &gt;<br/><br/>例如: explain select * from t3 where id=3952602;<br/><br/>二.explain输出解释<br/><br/>+----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------+<br/>&#124; id &#124; select_type &#124; table &#124; type&nbsp;&nbsp;&#124; possible_keys&nbsp;&nbsp;&nbsp;&nbsp; &#124; key&nbsp;&nbsp;&nbsp;&nbsp; &#124; key_len &#124; ref&nbsp;&nbsp; &#124; rows &#124; Extra &#124;<br/>+----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------+<br/><br/>1.id<br/>&nbsp;&nbsp;我的理解是SQL执行的顺利的标识,SQL从大到小的执行.<br/><br/>例如:<br/>mysql&gt; explain select * from (select * from ( select * from t3 where id=3952602) a) b;<br/>+----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+<br/>&#124; id &#124; select_type &#124; table&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124; type&nbsp;&nbsp; &#124; possible_keys&nbsp;&nbsp;&nbsp;&nbsp; &#124; key&nbsp;&nbsp;&nbsp;&nbsp; &#124; key_len &#124; ref&nbsp;&nbsp;&#124; rows &#124; Extra &#124;<br/>+----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+<br/>&#124;&nbsp;&nbsp;1 &#124; PRIMARY&nbsp;&nbsp;&nbsp;&nbsp; &#124; &lt;derived2&gt; &#124; system &#124; NULL&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124; NULL&nbsp;&nbsp;&nbsp;&nbsp;&#124; NULL&nbsp;&nbsp;&nbsp;&nbsp;&#124; NULL &#124;&nbsp;&nbsp;&nbsp;&nbsp;1 &#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124;<br/>&#124;&nbsp;&nbsp;2 &#124; DERIVED&nbsp;&nbsp;&nbsp;&nbsp; &#124; &lt;derived3&gt; &#124; system &#124; NULL&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124; NULL&nbsp;&nbsp;&nbsp;&nbsp;&#124; NULL&nbsp;&nbsp;&nbsp;&nbsp;&#124; NULL &#124;&nbsp;&nbsp;&nbsp;&nbsp;1 &#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124;<br/>&#124;&nbsp;&nbsp;3 &#124; DERIVED&nbsp;&nbsp;&nbsp;&nbsp; &#124; t3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124; const&nbsp;&nbsp;&#124; PRIMARY,idx_t3_id &#124; PRIMARY &#124; 4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124;&nbsp;&nbsp;&nbsp;&nbsp;1 &#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124;<br/>+----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+<br/><br/>很显然这条SQL是从里向外的执行,就是从id=3 向上执行.<br/><br/>2. select_type<br/><br/>就是select类型,可以有以下几种<br/><br/>(1) SIMPLE<br/>简单SELECT(不使用UNION或子查询等) 例如:<br/>mysql&gt; explain select * from t3 where id=3952602;<br/>+----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------+<br/>&#124; id &#124; select_type &#124; table &#124; type&nbsp;&nbsp;&#124; possible_keys&nbsp;&nbsp;&nbsp;&nbsp; &#124; key&nbsp;&nbsp;&nbsp;&nbsp; &#124; key_len &#124; ref&nbsp;&nbsp; &#124; rows &#124; Extra &#124;<br/>+----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------+<br/>&#124;&nbsp;&nbsp;1 &#124; SIMPLE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124; t3&nbsp;&nbsp;&nbsp;&nbsp;&#124; const &#124; PRIMARY,idx_t3_id &#124; PRIMARY &#124; 4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124; const &#124;&nbsp;&nbsp;&nbsp;&nbsp;1 &#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124;<br/>+----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------+<br/><br/>(2). PRIMARY<br/><br/>我的理解是最外层的select.例如:<br/><br/>mysql&gt; explain select * from (select * from t3 where id=3952602) a ;<br/>+----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+<br/>&#124; id &#124; select_type &#124; table&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124; type&nbsp;&nbsp; &#124; possible_keys&nbsp;&nbsp;&nbsp;&nbsp; &#124; key&nbsp;&nbsp;&nbsp;&nbsp; &#124; key_len &#124; ref&nbsp;&nbsp;&#124; rows &#124; Extra &#124;<br/>+----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+<br/>&#124;&nbsp;&nbsp;1 &#124; PRIMARY&nbsp;&nbsp;&nbsp;&nbsp; &#124; &lt;derived2&gt; &#124; system &#124; NULL&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124; NULL&nbsp;&nbsp;&nbsp;&nbsp;&#124; NULL&nbsp;&nbsp;&nbsp;&nbsp;&#124; NULL &#124;&nbsp;&nbsp;&nbsp;&nbsp;1 &#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124;<br/>&#124;&nbsp;&nbsp;2 &#124; DERIVED&nbsp;&nbsp;&nbsp;&nbsp; &#124; t3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124; const&nbsp;&nbsp;&#124; PRIMARY,idx_t3_id &#124; PRIMARY &#124; 4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124;&nbsp;&nbsp;&nbsp;&nbsp;1 &#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124;<br/>+----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+<br/><br/>(3).UNION<br/><br/>UNION中的第二个或后面的SELECT语句.例如<br/>mysql&gt; explain select * from t3 where id=3952602 union all select * from t3 ;<br/>+----+--------------+------------+-------+-------------------+---------+---------+-------+------+-------+<br/>&#124; id &#124; select_type&nbsp;&nbsp;&#124; table&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124; type&nbsp;&nbsp;&#124; possible_keys&nbsp;&nbsp;&nbsp;&nbsp; &#124; key&nbsp;&nbsp;&nbsp;&nbsp; &#124; key_len &#124; ref&nbsp;&nbsp; &#124; rows &#124; Extra &#124;<br/>+----+--------------+------------+-------+-------------------+---------+---------+-------+------+-------+<br/>&#124;&nbsp;&nbsp;1 &#124; PRIMARY&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124; t3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124; const &#124; PRIMARY,idx_t3_id &#124; PRIMARY &#124; 4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124; const &#124;&nbsp;&nbsp;&nbsp;&nbsp;1 &#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124;<br/>&#124;&nbsp;&nbsp;2 &#124; UNION&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124; t3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124; ALL&nbsp;&nbsp; &#124; NULL&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124; NULL&nbsp;&nbsp;&nbsp;&nbsp;&#124; NULL&nbsp;&nbsp;&nbsp;&nbsp;&#124; NULL&nbsp;&nbsp;&#124; 1000 &#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124;<br/>&#124;NULL &#124; UNION RESULT &#124; &lt;union1,2&gt; &#124; ALL&nbsp;&nbsp; &#124; NULL&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124; NULL&nbsp;&nbsp;&nbsp;&nbsp;&#124; NULL&nbsp;&nbsp;&nbsp;&nbsp;&#124; NULL&nbsp;&nbsp;&#124; NULL &#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124;<br/>+----+--------------+------------+-------+-------------------+---------+---------+-------+------+-------+<br/><br/>(4).DEPENDENT UNION<br/><br/>UNION中的第二个或后面的SELECT语句，取决于外面的查询<br/><br/>mysql&gt; explain select * from t3 where id in (select id from t3 where id=3952602 union all select id from t3)&nbsp;&nbsp;;<br/>+----+--------------------+------------+--------+-------------------+---------+---------+-------+------+--------------------------+<br/>&#124; id &#124; select_type&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124; table&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124; type&nbsp;&nbsp; &#124; possible_keys&nbsp;&nbsp;&nbsp;&nbsp; &#124; key&nbsp;&nbsp;&nbsp;&nbsp; &#124; key_len &#124; ref&nbsp;&nbsp; &#124; rows &#124; Extra&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124;<br/>+----+--------------------+------------+--------+-------------------+---------+---------+-------+------+--------------------------+<br/>&#124;&nbsp;&nbsp;1 &#124; PRIMARY&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124; t3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124; ALL&nbsp;&nbsp;&nbsp;&nbsp;&#124; NULL&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124; NULL&nbsp;&nbsp;&nbsp;&nbsp;&#124; NULL&nbsp;&nbsp;&nbsp;&nbsp;&#124; NULL&nbsp;&nbsp;&#124; 1000 &#124; Using where&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124;<br/>&#124;&nbsp;&nbsp;2 &#124; DEPENDENT SUBQUERY &#124; t3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124; const&nbsp;&nbsp;&#124; PRIMARY,idx_t3_id &#124; PRIMARY &#124; 4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124; const &#124;&nbsp;&nbsp;&nbsp;&nbsp;1 &#124; Using index&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124;<br/>&#124;&nbsp;&nbsp;3 &#124; DEPENDENT UNION&nbsp;&nbsp;&nbsp;&nbsp;&#124; t3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124; eq_ref &#124; PRIMARY,idx_t3_id &#124; PRIMARY &#124; 4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124; func&nbsp;&nbsp;&#124;&nbsp;&nbsp;&nbsp;&nbsp;1 &#124; Using where; Using index &#124;<br/>&#124;NULL &#124; UNION RESULT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124; &lt;union2,3&gt; &#124; ALL&nbsp;&nbsp;&nbsp;&nbsp;&#124; NULL&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124; NULL&nbsp;&nbsp;&nbsp;&nbsp;&#124; NULL&nbsp;&nbsp;&nbsp;&nbsp;&#124; NULL&nbsp;&nbsp;&#124; NULL &#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124;<br/>+----+--------------------+------------+--------+-------------------+---------+---------+-------+------+--------------------------+<br/><br/>(4).UNION RESULT<br/><br/>UNION的结果。<br/><br/>mysql&gt; explain select * from t3 where id=3952602 union all select * from t3 ;<br/>+----+--------------+------------+-------+-------------------+---------+---------+-------+------+-------+<br/>&#124; id &#124; select_type&nbsp;&nbsp;&#124; table&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124; type&nbsp;&nbsp;&#124; possible_keys&nbsp;&nbsp;&nbsp;&nbsp; &#124; key&nbsp;&nbsp;&nbsp;&nbsp; &#124; key_len &#124; ref&nbsp;&nbsp; &#124; rows &#124; Extra &#124;<br/>+----+--------------+------------+-------+-------------------+---------+---------+-------+------+-------+<br/>&#124;&nbsp;&nbsp;1 &#124; PRIMARY&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124; t3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124; const &#124; PRIMARY,idx_t3_id &#124; PRIMARY &#124; 4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124; const &#124;&nbsp;&nbsp;&nbsp;&nbsp;1 &#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124;<br/>&#124;&nbsp;&nbsp;2 &#124; UNION&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124; t3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124; ALL&nbsp;&nbsp; &#124; NULL&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124; NULL&nbsp;&nbsp;&nbsp;&nbsp;&#124; NULL&nbsp;&nbsp;&nbsp;&nbsp;&#124; NULL&nbsp;&nbsp;&#124; 1000 &#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124;<br/>&#124;NULL &#124; UNION RESULT &#124; &lt;union1,2&gt; &#124; ALL&nbsp;&nbsp; &#124; NULL&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124; NULL&nbsp;&nbsp;&nbsp;&nbsp;&#124; NULL&nbsp;&nbsp;&nbsp;&nbsp;&#124; NULL&nbsp;&nbsp;&#124; NULL &#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124;<br/>+----+--------------+------------+-------+-------------------+---------+---------+-------+------+-------+<br/><br/>(5).SUBQUERY<br/><br/>子查询中的第一个SELECT.<br/><br/>mysql&gt; explain select * from t3 where id = (select id from t3 where id=3952602 )&nbsp;&nbsp;;<br/>+----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------------+<br/>&#124; id &#124; select_type &#124; table &#124; type&nbsp;&nbsp;&#124; possible_keys&nbsp;&nbsp;&nbsp;&nbsp; &#124; key&nbsp;&nbsp;&nbsp;&nbsp; &#124; key_len &#124; ref&nbsp;&nbsp; &#124; rows &#124; Extra&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124;<br/>+----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------------+<br/>&#124;&nbsp;&nbsp;1 &#124; PRIMARY&nbsp;&nbsp;&nbsp;&nbsp; &#124; t3&nbsp;&nbsp;&nbsp;&nbsp;&#124; const &#124; PRIMARY,idx_t3_id &#124; PRIMARY &#124; 4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124; const &#124;&nbsp;&nbsp;&nbsp;&nbsp;1 &#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124;<br/>&#124;&nbsp;&nbsp;2 &#124; SUBQUERY&nbsp;&nbsp;&nbsp;&nbsp;&#124; t3&nbsp;&nbsp;&nbsp;&nbsp;&#124; const &#124; PRIMARY,idx_t3_id &#124; PRIMARY &#124; 4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124;&nbsp;&nbsp;&nbsp;&nbsp;1 &#124; Using index &#124;<br/>+----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------------+<br/><br/>(6).&nbsp;&nbsp;DEPENDENT SUBQUERY<br/><br/>子查询中的第一个SELECT，取决于外面的查询<br/><br/>mysql&gt; explain select id from t3 where id in (select id from t3 where id=3952602 )&nbsp;&nbsp;;<br/>+----+--------------------+-------+-------+-------------------+---------+---------+-------+------+--------------------------+<br/>&#124; id &#124; select_type&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124; table &#124; type&nbsp;&nbsp;&#124; possible_keys&nbsp;&nbsp;&nbsp;&nbsp; &#124; key&nbsp;&nbsp;&nbsp;&nbsp; &#124; key_len &#124; ref&nbsp;&nbsp; &#124; rows &#124; Extra&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124;<br/>+----+--------------------+-------+-------+-------------------+---------+---------+-------+------+--------------------------+<br/>&#124;&nbsp;&nbsp;1 &#124; PRIMARY&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124; t3&nbsp;&nbsp;&nbsp;&nbsp;&#124; index &#124; NULL&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124; PRIMARY &#124; 4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124; NULL&nbsp;&nbsp;&#124; 1000 &#124; Using where; Using index &#124;<br/>&#124;&nbsp;&nbsp;2 &#124; DEPENDENT SUBQUERY &#124; t3&nbsp;&nbsp;&nbsp;&nbsp;&#124; const &#124; PRIMARY,idx_t3_id &#124; PRIMARY &#124; 4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124; const &#124;&nbsp;&nbsp;&nbsp;&nbsp;1 &#124; Using index&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124;<br/>+----+--------------------+-------+-------+-------------------+---------+---------+-------+------+--------------------------+<br/><br/><br/>(7).DERIVED<br/><br/>派生表的SELECT(FROM子句的子查询)<br/><br/>mysql&gt; explain select * from (select * from t3 where id=3952602) a ;<br/>+----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+<br/>&#124; id &#124; select_type &#124; table&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124; type&nbsp;&nbsp; &#124; possible_keys&nbsp;&nbsp;&nbsp;&nbsp; &#124; key&nbsp;&nbsp;&nbsp;&nbsp; &#124; key_len &#124; ref&nbsp;&nbsp;&#124; rows &#124; Extra &#124;<br/>+----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+<br/>&#124;&nbsp;&nbsp;1 &#124; PRIMARY&nbsp;&nbsp;&nbsp;&nbsp; &#124; &lt;derived2&gt; &#124; system &#124; NULL&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124; NULL&nbsp;&nbsp;&nbsp;&nbsp;&#124; NULL&nbsp;&nbsp;&nbsp;&nbsp;&#124; NULL &#124;&nbsp;&nbsp;&nbsp;&nbsp;1 &#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124;<br/>&#124;&nbsp;&nbsp;2 &#124; DERIVED&nbsp;&nbsp;&nbsp;&nbsp; &#124; t3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124; const&nbsp;&nbsp;&#124; PRIMARY,idx_t3_id &#124; PRIMARY &#124; 4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124;&nbsp;&nbsp;&nbsp;&nbsp;1 &#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124;<br/>+----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+<br/><br/><br/>3.table<br/><br/>显示这一行的数据是关于哪张表的.<br/>有时不是真实的表名字,看到的是derivedx(x是个数字,我的理解是第几步执行的结果)<br/><br/>mysql&gt; explain select * from (select * from ( select * from t3 where id=3952602) a) b;<br/>+----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+<br/>&#124; id &#124; select_type &#124; table&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124; type&nbsp;&nbsp; &#124; possible_keys&nbsp;&nbsp;&nbsp;&nbsp; &#124; key&nbsp;&nbsp;&nbsp;&nbsp; &#124; key_len &#124; ref&nbsp;&nbsp;&#124; rows &#124; Extra &#124;<br/>+----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+<br/>&#124;&nbsp;&nbsp;1 &#124; PRIMARY&nbsp;&nbsp;&nbsp;&nbsp; &#124; &lt;derived2&gt; &#124; system &#124; NULL&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124; NULL&nbsp;&nbsp;&nbsp;&nbsp;&#124; NULL&nbsp;&nbsp;&nbsp;&nbsp;&#124; NULL &#124;&nbsp;&nbsp;&nbsp;&nbsp;1 &#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124;<br/>&#124;&nbsp;&nbsp;2 &#124; DERIVED&nbsp;&nbsp;&nbsp;&nbsp; &#124; &lt;derived3&gt; &#124; system &#124; NULL&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124; NULL&nbsp;&nbsp;&nbsp;&nbsp;&#124; NULL&nbsp;&nbsp;&nbsp;&nbsp;&#124; NULL &#124;&nbsp;&nbsp;&nbsp;&nbsp;1 &#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124;<br/>&#124;&nbsp;&nbsp;3 &#124; DERIVED&nbsp;&nbsp;&nbsp;&nbsp; &#124; t3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124; const&nbsp;&nbsp;&#124; PRIMARY,idx_t3_id &#124; PRIMARY &#124; 4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124;&nbsp;&nbsp;&nbsp;&nbsp;1 &#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124;<br/>+----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+<br/><br/>4.type<br/><br/>这列很重要,显示了连接使用了哪种类别,有无使用索引.<br/>从最好到最差的连接类型为const、eq_reg、ref、range、indexhe和ALL<br/><br/>(1).system<br/><br/>这是const联接类型的一个特例。表仅有一行满足条件.如下(t3表上的id是 primary key)<br/><br/>mysql&gt; explain select * from (select * from t3 where id=3952602) a ;<br/>+----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+<br/>&#124; id &#124; select_type &#124; table&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124; type&nbsp;&nbsp; &#124; possible_keys&nbsp;&nbsp;&nbsp;&nbsp; &#124; key&nbsp;&nbsp;&nbsp;&nbsp; &#124; key_len &#124; ref&nbsp;&nbsp;&#124; rows &#124; Extra &#124;<br/>+----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+<br/>&#124;&nbsp;&nbsp;1 &#124; PRIMARY&nbsp;&nbsp;&nbsp;&nbsp; &#124; &lt;derived2&gt; &#124; system &#124; NULL&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124; NULL&nbsp;&nbsp;&nbsp;&nbsp;&#124; NULL&nbsp;&nbsp;&nbsp;&nbsp;&#124; NULL &#124;&nbsp;&nbsp;&nbsp;&nbsp;1 &#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124;<br/>&#124;&nbsp;&nbsp;2 &#124; DERIVED&nbsp;&nbsp;&nbsp;&nbsp; &#124; t3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124; const&nbsp;&nbsp;&#124; PRIMARY,idx_t3_id &#124; PRIMARY &#124; 4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124;&nbsp;&nbsp;&nbsp;&nbsp;1 &#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124;<br/>+----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+<br/><br/>(2).const<br/><br/>表最多有一个匹配行，它将在查询开始时被读取。因为仅有一行，在这行的列值可被优化器剩余部分认为是常数。const表很快，因为它们只读取一次！<br/><br/>const用于用常数值比较PRIMARY KEY或UNIQUE索引的所有部分时。在下面的查询中，tbl_name可以用于const表：<br/>SELECT * from tbl_name WHERE primary_key=1；<br/>SELECT * from tbl_name WHERE primary_key_part1=1和 primary_key_part2=2；<br/><br/>例如:<br/>mysql&gt; explain select * from t3 where id=3952602;<br/>+----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------+<br/>&#124; id &#124; select_type &#124; table &#124; type&nbsp;&nbsp;&#124; possible_keys&nbsp;&nbsp;&nbsp;&nbsp; &#124; key&nbsp;&nbsp;&nbsp;&nbsp; &#124; key_len &#124; ref&nbsp;&nbsp; &#124; rows &#124; Extra &#124;<br/>+----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------+<br/>&#124;&nbsp;&nbsp;1 &#124; SIMPLE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124; t3&nbsp;&nbsp;&nbsp;&nbsp;&#124; const &#124; PRIMARY,idx_t3_id &#124; PRIMARY &#124; 4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124; const &#124;&nbsp;&nbsp;&nbsp;&nbsp;1 &#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124;<br/>+----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------+<br/><br/><br/>(3). eq_ref<br/><br/>对于每个来自于前面的表的行组合，从该表中读取一行。这可能是最好的联接类型，除了const类型。它用在一个索引的所有部分被联接使用并且索引是UNIQUE或PRIMARY KEY。<br/><br/>eq_ref可以用于使用= 操作符比较的带索引的列。比较值可以为常量或一个使用在该表前面所读取的表的列的表达式。<br/><br/>在下面的例子中，MySQL可以使用eq_ref联接来处理ref_tables：<br/><br/>SELECT * FROM ref_table,other_table<br/>&nbsp;&nbsp;WHERE ref_table.key_column=other_table.column;<br/><br/>SELECT * FROM ref_table,other_table<br/>&nbsp;&nbsp;WHERE ref_table.key_column_part1=other_table.column<br/>&nbsp;&nbsp;&nbsp;&nbsp;AND ref_table.key_column_part2=1;<br/><br/>例如<br/>mysql&gt; create unique index&nbsp;&nbsp;idx_t3_id on t3(id) ;<br/>Query OK, 1000 rows affected (0.03 sec)<br/>Records: 1000&nbsp;&nbsp;Duplicates: 0&nbsp;&nbsp;Warnings: 0<br/><br/>mysql&gt; explain select * from t3,t4 where t3.id=t4.accountid;<br/>+----+-------------+-------+--------+-------------------+-----------+---------+----------------------+------+-------+<br/>&#124; id &#124; select_type &#124; table &#124; type&nbsp;&nbsp; &#124; possible_keys&nbsp;&nbsp;&nbsp;&nbsp; &#124; key&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124; key_len &#124; ref&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124; rows &#124; Extra &#124;<br/>+----+-------------+-------+--------+-------------------+-----------+---------+----------------------+------+-------+<br/>&#124;&nbsp;&nbsp;1 &#124; SIMPLE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124; t4&nbsp;&nbsp;&nbsp;&nbsp;&#124; ALL&nbsp;&nbsp;&nbsp;&nbsp;&#124; NULL&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124; NULL&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124; NULL&nbsp;&nbsp;&nbsp;&nbsp;&#124; NULL&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124; 1000 &#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124;<br/>&#124;&nbsp;&nbsp;1 &#124; SIMPLE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124; t3&nbsp;&nbsp;&nbsp;&nbsp;&#124; eq_ref &#124; PRIMARY,idx_t3_id &#124; idx_t3_id &#124; 4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124; dbatest.t4.accountid &#124;&nbsp;&nbsp;&nbsp;&nbsp;1 &#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124;<br/>+----+-------------+-------+--------+-------------------+-----------+---------+----------------------+------+-------+<br/><br/>(4).ref<br/><br/>对于每个来自于前面的表的行组合，所有有匹配索引值的行将从这张表中读取。如果联接只使用键的最左边的前缀，或如果键不是UNIQUE或PRIMARY KEY（换句话说，如果联接不能基于关键字选择单个行的话），则使用ref。如果使用的键仅仅匹配少量行，该联接类型是不错的。<br/><br/>ref可以用于使用=或&lt;=&gt;操作符的带索引的列。<br/><br/>在下面的例子中，MySQL可以使用ref联接来处理ref_tables：<br/><br/>SELECT * FROM ref_table WHERE key_column=expr;<br/><br/>SELECT * FROM ref_table,other_table<br/>&nbsp;&nbsp;WHERE ref_table.key_column=other_table.column;<br/><br/>SELECT * FROM ref_table,other_table<br/>&nbsp;&nbsp;WHERE ref_table.key_column_part1=other_table.column<br/>&nbsp;&nbsp;&nbsp;&nbsp;AND ref_table.key_column_part2=1;<br/><br/>例如:<br/><br/>mysql&gt; drop index idx_t3_id on t3;<br/>Query OK, 1000 rows affected (0.03 sec)<br/>Records: 1000&nbsp;&nbsp;Duplicates: 0&nbsp;&nbsp;Warnings: 0<br/><br/>mysql&gt; create index idx_t3_id on t3(id) ;<br/>Query OK, 1000 rows affected (0.04 sec)<br/>Records: 1000&nbsp;&nbsp;Duplicates: 0&nbsp;&nbsp;Warnings: 0<br/><br/>mysql&gt; explain select * from t3,t4 where t3.id=t4.accountid;<br/>+----+-------------+-------+------+-------------------+-----------+---------+----------------------+------+-------+<br/>&#124; id &#124; select_type &#124; table &#124; type &#124; possible_keys&nbsp;&nbsp;&nbsp;&nbsp; &#124; key&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124; key_len &#124; ref&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124; rows &#124; Extra &#124;<br/>+----+-------------+-------+------+-------------------+-----------+---------+----------------------+------+-------+<br/>&#124;&nbsp;&nbsp;1 &#124; SIMPLE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124; t4&nbsp;&nbsp;&nbsp;&nbsp;&#124; ALL&nbsp;&nbsp;&#124; NULL&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124; NULL&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124; NULL&nbsp;&nbsp;&nbsp;&nbsp;&#124; NULL&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124; 1000 &#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124;<br/>&#124;&nbsp;&nbsp;1 &#124; SIMPLE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124; t3&nbsp;&nbsp;&nbsp;&nbsp;&#124; ref&nbsp;&nbsp;&#124; PRIMARY,idx_t3_id &#124; idx_t3_id &#124; 4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124; dbatest.t4.accountid &#124;&nbsp;&nbsp;&nbsp;&nbsp;1 &#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124;<br/>+----+-------------+-------+------+-------------------+-----------+---------+----------------------+------+-------+<br/>2 rows in set (0.00 sec)<br/><br/>(5).&nbsp;&nbsp;ref_or_null<br/><br/>该联接类型如同ref，但是添加了MySQL可以专门搜索包含NULL值的行。在解决子查询中经常使用该联接类型的优化。<br/><br/>在下面的例子中，MySQL可以使用ref_or_null联接来处理ref_tables：<br/><br/>SELECT * FROM ref_table<br/>WHERE key_column=expr OR key_column IS NULL;<br/><br/>(6). index_merge<br/><br/>该联接类型表示使用了索引合并优化方法。在这种情况下，key列包含了使用的索引的清单，key_len包含了使用的索引的最长的关键元素。<br/><br/>例如:<br/>mysql&gt; explain select * from t4 where id=3952602 or accountid=31754306 ;<br/>+----+-------------+-------+-------------+----------------------------+----------------------------+---------+------+------+------------------------------------------------------+<br/>&#124; id &#124; select_type &#124; table &#124; type&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124; possible_keys&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124; key&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124; key_len &#124; ref&nbsp;&nbsp;&#124; rows &#124; Extra&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124;<br/>+----+-------------+-------+-------------+----------------------------+----------------------------+---------+------+------+------------------------------------------------------+<br/>&#124;&nbsp;&nbsp;1 &#124; SIMPLE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124; t4&nbsp;&nbsp;&nbsp;&nbsp;&#124; index_merge &#124; idx_t4_id,idx_t4_accountid &#124; idx_t4_id,idx_t4_accountid &#124; 4,4&nbsp;&nbsp;&nbsp;&nbsp; &#124; NULL &#124;&nbsp;&nbsp;&nbsp;&nbsp;2 &#124; Using union(idx_t4_id,idx_t4_accountid); Using where &#124;<br/>+----+-------------+-------+-------------+----------------------------+----------------------------+---------+------+------+------------------------------------------------------+<br/>1 row in set (0.00 sec)<br/><br/>(7). unique_subquery<br/><br/>该类型替换了下面形式的IN子查询的ref：<br/><br/>value IN (SELECT primary_key FROM single_table WHERE some_expr)<br/>unique_subquery是一个索引查找函数，可以完全替换子查询，效率更高。<br/><br/>(8).index_subquery<br/><br/>该联接类型类似于unique_subquery。可以替换IN子查询，但只适合下列形式的子查询中的非唯一索引：<br/><br/>value IN (SELECT key_column FROM single_table WHERE some_expr)<br/><br/>(9).range<br/><br/>只检索给定范围的行，使用一个索引来选择行。key列显示使用了哪个索引。key_len包含所使用索引的最长关键元素。在该类型中ref列为NULL。<br/><br/>当使用=、&lt;&gt;、&gt;、&gt;=、&lt;、&lt;=、IS NULL、&lt;=&gt;、BETWEEN或者IN操作符，用常量比较关键字列时，可以使用range<br/><br/>mysql&gt; explain select * from t3 where id=3952602 or id=3952603 ;<br/>+----+-------------+-------+-------+-------------------+-----------+---------+------+------+-------------+<br/>&#124; id &#124; select_type &#124; table &#124; type&nbsp;&nbsp;&#124; possible_keys&nbsp;&nbsp;&nbsp;&nbsp; &#124; key&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124; key_len &#124; ref&nbsp;&nbsp;&#124; rows &#124; Extra&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124;<br/>+----+-------------+-------+-------+-------------------+-----------+---------+------+------+-------------+<br/>&#124;&nbsp;&nbsp;1 &#124; SIMPLE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124; t3&nbsp;&nbsp;&nbsp;&nbsp;&#124; range &#124; PRIMARY,idx_t3_id &#124; idx_t3_id &#124; 4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124; NULL &#124;&nbsp;&nbsp;&nbsp;&nbsp;2 &#124; Using where &#124;<br/>+----+-------------+-------+-------+-------------------+-----------+---------+------+------+-------------+<br/>1 row in set (0.02 sec)<br/><br/>(10).index<br/><br/>该联接类型与ALL相同，除了只有索引树被扫描。这通常比ALL快，因为索引文件通常比数据文件小。<br/><br/>当查询只使用作为单索引一部分的列时，MySQL可以使用该联接类型。<br/><br/>(11). ALL<br/><br/>对于每个来自于先前的表的行组合，进行完整的表扫描。如果表是第一个没标记const的表，这通常不好，并且通常在它情况下很差。通常可以增加更多的索引而不要使用ALL，使得行能基于前面的表中的常数值或列值被检索出。<br/><br/><br/>5.possible_keys<br/><br/>possible_keys列指出MySQL能使用哪个索引在该表中找到行。注意，该列完全独立于EXPLAIN输出所示的表的次序。这意味着在possible_keys中的某些键实际上不能按生成的表次序使用。<br/><br/>如果该列是NULL，则没有相关的索引。在这种情况下，可以通过检查WHERE子句看是否它引用某些列或适合索引的列来提高你的查询性能。如果是这样，创造一个适当的索引并且再次用EXPLAIN检查查询<br/><br/>6. key<br/><br/>key列显示MySQL实际决定使用的键（索引）。如果没有选择索引，键是NULL。要想强制MySQL使用或忽视possible_keys列中的索引，在查询中使用FORCE INDEX、USE INDEX或者IGNORE INDEX。<br/><br/>7.key_len<br/><br/>key_len列显示MySQL决定使用的键长度。如果键是NULL，则长度为NULL。<br/>使用的索引的长度。在不损失精确性的情况下，长度越短越好<br/><br/>8. ref<br/><br/>ref列显示使用哪个列或常数与key一起从表中选择行。<br/><br/>9. rows<br/><br/>rows列显示MySQL认为它执行查询时必须检查的行数。<br/><br/>10. Extra<br/><br/>该列包含MySQL解决查询的详细信息,下面详细.<br/><br/>(1).Distinct<br/>一旦MYSQL找到了与行相联合匹配的行，就不再搜索了<br/><br/>(2).Not exists<br/>MYSQL优化了LEFT JOIN，一旦它找到了匹配LEFT JOIN标准的行，<br/><br/>就不再搜索了<br/><br/>(3).Range checked for each<br/><br/>Record（index map:#）<br/>没有找到理想的索引，因此对于从前面表中来的每一个行组合，MYSQL检查使用哪个索引，并用它来从表中返回行。这是使用索引的最慢的连接之一<br/><br/>(4).Using filesort<br/>看到这个的时候，查询就需要优化了。MYSQL需要进行额外的步骤来发现如何对返回的行排序。它根据连接类型以及存储排序键值和匹配条件的全部行的行指针来排序全部行<br/><br/>(5).Using index<br/>列数据是从仅仅使用了索引中的信息而没有读取实际的行动的表返回的，这发生在对表的全部的请求列都是同一个索引的部分的时候<br/><br/>(6).Using temporary<br/>看到这个的时候，查询需要优化了。这里，MYSQL需要创建一个临时表来存储结果，这通常发生在对不同的列集进行ORDER BY上，而不是GROUP BY上<br/><br/>(7).Using where<br/>使用了WHERE从句来限制哪些行将与下一张表匹配或者是返回给用户。如果不想返回表中的全部行，并且连接类型ALL或index，这就会发生，或者是查询有问题<br/>
]]>
</description>
</item><item>
<link>http://jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] [转]Mysql Explain 详解]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>