Mysql Explain 详细介绍
mysql explain 这里做一个资料的全面整理。
一.语法
explain < table_name >
例如: explain select * from t3 where id=3952602;
二.explain输出解释
+----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | extra |
+----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------+
1.id
我的理解是sql执行的顺利的标识,sql从大到小的执行.
例如:
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 | <derived2> | system | null | null | null | null | 1 | |
| 2 | derived | <derived3> | 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 | <derived2> | 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 | <union1,2> | 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 | <union2,3> | 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 | <union1,2> | 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 | <derived2> | 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 | <derived2> | system | null | null | null | null | 1 | |
| 2 | derived | <derived3> | 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联接类型的一个特例。表仅有一行满足条件.如下(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 | <derived2> | 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
对于每个来自于前面的表的行组合,从该表中读取一行。这可能是最好的联接类型,除了const类型。它用在一个索引的所有部分被联接使用并且索引是unique或primary key。
eq_ref可以用于使用= 操作符比较的带索引的列。比较值可以为常量或一个使用在该表前面所读取的表的列的表达式。
在下面的例子中,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可以用于使用=或<=>操作符的带索引的列。
在下面的例子中,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值的行。在解决子查询中经常使用该联接类型的优化。
在下面的例子中,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
该类型替换了下面形式的in子查询的ref:
value in (select primary_key from single_table where some_expr)
unique_subquery是一个索引查找函数,可以完全替换子查询,效率更高。
(8).index_subquery
该联接类型类似于unique_subquery。可以替换in子查询,但只适合下列形式的子查询中的非唯一索引:
value in (select key_column from single_table where some_expr)
(9).range
只检索给定范围的行,使用一个索引来选择行。key列显示使用了哪个索引。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)
(10).index
该联接类型与all相同,除了只有索引树被扫描。这通常比all快,因为索引文件通常比数据文件小。
当查询只使用作为单索引一部分的列时,mysql可以使用该联接类型。
(11). all
对于每个来自于先前的表的行组合,进行完整的表扫描。如果表是第一个没标记const的表,这通常不好,并且通常在它情况下很差。通常可以增加更多的索引而不要使用all,使得行能基于前面的表中的常数值或列值被检索出。
5.possible_keys
possible_keys列指出mysql能使用哪个索引在该表中找到行。注意,该列完全独立于explain输出所示的表的次序。这意味着在possible_keys中的某些键实际上不能按生成的表次序使用。
如果该列是null,则没有相关的索引。在这种情况下,可以通过检查where子句看是否它引用某些列或适合索引的列来提高你的查询性能。如果是这样,创造一个适当的索引并且再次用explain检查查询
6. key
key列显示mysql实际决定使用的键(索引)。如果没有选择索引,键是null。要想强制mysql使用或忽视possible_keys列中的索引,在查询中使用force index、use index或者ignore index。
7.key_len
key_len列显示mysql决定使用的键长度。如果键是null,则长度为null。
使用的索引的长度。在不损失精确性的情况下,长度越短越好
8. ref
ref列显示使用哪个列或常数与key一起从表中选择行。
9. rows
rows列显示mysql认为它执行查询时必须检查的行数。
10. extra
该列包含mysql解决查询的详细信息,下面详细.
(1).distinct
一旦mysql找到了与行相联合匹配的行,就不再搜索了
(2).not exists
mysql优化了left join,一旦它找到了匹配left join标准的行,
就不再搜索了
(3).range checked for each
record(index map:#)
没有找到理想的索引,因此对于从前面表中来的每一个行组合,mysql检查使用哪个索引,并用它来从表中返回行。这是使用索引的最慢的连接之一
(4).using filesort
看到这个的时候,查询就需要优化了。mysql需要进行额外的步骤来发现如何对返回的行排序。它根据连接类型以及存储排序键值和匹配条件的全部行的行指针来排序全部行
(5).using index
列数据是从仅仅使用了索引中的信息而没有读取实际的行动的表返回的,这发生在对表的全部的请求列都是同一个索引的部分的时候
(6).using temporary
看到这个的时候,查询需要优化了。这里,mysql需要创建一个临时表来存储结果,这通常发生在对不同的列集进行order by上,而不是group by上
(7).using where
使用了where从句来限制哪些行将与下一张表匹配或者是返回给用户。如果不想返回表中的全部行,并且连接类型all或index,这就会发生,或者是查询有问题
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!