{"id":267,"date":"2012-12-05T09:35:15","date_gmt":"2012-12-05T09:35:15","guid":{"rendered":"eda80a3d5b344bc40f3bc04f65b7a357"},"modified":"2012-12-05T09:35:15","modified_gmt":"2012-12-05T09:35:15","slug":"","status":"publish","type":"post","link":"https:\/\/www.xiaobo.li\/notes\/archives\/267","title":{"rendered":"Mysql Explain \u8be6\u89e3"},"content":{"rendered":"<div class=\"postText\">Mysql Explain \u8be6\u89e3<\/p>\n<p>\n\u4e00.\u8bed\u6cd5<\/p>\n<p>explain &lt; table_name &gt;<\/p>\n<p>\u4f8b\u5982: explain select * from t3 where id=3952602;<\/p>\n<p>\u4e8c.explain\u8f93\u51fa\u89e3\u91ca<\/p>\n<p>+----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------+<br \/>\n| id | select_type | table | type&nbsp;&nbsp;| possible_keys&nbsp; &nbsp;&nbsp;&nbsp;| key&nbsp; &nbsp;&nbsp;&nbsp;| key_len | ref&nbsp; &nbsp;| rows | Extra |<br \/>\n+----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------+<\/p>\n<p>1.id<br \/>\n&nbsp;&nbsp;\u6211\u7684\u7406\u89e3\u662fSQL\u6267\u884c\u7684\u987a\u5229\u7684\u6807\u8bc6,SQL\u4ece\u5927\u5230\u5c0f\u7684\u6267\u884c.<\/p>\n<p>\u4f8b\u5982:<br \/>\nmysql&gt; explain select * from (select * from ( select * from t3 where id=3952602) a) b;<br \/>\n+----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+<br \/>\n| id | select_type | table&nbsp; &nbsp;&nbsp; &nbsp;| type&nbsp; &nbsp;| possible_keys&nbsp; &nbsp;&nbsp;&nbsp;| key&nbsp; &nbsp;&nbsp;&nbsp;| key_len | ref&nbsp;&nbsp;| rows | Extra |<br \/>\n+----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+<br \/>\n|&nbsp;&nbsp;1 | PRIMARY&nbsp; &nbsp;&nbsp;&nbsp;| &lt;derived2&gt; | system | NULL&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;| NULL&nbsp; &nbsp; | NULL&nbsp; &nbsp; | NULL |&nbsp; &nbsp; 1 |&nbsp; &nbsp;&nbsp; &nbsp; |<br \/>\n|&nbsp;&nbsp;2 | DERIVED&nbsp; &nbsp;&nbsp;&nbsp;| &lt;derived3&gt; | system | NULL&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;| NULL&nbsp; &nbsp; | NULL&nbsp; &nbsp; | NULL |&nbsp; &nbsp; 1 |&nbsp; &nbsp;&nbsp; &nbsp; |<br \/>\n|&nbsp;&nbsp;3 | DERIVED&nbsp; &nbsp;&nbsp;&nbsp;| t3&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;| const&nbsp;&nbsp;| PRIMARY,idx_t3_id | PRIMARY | 4&nbsp; &nbsp;&nbsp; &nbsp; |&nbsp; &nbsp;&nbsp; &nbsp;|&nbsp; &nbsp; 1 |&nbsp; &nbsp;&nbsp; &nbsp; |<br \/>\n+----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+<\/p>\n<p>\u5f88\u663e\u7136\u8fd9\u6761SQL\u662f\u4ece\u91cc\u5411\u5916\u7684\u6267\u884c,\u5c31\u662f\u4eceid=3 \u5411\u4e0a\u6267\u884c.<\/p>\n<p>2. select_type<\/p>\n<p>\u5c31\u662fselect\u7c7b\u578b,\u53ef\u4ee5\u6709\u4ee5\u4e0b\u51e0\u79cd<\/p>\n<p>(1) SIMPLE<br \/>\n\u7b80\u5355SELECT(\u4e0d\u4f7f\u7528UNION\u6216\u5b50\u67e5\u8be2\u7b49) \u4f8b\u5982:<br \/>\nmysql&gt; explain select * from t3 where id=3952602;<br \/>\n+----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------+<br \/>\n| id | select_type | table | type&nbsp;&nbsp;| possible_keys&nbsp; &nbsp;&nbsp;&nbsp;| key&nbsp; &nbsp;&nbsp;&nbsp;| key_len | ref&nbsp; &nbsp;| rows | Extra |<br \/>\n+----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------+<br \/>\n|&nbsp;&nbsp;1 | SIMPLE&nbsp; &nbsp;&nbsp; &nbsp;| t3&nbsp; &nbsp; | const | PRIMARY,idx_t3_id | PRIMARY | 4&nbsp; &nbsp;&nbsp; &nbsp; | const |&nbsp; &nbsp; 1 |&nbsp; &nbsp;&nbsp; &nbsp; |<br \/>\n+----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------+<\/p>\n<p>(2). PRIMARY<\/p>\n<p>\u6211\u7684\u7406\u89e3\u662f\u6700\u5916\u5c42\u7684select.\u4f8b\u5982:<\/p>\n<p>mysql&gt; explain select * from (select * from t3 where id=3952602) a ;<br \/>\n+----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+<br \/>\n| id | select_type | table&nbsp; &nbsp;&nbsp; &nbsp;| type&nbsp; &nbsp;| possible_keys&nbsp; &nbsp;&nbsp;&nbsp;| key&nbsp; &nbsp;&nbsp;&nbsp;| key_len | ref&nbsp;&nbsp;| rows | Extra |<br \/>\n+----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+<br \/>\n|&nbsp;&nbsp;1 | PRIMARY&nbsp; &nbsp;&nbsp;&nbsp;| &lt;derived2&gt; | system | NULL&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;| NULL&nbsp; &nbsp; | NULL&nbsp; &nbsp; | NULL |&nbsp; &nbsp; 1 |&nbsp; &nbsp;&nbsp; &nbsp; |<br \/>\n|&nbsp;&nbsp;2 | DERIVED&nbsp; &nbsp;&nbsp;&nbsp;| t3&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;| const&nbsp;&nbsp;| PRIMARY,idx_t3_id | PRIMARY | 4&nbsp; &nbsp;&nbsp; &nbsp; |&nbsp; &nbsp;&nbsp; &nbsp;|&nbsp; &nbsp; 1 |&nbsp; &nbsp;&nbsp; &nbsp; |<br \/>\n+----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+<\/p>\n<p>(3).UNION<\/p>\n<p>UNION\u4e2d\u7684\u7b2c\u4e8c\u4e2a\u6216\u540e\u9762\u7684SELECT\u8bed\u53e5.\u4f8b\u5982<br \/>\nmysql&gt; explain select * from t3 where id=3952602 union all select * from t3 ;<br \/>\n+----+--------------+------------+-------+-------------------+---------+---------+-------+------+-------+<br \/>\n| id | select_type&nbsp;&nbsp;| table&nbsp; &nbsp;&nbsp; &nbsp;| type&nbsp;&nbsp;| possible_keys&nbsp; &nbsp;&nbsp;&nbsp;| key&nbsp; &nbsp;&nbsp;&nbsp;| key_len | ref&nbsp; &nbsp;| rows | Extra |<br \/>\n+----+--------------+------------+-------+-------------------+---------+---------+-------+------+-------+<br \/>\n|&nbsp;&nbsp;1 | PRIMARY&nbsp; &nbsp;&nbsp; &nbsp;| t3&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;| const | PRIMARY,idx_t3_id | PRIMARY | 4&nbsp; &nbsp;&nbsp; &nbsp; | const |&nbsp; &nbsp; 1 |&nbsp; &nbsp;&nbsp; &nbsp; |<br \/>\n|&nbsp;&nbsp;2 | UNION&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;| t3&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;| ALL&nbsp; &nbsp;| NULL&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;| NULL&nbsp; &nbsp; | NULL&nbsp; &nbsp; | NULL&nbsp;&nbsp;| 1000 |&nbsp; &nbsp;&nbsp; &nbsp; |<br \/>\n|NULL | UNION RESULT | &lt;union1,2&gt; | ALL&nbsp; &nbsp;| NULL&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;| NULL&nbsp; &nbsp; | NULL&nbsp; &nbsp; | NULL&nbsp;&nbsp;| NULL |&nbsp; &nbsp;&nbsp; &nbsp; |<br \/>\n+----+--------------+------------+-------+-------------------+---------+---------+-------+------+-------+<\/p>\n<p>(4).DEPENDENT UNION<\/p>\n<p>UNION\u4e2d\u7684\u7b2c\u4e8c\u4e2a\u6216\u540e\u9762\u7684SELECT\u8bed\u53e5\uff0c\u53d6\u51b3\u4e8e\u5916\u9762\u7684\u67e5\u8be2<\/p>\n<p>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 \/>\n+----+--------------------+------------+--------+-------------------+---------+---------+-------+------+--------------------------+<br \/>\n| id | select_type&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;| table&nbsp; &nbsp;&nbsp; &nbsp;| type&nbsp; &nbsp;| possible_keys&nbsp; &nbsp;&nbsp;&nbsp;| key&nbsp; &nbsp;&nbsp;&nbsp;| key_len | ref&nbsp; &nbsp;| rows | Extra&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;|<br \/>\n+----+--------------------+------------+--------+-------------------+---------+---------+-------+------+--------------------------+<br \/>\n|&nbsp;&nbsp;1 | PRIMARY&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;| t3&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;| ALL&nbsp; &nbsp; | NULL&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;| NULL&nbsp; &nbsp; | NULL&nbsp; &nbsp; | NULL&nbsp;&nbsp;| 1000 | Using where&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;|<br \/>\n|&nbsp;&nbsp;2 | DEPENDENT SUBQUERY | t3&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;| const&nbsp;&nbsp;| PRIMARY,idx_t3_id | PRIMARY | 4&nbsp; &nbsp;&nbsp; &nbsp; | const |&nbsp; &nbsp; 1 | Using index&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;|<br \/>\n|&nbsp;&nbsp;3 | DEPENDENT UNION&nbsp; &nbsp; | t3&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;| eq_ref | PRIMARY,idx_t3_id | PRIMARY | 4&nbsp; &nbsp;&nbsp; &nbsp; | func&nbsp;&nbsp;|&nbsp; &nbsp; 1 | Using where; Using index |<br \/>\n|NULL | UNION RESULT&nbsp; &nbsp;&nbsp; &nbsp; | &lt;union2,3&gt; | ALL&nbsp; &nbsp; | NULL&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;| NULL&nbsp; &nbsp; | NULL&nbsp; &nbsp; | NULL&nbsp;&nbsp;| NULL |&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;|<br \/>\n+----+--------------------+------------+--------+-------------------+---------+---------+-------+------+--------------------------+<\/p>\n<p>(4).UNION RESULT<\/p>\n<p>UNION\u7684\u7ed3\u679c\u3002<\/p>\n<p>mysql&gt; explain select * from t3 where id=3952602 union all select * from t3 ;<br \/>\n+----+--------------+------------+-------+-------------------+---------+---------+-------+------+-------+<br \/>\n| id | select_type&nbsp;&nbsp;| table&nbsp; &nbsp;&nbsp; &nbsp;| type&nbsp;&nbsp;| possible_keys&nbsp; &nbsp;&nbsp;&nbsp;| key&nbsp; &nbsp;&nbsp;&nbsp;| key_len | ref&nbsp; &nbsp;| rows | Extra |<br \/>\n+----+--------------+------------+-------+-------------------+---------+---------+-------+------+-------+<br \/>\n|&nbsp;&nbsp;1 | PRIMARY&nbsp; &nbsp;&nbsp; &nbsp;| t3&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;| const | PRIMARY,idx_t3_id | PRIMARY | 4&nbsp; &nbsp;&nbsp; &nbsp; | const |&nbsp; &nbsp; 1 |&nbsp; &nbsp;&nbsp; &nbsp; |<br \/>\n|&nbsp;&nbsp;2 | UNION&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;| t3&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;| ALL&nbsp; &nbsp;| NULL&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;| NULL&nbsp; &nbsp; | NULL&nbsp; &nbsp; | NULL&nbsp;&nbsp;| 1000 |&nbsp; &nbsp;&nbsp; &nbsp; |<br \/>\n|NULL | UNION RESULT | &lt;union1,2&gt; | ALL&nbsp; &nbsp;| NULL&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;| NULL&nbsp; &nbsp; | NULL&nbsp; &nbsp; | NULL&nbsp;&nbsp;| NULL |&nbsp; &nbsp;&nbsp; &nbsp; |<br \/>\n+----+--------------+------------+-------+-------------------+---------+---------+-------+------+-------+<\/p>\n<p>(5).SUBQUERY<\/p>\n<p>\u5b50\u67e5\u8be2\u4e2d\u7684\u7b2c\u4e00\u4e2aSELECT.<\/p>\n<p>mysql&gt; explain select * from t3 where id = (select id from t3 where id=3952602 )&nbsp;&nbsp;;<br \/>\n+----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------------+<br \/>\n| id | select_type | table | type&nbsp;&nbsp;| possible_keys&nbsp; &nbsp;&nbsp;&nbsp;| key&nbsp; &nbsp;&nbsp;&nbsp;| key_len | ref&nbsp; &nbsp;| rows | Extra&nbsp; &nbsp;&nbsp; &nbsp; |<br \/>\n+----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------------+<br \/>\n|&nbsp;&nbsp;1 | PRIMARY&nbsp; &nbsp;&nbsp;&nbsp;| t3&nbsp; &nbsp; | const | PRIMARY,idx_t3_id | PRIMARY | 4&nbsp; &nbsp;&nbsp; &nbsp; | const |&nbsp; &nbsp; 1 |&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; |<br \/>\n|&nbsp;&nbsp;2 | SUBQUERY&nbsp; &nbsp; | t3&nbsp; &nbsp; | const | PRIMARY,idx_t3_id | PRIMARY | 4&nbsp; &nbsp;&nbsp; &nbsp; |&nbsp; &nbsp;&nbsp; &nbsp; |&nbsp; &nbsp; 1 | Using index |<br \/>\n+----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------------+<\/p>\n<p>(6).&nbsp;&nbsp;DEPENDENT SUBQUERY<\/p>\n<p>\u5b50\u67e5\u8be2\u4e2d\u7684\u7b2c\u4e00\u4e2aSELECT\uff0c\u53d6\u51b3\u4e8e\u5916\u9762\u7684\u67e5\u8be2<\/p>\n<p>mysql&gt; explain select id from t3 where id in (select id from t3 where id=3952602 )&nbsp;&nbsp;;<br \/>\n+----+--------------------+-------+-------+-------------------+---------+---------+-------+------+--------------------------+<br \/>\n| id | select_type&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;| table | type&nbsp;&nbsp;| possible_keys&nbsp; &nbsp;&nbsp;&nbsp;| key&nbsp; &nbsp;&nbsp;&nbsp;| key_len | ref&nbsp; &nbsp;| rows | Extra&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;|<br \/>\n+----+--------------------+-------+-------+-------------------+---------+---------+-------+------+--------------------------+<br \/>\n|&nbsp;&nbsp;1 | PRIMARY&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;| t3&nbsp; &nbsp; | index | NULL&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;| PRIMARY | 4&nbsp; &nbsp;&nbsp; &nbsp; | NULL&nbsp;&nbsp;| 1000 | Using where; Using index |<br \/>\n|&nbsp;&nbsp;2 | DEPENDENT SUBQUERY | t3&nbsp; &nbsp; | const | PRIMARY,idx_t3_id | PRIMARY | 4&nbsp; &nbsp;&nbsp; &nbsp; | const |&nbsp; &nbsp; 1 | Using index&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;|<br \/>\n+----+--------------------+-------+-------+-------------------+---------+---------+-------+------+--------------------------+<\/p>\n<p>\n(7).DERIVED<\/p>\n<p>\u6d3e\u751f\u8868\u7684SELECT(FROM\u5b50\u53e5\u7684\u5b50\u67e5\u8be2)<\/p>\n<p>mysql&gt; explain select * from (select * from t3 where id=3952602) a ;<br \/>\n+----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+<br \/>\n| id | select_type | table&nbsp; &nbsp;&nbsp; &nbsp;| type&nbsp; &nbsp;| possible_keys&nbsp; &nbsp;&nbsp;&nbsp;| key&nbsp; &nbsp;&nbsp;&nbsp;| key_len | ref&nbsp;&nbsp;| rows | Extra |<br \/>\n+----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+<br \/>\n|&nbsp;&nbsp;1 | PRIMARY&nbsp; &nbsp;&nbsp;&nbsp;| &lt;derived2&gt; | system | NULL&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;| NULL&nbsp; &nbsp; | NULL&nbsp; &nbsp; | NULL |&nbsp; &nbsp; 1 |&nbsp; &nbsp;&nbsp; &nbsp; |<br \/>\n|&nbsp;&nbsp;2 | DERIVED&nbsp; &nbsp;&nbsp;&nbsp;| t3&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;| const&nbsp;&nbsp;| PRIMARY,idx_t3_id | PRIMARY | 4&nbsp; &nbsp;&nbsp; &nbsp; |&nbsp; &nbsp;&nbsp; &nbsp;|&nbsp; &nbsp; 1 |&nbsp; &nbsp;&nbsp; &nbsp; |<br \/>\n+----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+<\/p>\n<p>\n3.table<\/p>\n<p>\u663e\u793a\u8fd9\u4e00\u884c\u7684\u6570\u636e\u662f\u5173\u4e8e\u54ea\u5f20\u8868\u7684.<br \/>\n\u6709\u65f6\u4e0d\u662f\u771f\u5b9e\u7684\u8868\u540d\u5b57,\u770b\u5230\u7684\u662fderivedx(x\u662f\u4e2a\u6570\u5b57,\u6211\u7684\u7406\u89e3\u662f\u7b2c\u51e0\u6b65\u6267\u884c\u7684\u7ed3\u679c)<\/p>\n<p>mysql&gt; explain select * from (select * from ( select * from t3 where id=3952602) a) b;<br \/>\n+----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+<br \/>\n| id | select_type | table&nbsp; &nbsp;&nbsp; &nbsp;| type&nbsp; &nbsp;| possible_keys&nbsp; &nbsp;&nbsp;&nbsp;| key&nbsp; &nbsp;&nbsp;&nbsp;| key_len | ref&nbsp;&nbsp;| rows | Extra |<br \/>\n+----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+<br \/>\n|&nbsp;&nbsp;1 | PRIMARY&nbsp; &nbsp;&nbsp;&nbsp;| &lt;derived2&gt; | system | NULL&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;| NULL&nbsp; &nbsp; | NULL&nbsp; &nbsp; | NULL |&nbsp; &nbsp; 1 |&nbsp; &nbsp;&nbsp; &nbsp; |<br \/>\n|&nbsp;&nbsp;2 | DERIVED&nbsp; &nbsp;&nbsp;&nbsp;| &lt;derived3&gt; | system | NULL&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;| NULL&nbsp; &nbsp; | NULL&nbsp; &nbsp; | NULL |&nbsp; &nbsp; 1 |&nbsp; &nbsp;&nbsp; &nbsp; |<br \/>\n|&nbsp;&nbsp;3 | DERIVED&nbsp; &nbsp;&nbsp;&nbsp;| t3&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;| const&nbsp;&nbsp;| PRIMARY,idx_t3_id | PRIMARY | 4&nbsp; &nbsp;&nbsp; &nbsp; |&nbsp; &nbsp;&nbsp; &nbsp;|&nbsp; &nbsp; 1 |&nbsp; &nbsp;&nbsp; &nbsp; |<br \/>\n+----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+<\/p>\n<p>4.type<\/p>\n<p>\u8fd9\u5217\u5f88\u91cd\u8981,\u663e\u793a\u4e86\u8fde\u63a5\u4f7f\u7528\u4e86\u54ea\u79cd\u7c7b\u522b,\u6709\u65e0\u4f7f\u7528\u7d22\u5f15.<br \/>\n\u4ece\u6700\u597d\u5230\u6700\u5dee\u7684\u8fde\u63a5\u7c7b\u578b\u4e3aconst\u3001eq_reg\u3001ref\u3001range\u3001indexhe\u548cALL <\/p>\n<p>(1).system<\/p>\n<p>\u8fd9\u662fconst\u8054\u63a5\u7c7b\u578b\u7684\u4e00\u4e2a\u7279\u4f8b\u3002\u8868\u4ec5\u6709\u4e00\u884c\u6ee1\u8db3\u6761\u4ef6.\u5982\u4e0b(t3\u8868\u4e0a\u7684id\u662f primary key)<\/p>\n<p>mysql&gt; explain select * from (select * from t3 where id=3952602) a ;<br \/>\n+----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+<br \/>\n| id | select_type | table&nbsp; &nbsp;&nbsp; &nbsp;| type&nbsp; &nbsp;| possible_keys&nbsp; &nbsp;&nbsp;&nbsp;| key&nbsp; &nbsp;&nbsp;&nbsp;| key_len | ref&nbsp;&nbsp;| rows | Extra |<br \/>\n+----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+<br \/>\n|&nbsp;&nbsp;1 | PRIMARY&nbsp; &nbsp;&nbsp;&nbsp;| &lt;derived2&gt; | system | NULL&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;| NULL&nbsp; &nbsp; | NULL&nbsp; &nbsp; | NULL |&nbsp; &nbsp; 1 |&nbsp; &nbsp;&nbsp; &nbsp; |<br \/>\n|&nbsp;&nbsp;2 | DERIVED&nbsp; &nbsp;&nbsp;&nbsp;| t3&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;| const&nbsp;&nbsp;| PRIMARY,idx_t3_id | PRIMARY | 4&nbsp; &nbsp;&nbsp; &nbsp; |&nbsp; &nbsp;&nbsp; &nbsp;|&nbsp; &nbsp; 1 |&nbsp; &nbsp;&nbsp; &nbsp; |<br \/>\n+----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+<\/p>\n<p>(2).const<\/p>\n<p>\u8868\u6700\u591a\u6709\u4e00\u4e2a\u5339\u914d\u884c\uff0c\u5b83\u5c06\u5728\u67e5\u8be2\u5f00\u59cb\u65f6\u88ab\u8bfb\u53d6\u3002\u56e0\u4e3a\u4ec5\u6709\u4e00\u884c\uff0c\u5728\u8fd9\u884c\u7684\u5217\u503c\u53ef\u88ab\u4f18\u5316\u5668\u5269\u4f59\u90e8\u5206\u8ba4\u4e3a\u662f\u5e38\u6570\u3002const\u8868\u5f88\u5feb\uff0c\u56e0\u4e3a\u5b83\u4eec\u53ea\u8bfb\u53d6\u4e00\u6b21\uff01<\/p>\n<p>const\u7528\u4e8e\u7528\u5e38\u6570\u503c\u6bd4\u8f83PRIMARY KEY\u6216UNIQUE\u7d22\u5f15\u7684\u6240\u6709\u90e8\u5206\u65f6\u3002\u5728\u4e0b\u9762\u7684\u67e5\u8be2\u4e2d\uff0ctbl_name\u53ef\u4ee5\u7528\u4e8econst\u8868\uff1a<br \/>\nSELECT * from tbl_name WHERE primary_key=1\uff1b<br \/>\nSELECT * from tbl_name WHERE primary_key_part1=1\u548c primary_key_part2=2\uff1b<\/p>\n<p>\u4f8b\u5982:<br \/>\nmysql&gt; explain select * from t3 where id=3952602;<br \/>\n+----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------+<br \/>\n| id | select_type | table | type&nbsp;&nbsp;| possible_keys&nbsp; &nbsp;&nbsp;&nbsp;| key&nbsp; &nbsp;&nbsp;&nbsp;| key_len | ref&nbsp; &nbsp;| rows | Extra |<br \/>\n+----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------+<br \/>\n|&nbsp;&nbsp;1 | SIMPLE&nbsp; &nbsp;&nbsp; &nbsp;| t3&nbsp; &nbsp; | const | PRIMARY,idx_t3_id | PRIMARY | 4&nbsp; &nbsp;&nbsp; &nbsp; | const |&nbsp; &nbsp; 1 |&nbsp; &nbsp;&nbsp; &nbsp; |<br \/>\n+----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------+<\/p>\n<p>\n(3). eq_ref<\/p>\n<p>\u5bf9\u4e8e\u6bcf\u4e2a\u6765\u81ea\u4e8e\u524d\u9762\u7684\u8868\u7684\u884c\u7ec4\u5408\uff0c\u4ece\u8be5\u8868\u4e2d\u8bfb\u53d6\u4e00\u884c\u3002\u8fd9\u53ef\u80fd\u662f\u6700\u597d\u7684\u8054\u63a5\u7c7b\u578b\uff0c\u9664\u4e86const\u7c7b\u578b\u3002\u5b83\u7528\u5728\u4e00\u4e2a\u7d22\u5f15\u7684\u6240\u6709\u90e8\u5206\u88ab\u8054\u63a5\u4f7f\u7528\u5e76\u4e14\u7d22\u5f15\u662fUNIQUE\u6216PRIMARY KEY\u3002<\/p>\n<p>eq_ref\u53ef\u4ee5\u7528\u4e8e\u4f7f\u7528= \u64cd\u4f5c\u7b26\u6bd4\u8f83\u7684\u5e26\u7d22\u5f15\u7684\u5217\u3002\u6bd4\u8f83\u503c\u53ef\u4ee5\u4e3a\u5e38\u91cf\u6216\u4e00\u4e2a\u4f7f\u7528\u5728\u8be5\u8868\u524d\u9762\u6240\u8bfb\u53d6\u7684\u8868\u7684\u5217\u7684\u8868\u8fbe\u5f0f\u3002<\/p>\n<p>\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0cMySQL\u53ef\u4ee5\u4f7f\u7528eq_ref\u8054\u63a5\u6765\u5904\u7406ref_tables\uff1a<\/p>\n<p>SELECT * FROM ref_table,other_table<br \/>\n&nbsp;&nbsp;WHERE ref_table.key_column=other_table.column;<\/p>\n<p>SELECT * FROM ref_table,other_table<br \/>\n&nbsp;&nbsp;WHERE ref_table.key_column_part1=other_table.column<br \/>\n&nbsp; &nbsp; AND ref_table.key_column_part2=1;<\/p>\n<p>\u4f8b\u5982<br \/>\nmysql&gt; create unique index&nbsp;&nbsp;idx_t3_id on t3(id) ;<br \/>\nQuery OK, 1000 rows affected (0.03 sec)<br \/>\nRecords: 1000&nbsp;&nbsp;Duplicates: 0&nbsp;&nbsp;Warnings: 0<\/p>\n<p>mysql&gt; explain select * from t3,t4 where t3.id=t4.accountid;<br \/>\n+----+-------------+-------+--------+-------------------+-----------+---------+----------------------+------+-------+<br \/>\n| id | select_type | table | type&nbsp; &nbsp;| possible_keys&nbsp; &nbsp;&nbsp;&nbsp;| key&nbsp; &nbsp;&nbsp; &nbsp; | key_len | ref&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;| rows | Extra |<br \/>\n+----+-------------+-------+--------+-------------------+-----------+---------+----------------------+------+-------+<br \/>\n|&nbsp;&nbsp;1 | SIMPLE&nbsp; &nbsp;&nbsp; &nbsp;| t4&nbsp; &nbsp; | ALL&nbsp; &nbsp; | NULL&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;| NULL&nbsp; &nbsp;&nbsp; &nbsp;| NULL&nbsp; &nbsp; | NULL&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;| 1000 |&nbsp; &nbsp;&nbsp; &nbsp; |<br \/>\n|&nbsp;&nbsp;1 | SIMPLE&nbsp; &nbsp;&nbsp; &nbsp;| t3&nbsp; &nbsp; | eq_ref | PRIMARY,idx_t3_id | idx_t3_id | 4&nbsp; &nbsp;&nbsp; &nbsp; | dbatest.t4.accountid |&nbsp; &nbsp; 1 |&nbsp; &nbsp;&nbsp; &nbsp; |<br \/>\n+----+-------------+-------+--------+-------------------+-----------+---------+----------------------+------+-------+<\/p>\n<p>(4).ref<\/p>\n<p>\u5bf9\u4e8e\u6bcf\u4e2a\u6765\u81ea\u4e8e\u524d\u9762\u7684\u8868\u7684\u884c\u7ec4\u5408\uff0c\u6240\u6709\u6709\u5339\u914d\u7d22\u5f15\u503c\u7684\u884c\u5c06\u4ece\u8fd9\u5f20\u8868\u4e2d\u8bfb\u53d6\u3002\u5982\u679c\u8054\u63a5\u53ea\u4f7f\u7528\u952e\u7684\u6700\u5de6\u8fb9\u7684\u524d\u7f00\uff0c\u6216\u5982\u679c\u952e\u4e0d\u662fUNIQUE\u6216PRIMARY KEY\uff08\u6362\u53e5\u8bdd\u8bf4\uff0c\u5982\u679c\u8054\u63a5\u4e0d\u80fd\u57fa\u4e8e\u5173\u952e\u5b57\u9009\u62e9\u5355\u4e2a\u884c\u7684\u8bdd\uff09\uff0c\u5219\u4f7f\u7528ref\u3002\u5982\u679c\u4f7f\u7528\u7684\u952e\u4ec5\u4ec5\u5339\u914d\u5c11\u91cf\u884c\uff0c\u8be5\u8054\u63a5\u7c7b\u578b\u662f\u4e0d\u9519\u7684\u3002<\/p>\n<p>ref\u53ef\u4ee5\u7528\u4e8e\u4f7f\u7528=\u6216&lt;=&gt;\u64cd\u4f5c\u7b26\u7684\u5e26\u7d22\u5f15\u7684\u5217\u3002<\/p>\n<p>\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0cMySQL\u53ef\u4ee5\u4f7f\u7528ref\u8054\u63a5\u6765\u5904\u7406ref_tables\uff1a<\/p>\n<p>SELECT * FROM ref_table WHERE key_column=expr;<\/p>\n<p>SELECT * FROM ref_table,other_table<br \/>\n&nbsp;&nbsp;WHERE ref_table.key_column=other_table.column;<\/p>\n<p>SELECT * FROM ref_table,other_table<br \/>\n&nbsp;&nbsp;WHERE ref_table.key_column_part1=other_table.column<br \/>\n&nbsp; &nbsp; AND ref_table.key_column_part2=1;<\/p>\n<p>\u4f8b\u5982:<\/p>\n<p>mysql&gt; drop index idx_t3_id on t3;<br \/>\nQuery OK, 1000 rows affected (0.03 sec)<br \/>\nRecords: 1000&nbsp;&nbsp;Duplicates: 0&nbsp;&nbsp;Warnings: 0<\/p>\n<p>mysql&gt; create index idx_t3_id on t3(id) ;<br \/>\nQuery OK, 1000 rows affected (0.04 sec)<br \/>\nRecords: 1000&nbsp;&nbsp;Duplicates: 0&nbsp;&nbsp;Warnings: 0<\/p>\n<p>mysql&gt; explain select * from t3,t4 where t3.id=t4.accountid;<br \/>\n+----+-------------+-------+------+-------------------+-----------+---------+----------------------+------+-------+<br \/>\n| id | select_type | table | type | possible_keys&nbsp; &nbsp;&nbsp;&nbsp;| key&nbsp; &nbsp;&nbsp; &nbsp; | key_len | ref&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;| rows | Extra |<br \/>\n+----+-------------+-------+------+-------------------+-----------+---------+----------------------+------+-------+<br \/>\n|&nbsp;&nbsp;1 | SIMPLE&nbsp; &nbsp;&nbsp; &nbsp;| t4&nbsp; &nbsp; | ALL&nbsp;&nbsp;| NULL&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;| NULL&nbsp; &nbsp;&nbsp; &nbsp;| NULL&nbsp; &nbsp; | NULL&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;| 1000 |&nbsp; &nbsp;&nbsp; &nbsp; |<br \/>\n|&nbsp;&nbsp;1 | SIMPLE&nbsp; &nbsp;&nbsp; &nbsp;| t3&nbsp; &nbsp; | ref&nbsp;&nbsp;| PRIMARY,idx_t3_id | idx_t3_id | 4&nbsp; &nbsp;&nbsp; &nbsp; | dbatest.t4.accountid |&nbsp; &nbsp; 1 |&nbsp; &nbsp;&nbsp; &nbsp; |<br \/>\n+----+-------------+-------+------+-------------------+-----------+---------+----------------------+------+-------+<br \/>\n2 rows in set (0.00 sec)<\/p>\n<p>(5).&nbsp;&nbsp;ref_or_null<\/p>\n<p>\u8be5\u8054\u63a5\u7c7b\u578b\u5982\u540cref\uff0c\u4f46\u662f\u6dfb\u52a0\u4e86MySQL\u53ef\u4ee5\u4e13\u95e8\u641c\u7d22\u5305\u542bNULL\u503c\u7684\u884c\u3002\u5728\u89e3\u51b3\u5b50\u67e5\u8be2\u4e2d\u7ecf\u5e38\u4f7f\u7528\u8be5\u8054\u63a5\u7c7b\u578b\u7684\u4f18\u5316\u3002<\/p>\n<p>\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0cMySQL\u53ef\u4ee5\u4f7f\u7528ref_or_null\u8054\u63a5\u6765\u5904\u7406ref_tables\uff1a<\/p>\n<p>SELECT * FROM ref_table<br \/>\nWHERE key_column=expr OR key_column IS NULL;<\/p>\n<p>(6). index_merge<\/p>\n<p>\u8be5\u8054\u63a5\u7c7b\u578b\u8868\u793a\u4f7f\u7528\u4e86\u7d22\u5f15\u5408\u5e76\u4f18\u5316\u65b9\u6cd5\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0ckey\u5217\u5305\u542b\u4e86\u4f7f\u7528\u7684\u7d22\u5f15\u7684\u6e05\u5355\uff0ckey_len\u5305\u542b\u4e86\u4f7f\u7528\u7684\u7d22\u5f15\u7684\u6700\u957f\u7684\u5173\u952e\u5143\u7d20\u3002<\/p>\n<p>\u4f8b\u5982:<br \/>\nmysql&gt; explain select * from t4 where id=3952602 or accountid=31754306 ;<br \/>\n+----+-------------+-------+-------------+----------------------------+----------------------------+---------+------+------+------------------------------------------------------+<br \/>\n| id | select_type | table | type&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;| possible_keys&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;| key&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;| key_len | ref&nbsp;&nbsp;| rows | 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;|<br \/>\n+----+-------------+-------+-------------+----------------------------+----------------------------+---------+------+------+------------------------------------------------------+<br \/>\n|&nbsp;&nbsp;1 | SIMPLE&nbsp; &nbsp;&nbsp; &nbsp;| t4&nbsp; &nbsp; | index_merge | idx_t4_id,idx_t4_accountid | idx_t4_id,idx_t4_accountid | 4,4&nbsp; &nbsp;&nbsp;&nbsp;| NULL |&nbsp; &nbsp; 2 | Using union(idx_t4_id,idx_t4_accountid); Using where |<br \/>\n+----+-------------+-------+-------------+----------------------------+----------------------------+---------+------+------+------------------------------------------------------+<br \/>\n1 row in set (0.00 sec)<\/p>\n<p>(7). unique_subquery<\/p>\n<p>\u8be5\u7c7b\u578b\u66ff\u6362\u4e86\u4e0b\u9762\u5f62\u5f0f\u7684IN\u5b50\u67e5\u8be2\u7684ref\uff1a<\/p>\n<p>value IN (SELECT primary_key FROM single_table WHERE some_expr)<br \/>\nunique_subquery\u662f\u4e00\u4e2a\u7d22\u5f15\u67e5\u627e\u51fd\u6570\uff0c\u53ef\u4ee5\u5b8c\u5168\u66ff\u6362\u5b50\u67e5\u8be2\uff0c\u6548\u7387\u66f4\u9ad8\u3002<\/p>\n<p>(8).index_subquery<\/p>\n<p>\u8be5\u8054\u63a5\u7c7b\u578b\u7c7b\u4f3c\u4e8eunique_subquery\u3002\u53ef\u4ee5\u66ff\u6362IN\u5b50\u67e5\u8be2\uff0c\u4f46\u53ea\u9002\u5408\u4e0b\u5217\u5f62\u5f0f\u7684\u5b50\u67e5\u8be2\u4e2d\u7684\u975e\u552f\u4e00\u7d22\u5f15\uff1a<\/p>\n<p>value IN (SELECT key_column FROM single_table WHERE some_expr)<\/p>\n<p>(9).range<\/p>\n<p>\u53ea\u68c0\u7d22\u7ed9\u5b9a\u8303\u56f4\u7684\u884c\uff0c\u4f7f\u7528\u4e00\u4e2a\u7d22\u5f15\u6765\u9009\u62e9\u884c\u3002key\u5217\u663e\u793a\u4f7f\u7528\u4e86\u54ea\u4e2a\u7d22\u5f15\u3002key_len\u5305\u542b\u6240\u4f7f\u7528\u7d22\u5f15\u7684\u6700\u957f\u5173\u952e\u5143\u7d20\u3002\u5728\u8be5\u7c7b\u578b\u4e2dref\u5217\u4e3aNULL\u3002<\/p>\n<p>\u5f53\u4f7f\u7528=\u3001&lt;&gt;\u3001&gt;\u3001&gt;=\u3001&lt;\u3001&lt;=\u3001IS NULL\u3001&lt;=&gt;\u3001BETWEEN\u6216\u8005IN\u64cd\u4f5c\u7b26\uff0c\u7528\u5e38\u91cf\u6bd4\u8f83\u5173\u952e\u5b57\u5217\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528range<\/p>\n<p>mysql&gt; explain select * from t3 where id=3952602 or id=3952603 ;<br \/>\n+----+-------------+-------+-------+-------------------+-----------+---------+------+------+-------------+<br \/>\n| id | select_type | table | type&nbsp;&nbsp;| possible_keys&nbsp; &nbsp;&nbsp;&nbsp;| key&nbsp; &nbsp;&nbsp; &nbsp; | key_len | ref&nbsp;&nbsp;| rows | Extra&nbsp; &nbsp;&nbsp; &nbsp; |<br \/>\n+----+-------------+-------+-------+-------------------+-----------+---------+------+------+-------------+<br \/>\n|&nbsp;&nbsp;1 | SIMPLE&nbsp; &nbsp;&nbsp; &nbsp;| t3&nbsp; &nbsp; | range | PRIMARY,idx_t3_id | idx_t3_id | 4&nbsp; &nbsp;&nbsp; &nbsp; | NULL |&nbsp; &nbsp; 2 | Using where |<br \/>\n+----+-------------+-------+-------+-------------------+-----------+---------+------+------+-------------+<br \/>\n1 row in set (0.02 sec)<\/p>\n<p>(10).index<\/p>\n<p>\u8be5\u8054\u63a5\u7c7b\u578b\u4e0eALL\u76f8\u540c\uff0c\u9664\u4e86\u53ea\u6709\u7d22\u5f15\u6811\u88ab\u626b\u63cf\u3002\u8fd9\u901a\u5e38\u6bd4ALL\u5feb\uff0c\u56e0\u4e3a\u7d22\u5f15\u6587\u4ef6\u901a\u5e38\u6bd4\u6570\u636e\u6587\u4ef6\u5c0f\u3002<\/p>\n<p>\u5f53\u67e5\u8be2\u53ea\u4f7f\u7528\u4f5c\u4e3a\u5355\u7d22\u5f15\u4e00\u90e8\u5206\u7684\u5217\u65f6\uff0cMySQL\u53ef\u4ee5\u4f7f\u7528\u8be5\u8054\u63a5\u7c7b\u578b\u3002<\/p>\n<p>(11). ALL<\/p>\n<p>\u5bf9\u4e8e\u6bcf\u4e2a\u6765\u81ea\u4e8e\u5148\u524d\u7684\u8868\u7684\u884c\u7ec4\u5408\uff0c\u8fdb\u884c\u5b8c\u6574\u7684\u8868\u626b\u63cf\u3002\u5982\u679c\u8868\u662f\u7b2c\u4e00\u4e2a\u6ca1\u6807\u8bb0const\u7684\u8868\uff0c\u8fd9\u901a\u5e38\u4e0d\u597d\uff0c\u5e76\u4e14\u901a\u5e38\u5728\u5b83\u60c5\u51b5\u4e0b\u5f88\u5dee\u3002\u901a\u5e38\u53ef\u4ee5\u589e\u52a0\u66f4\u591a\u7684\u7d22\u5f15\u800c\u4e0d\u8981\u4f7f\u7528ALL\uff0c\u4f7f\u5f97\u884c\u80fd\u57fa\u4e8e\u524d\u9762\u7684\u8868\u4e2d\u7684\u5e38\u6570\u503c\u6216\u5217\u503c\u88ab\u68c0\u7d22\u51fa\u3002<\/p>\n<p>\n5.possible_keys<\/p>\n<p>possible_keys\u5217\u6307\u51faMySQL\u80fd\u4f7f\u7528\u54ea\u4e2a\u7d22\u5f15\u5728\u8be5\u8868\u4e2d\u627e\u5230\u884c\u3002\u6ce8\u610f\uff0c\u8be5\u5217\u5b8c\u5168\u72ec\u7acb\u4e8eEXPLAIN\u8f93\u51fa\u6240\u793a\u7684\u8868\u7684\u6b21\u5e8f\u3002\u8fd9\u610f\u5473\u7740\u5728possible_keys\u4e2d\u7684\u67d0\u4e9b\u952e\u5b9e\u9645\u4e0a\u4e0d\u80fd\u6309\u751f\u6210\u7684\u8868\u6b21\u5e8f\u4f7f\u7528\u3002<\/p>\n<p>\u5982\u679c\u8be5\u5217\u662fNULL\uff0c\u5219\u6ca1\u6709\u76f8\u5173\u7684\u7d22\u5f15\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u901a\u8fc7\u68c0\u67e5WHERE\u5b50\u53e5\u770b\u662f\u5426\u5b83\u5f15\u7528\u67d0\u4e9b\u5217\u6216\u9002\u5408\u7d22\u5f15\u7684\u5217\u6765\u63d0\u9ad8\u4f60\u7684\u67e5\u8be2\u6027\u80fd\u3002\u5982\u679c\u662f\u8fd9\u6837\uff0c\u521b\u9020\u4e00\u4e2a\u9002\u5f53\u7684\u7d22\u5f15\u5e76\u4e14\u518d\u6b21\u7528EXPLAIN\u68c0\u67e5\u67e5\u8be2<\/p>\n<p>6. key<\/p>\n<p>key\u5217\u663e\u793aMySQL\u5b9e\u9645\u51b3\u5b9a\u4f7f\u7528\u7684\u952e\uff08\u7d22\u5f15\uff09\u3002\u5982\u679c\u6ca1\u6709\u9009\u62e9\u7d22\u5f15\uff0c\u952e\u662fNULL\u3002\u8981\u60f3\u5f3a\u5236MySQL\u4f7f\u7528\u6216\u5ffd\u89c6possible_keys\u5217\u4e2d\u7684\u7d22\u5f15\uff0c\u5728\u67e5\u8be2\u4e2d\u4f7f\u7528FORCE INDEX\u3001USE INDEX\u6216\u8005IGNORE INDEX\u3002<\/p>\n<p>7.key_len<\/p>\n<p>key_len\u5217\u663e\u793aMySQL\u51b3\u5b9a\u4f7f\u7528\u7684\u952e\u957f\u5ea6\u3002\u5982\u679c\u952e\u662fNULL\uff0c\u5219\u957f\u5ea6\u4e3aNULL\u3002<br \/>\n\u4f7f\u7528\u7684\u7d22\u5f15\u7684\u957f\u5ea6\u3002\u5728\u4e0d\u635f\u5931\u7cbe\u786e\u6027\u7684\u60c5\u51b5\u4e0b\uff0c\u957f\u5ea6\u8d8a\u77ed\u8d8a\u597d <\/p>\n<p>8. ref<\/p>\n<p>ref\u5217\u663e\u793a\u4f7f\u7528\u54ea\u4e2a\u5217\u6216\u5e38\u6570\u4e0ekey\u4e00\u8d77\u4ece\u8868\u4e2d\u9009\u62e9\u884c\u3002<\/p>\n<p>9. rows<\/p>\n<p>rows\u5217\u663e\u793aMySQL\u8ba4\u4e3a\u5b83\u6267\u884c\u67e5\u8be2\u65f6\u5fc5\u987b\u68c0\u67e5\u7684\u884c\u6570\u3002<\/p>\n<p>10. Extra<\/p>\n<p>\u8be5\u5217\u5305\u542bMySQL\u89e3\u51b3\u67e5\u8be2\u7684\u8be6\u7ec6\u4fe1\u606f,\u4e0b\u9762\u8be6\u7ec6.<\/p>\n<p>(1).Distinct <br \/>\n\u4e00\u65e6MYSQL\u627e\u5230\u4e86\u4e0e\u884c\u76f8\u8054\u5408\u5339\u914d\u7684\u884c\uff0c\u5c31\u4e0d\u518d\u641c\u7d22\u4e86 <\/p>\n<p>(2).Not exists <br \/>\nMYSQL\u4f18\u5316\u4e86LEFT JOIN\uff0c\u4e00\u65e6\u5b83\u627e\u5230\u4e86\u5339\u914dLEFT JOIN\u6807\u51c6\u7684\u884c\uff0c <\/p>\n<p>\u5c31\u4e0d\u518d\u641c\u7d22\u4e86 <\/p>\n<p>(3).Range checked for each <\/p>\n<p>Record\uff08index map:#\uff09 <br \/>\n\u6ca1\u6709\u627e\u5230\u7406\u60f3\u7684\u7d22\u5f15\uff0c\u56e0\u6b64\u5bf9\u4e8e\u4ece\u524d\u9762\u8868\u4e2d\u6765\u7684\u6bcf\u4e00\u4e2a\u884c\u7ec4\u5408\uff0cMYSQL\u68c0\u67e5\u4f7f\u7528\u54ea\u4e2a\u7d22\u5f15\uff0c\u5e76\u7528\u5b83\u6765\u4ece\u8868\u4e2d\u8fd4\u56de\u884c\u3002\u8fd9\u662f\u4f7f\u7528\u7d22\u5f15\u7684\u6700\u6162\u7684\u8fde\u63a5\u4e4b\u4e00 <\/p>\n<p>(4).Using filesort <br \/>\n\u770b\u5230\u8fd9\u4e2a\u7684\u65f6\u5019\uff0c\u67e5\u8be2\u5c31\u9700\u8981\u4f18\u5316\u4e86\u3002MYSQL\u9700\u8981\u8fdb\u884c\u989d\u5916\u7684\u6b65\u9aa4\u6765\u53d1\u73b0\u5982\u4f55\u5bf9\u8fd4\u56de\u7684\u884c\u6392\u5e8f\u3002\u5b83\u6839\u636e\u8fde\u63a5\u7c7b\u578b\u4ee5\u53ca\u5b58\u50a8\u6392\u5e8f\u952e\u503c\u548c\u5339\u914d\u6761\u4ef6\u7684\u5168\u90e8\u884c\u7684\u884c\u6307\u9488\u6765\u6392\u5e8f\u5168\u90e8\u884c <\/p>\n<p>(5).Using index <br \/>\n\u5217\u6570\u636e\u662f\u4ece\u4ec5\u4ec5\u4f7f\u7528\u4e86\u7d22\u5f15\u4e2d\u7684\u4fe1\u606f\u800c\u6ca1\u6709\u8bfb\u53d6\u5b9e\u9645\u7684\u884c\u52a8\u7684\u8868\u8fd4\u56de\u7684\uff0c\u8fd9\u53d1\u751f\u5728\u5bf9\u8868\u7684\u5168\u90e8\u7684\u8bf7\u6c42\u5217\u90fd\u662f\u540c\u4e00\u4e2a\u7d22\u5f15\u7684\u90e8\u5206\u7684\u65f6\u5019 <\/p>\n<p>(6).Using temporary <br \/>\n\u770b\u5230\u8fd9\u4e2a\u7684\u65f6\u5019\uff0c\u67e5\u8be2\u9700\u8981\u4f18\u5316\u4e86\u3002\u8fd9\u91cc\uff0cMYSQL\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u8868\u6765\u5b58\u50a8\u7ed3\u679c\uff0c\u8fd9\u901a\u5e38\u53d1\u751f\u5728\u5bf9\u4e0d\u540c\u7684\u5217\u96c6\u8fdb\u884cORDER BY\u4e0a\uff0c\u800c\u4e0d\u662fGROUP BY\u4e0a <\/p>\n<p>(7).Using where<br \/>\n\u4f7f\u7528\u4e86WHERE\u4ece\u53e5\u6765\u9650\u5236\u54ea\u4e9b\u884c\u5c06\u4e0e\u4e0b\u4e00\u5f20\u8868\u5339\u914d\u6216\u8005\u662f\u8fd4\u56de\u7ed9\u7528\u6237\u3002\u5982\u679c\u4e0d\u60f3\u8fd4\u56de\u8868\u4e2d\u7684\u5168\u90e8\u884c\uff0c\u5e76\u4e14\u8fde\u63a5\u7c7b\u578bALL\u6216index\uff0c\u8fd9\u5c31\u4f1a\u53d1\u751f\uff0c\u6216\u8005\u662f\u67e5\u8be2\u6709\u95ee\u9898\n<\/div>\n","protected":false},"excerpt":{"rendered":"<div class=\"postText\">Mysql Explain \u8be6\u89e3<\/p>\n<p>\n\u4e00.\u8bed\u6cd5<\/p>\n<p>explain &lt; table_name &gt;<\/p>\n<p>\u4f8b\u5982: explain select * from t3 where id=3952602;<\/p>\n<p>\u4e8c.explain\u8f93\u51fa\u89e3\u91ca<\/p>\n<p>+----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------+<br \/>\n| id | select_type | table | type&nbsp;&nbsp;| possible_keys&nbsp; &nbsp;&nbsp;&#038;nb...<\/div>\n<p> <a href=\"https:\/\/www.xiaobo.li\/notes\/archives\/267\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[288],"tags":[2],"class_list":["post-267","post","type-post","status-publish","format-standard","hentry","category-db","tag-MySQL"],"_links":{"self":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/posts\/267","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/comments?post=267"}],"version-history":[{"count":0,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/posts\/267\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/media?parent=267"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/categories?post=267"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/tags?post=267"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}