您好,欢迎来到年旅网。
搜索
您的当前位置:首页mysql_explain总结

mysql_explain总结

来源:年旅网
一、用途

如果在SELECT语句前放上关键词EXPLAIN,MySQL将解释它如何处理SELECT,提供有关表如何联接和联接的次序。借助于EXPLAIN,可以知道什么时候必须为表加入索引以得到一个使用索引来寻找记录的更快的SELECT。比较常用的两种用途: 1、 查看采用不同的方式编写的查询是否影响了索引的使用。

2、 查看向数据表添加索引对优化器生成高效率执行计划的能力的影响。

二、返回信息

explain返回信息的意义(EXPLAIN为用于SELECT语句中的每个表返回一行信息。表以它们在处理查询过程中将被MySQL读入的顺序被列出)

下面的eg使用的数据表结构如下:

CREATE TABLE `manager` (

`managerid` int(10) NOT NULL, `roleid` int(10) NOT NULL,

`class` tinyint(2) NOT NULL default '0', `status` tinyint(2) NOT NULL default '0', `crm` tinyint(2) default '0', `cra` tinyint(2) default '0',

`crsystem` char(10) default NULL, `defaultsys` int(3) default NULL, `performType` tinyint(1) default '1', PRIMARY KEY (`managerid`)

) ENGINE=InnoDB DEFAULT CHARSET=gbk;

CREATE TABLE `systree` (

`id` int(10) NOT NULL auto_increment, `managerid` int(10) NOT NULL, `path` varchar(200) NOT NULL, PRIMARY KEY (`id`),

KEY `Index_systree_managerid` (`managerid`) ) ENGINE=InnoDB DEFAULT CHARSET=gbk;

1、 id

SELECT的查询序列号。

eg:explain select t1.managerid,t1.status from (select ma1.managerid,ma1.status from manager ma1,systree s where substr(s.path,21,10)=393268 and substr(s.path,71,10)=0) t1; id select_type table 1 type possible_keys key key_len ref rows Extra const found row not PRIMARY system NULL NULL NULL NULL 0 2 2 DERIVED DERIVED ma1 s All All NULL NULL NULL NULL NULL 625 NULL NULL NULL 4036 Using where

2、 select_type

SELECT类型,可以为以下任何一种:

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

eg:Select * from table1 where id=1

2、 PRIMARY:最外面的SELECT

eg:explain select t1.managerid,t1.status from (select ma1.managerid,ma1.status from manager ma1,systree s where substr(s.path,21,10)=393268 and substr(s.path,71,10)=0) t1; id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY system NULL NULL NULL NULL 0 const row not found 2 2 DERIVED DERIVED ma1 s All All NULL NULL NULL NULL NULL NULL NULL 625 NULL 4036 Using where

3、UNION:UNION中的第二个或后面的SELECT语句

eg:explain select t1.managerid from manager t1 where t1.status=2 union select t2.managerid from managerinfo t2 id select_type table type possible_keys key key_len ref rows Extra

1 PRIMARY t1 All NULL NULL NULL NULL 625 Using where 2 UNION t2 index NULL PRIMARY 4 NULL 629 Using index null UNION RESULT All NULL NULL NULL NULL NULL

4、DEPENDENT UNION:UNION中的第二个或后面的SELECT语句, 依赖外层查询

eg:explain select * from manager where managerid in (select t1.managerid from manager t1 where t1.status=2 union select t2.managerid from managerinfo t2); id select_type table type possible_keys key key_len ref rows Extra 1 2 PRIMARY SUBQUERY 3 DEPENDENT t2 UNION eq_ref PRIMARY PRIMARY 4 func 1 Using where;Using index manager All NULL NULL NULL NULL 625 func 1 Using where Using where DEPENDENT t1 eq_ref PRIMARY PRIMARY 4 null UNION RESULT All NULL NULL NULL NULL NULL

5、UNION RESULT:UNION的结果。

eg:explain select t1.managerid from manager t1 where t1.status=2 union select t2.managerid from managerinfo t2 id select_type table type possible_keys key key_len ref rows Extra

1 2 PRIMARY UNION t1 t2 All index NULL NULL NULL NULL NULL 625 NULL 629 Using where PRIMARY 4 Using index null UNION RESULT All NULL NULL NULL NULL NULL

6、SUBQUERY:子查询中的第一个SELECT

eg:explain select * from manager where managerid > (select count(*) from manager t1); id select_type table type possible_keys key key_len ref rows Extra

1 PRIMARY manager range PRIMARY PRIMARY 4 NULL 312 Using where 2 SUBQUERY t1 index NULL PRIMARY 4 NULL 625 Using index

7、DEPENDENT SUBQUERY:子查询中的第一个SELECT,依赖外层查询

eg:explain select * from manager where managerid in (select t1.managerid from managerinfo t1); iselect_type table t ype possible_keykey key_leref rowExtrd s n s a

1 PRIMARY manager All NULL NULL NULL NULL 625 Using where 2 DEPENDENT SUBQUERY t1 unique_subquery PRIMARY PRIMARY 4 func 1 Using index 8、DERIVED: FROM子句的子查询

eg:explain select t1.managerid,t1.status from (select ma1.managerid,ma1.status from manager ma1,systree s where substr(s.path,21,10)=393268 and substr(s.path,71,10)=0) t1; id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY system NULL NULL NULL NULL 0 const row not found 2 2 DERIVED DERIVED ma1 s All All NULL NULL NULL NULL NULL NULL NULL 625 NULL 4036 Using where

3、 table

输出的行所引用的表。

4、 type

type代表联接类型。下面给出各种联接类型,按照从最佳类型到最坏类型进行排序: 1、 System:表仅有一行(=系统表)。这是const联接类型的一个特例。

eg:explain select * from (select * from managerinfo mi order by mi.managername limit 1)t1; id select_type table type 1 2

PRIMARY DERIVED possible_keys key key_len ref rows Extra Using filesort system NULL mi All NULL NULL NULL NULL NULL NULL 1 NULL 629 2、 const:表最多有一行匹配的记录,它在查询一开始时就会被读取出来。由于只有一行记录,

在余下的处理中,该行记录可以被当作是一个恒定值,所以称作const。const很快,因为它们只读取一次!索引通常需要是 PRIMARY KEY或UNIQUE,如select * from table where primary_key =1;

eg:explain select * from managerinfo mi where managerid=392978;

id select_type table type 1 SIMPLE mi const possible_keys key PRIMARY key_len ref rows Extra

PRIMARY 4 const 1

3、 eq_ref:从该表中会有一行记录被读取出来以和前一个表中读取出来的记录做联合或对比。

与 const 类型不同的是,这是最好的连接类型。它用在索引所有部分都用于做连接并且这个索引是一个 PRIMARY KEY 或 UNIQUE 类型。eq_ref 可以用于在进行\"=\"做比较时检索字段。比较的值可以是固定值或者是表达式,表达示中可以使用表里的字段,它们在读表之前已经准备好了。以下的几个例子中,MySQL使用了 eq_ref 连接来处理 ref_table:

eg:explain select * from manager t1,managerinfo t2 where t1.managerid=t2.managerid; select_type table type possible_keys key key_len ref rows Extra

SIMPLE t1 All PRIMARY NULL NULL NULL 625 id 1 1 SIMPLE t2 eq_ref PRIMARY PRIMARY 4 crm_db.t1.managerid 1

4、 ref: 该表中所有符合检索值的记录都会被取出来和别表中取出来的记录作联合或对比。

ref 用于连接程序使用键的最左前缀或者是该键不是 PRIMARY KEY 或 UNIQUE 索引(换句话说,就是连接程序无法根据键值只取得一条记录)的情况。当根据键值只查询到少数几条匹配的记录时,这就是一个不错的连接类型。ref 还可以用于检索字段使用 = 操作符来比较的时候。以下的几个例子中,MySQL将使用 ref 来处理 ref_table:

eg:explain select * from custmnt t1,custmntpost t2 where t1.managerid=t2.managerid; select_type table type possible_keys key key_len ref rows Extra

SIMPLE SIMPLE t2 t1 All ref NULL managerid NULL NULL NULL 1938 Using where managerid 5 crm_db.t1.managerid 145 id 1 1

5、 ref_or_null:这种连接类型类似 ref,不同的是MySQL会在检索的时候额外的搜索包含 NULL 值的记录。这种连接类型的优化是从MySQL 4.1.1开始的,它经常用于子查询。 SELECT * FROM ref_table

WHERE key_column=expr OR key_column IS NULL;

6、 index_merge:该联接类型表示使用了多列索引。在这种情况下,key列包含了使用的索引

的清单,key_len包含了使用的索引的最长的关键元素

7、 unique_subquery:子查询中含有唯一索引,这种类型用以下形式的 IN 子查询来替换 ref:

value IN (SELECT primary_key FROM single_table WHERE some_expr) ,unique_subquery 只是用来完全替换子查询的索引查找函数效率更高了。

eg:explain select * from manager where managerid in (select t1.managerid from managerinfo t1); iselect_type table t ype possible_keykey key_leref rowExtrd s n s a

1 PRIMARY manager All NULL NULL NULL NULL 625 Using where 2 DEPENDENT t1 unique_subquery PRIMARY PRIMARY 4 func 1 Using SUBQUERY index

8、 index_subquery:这种连接类型类似 unique_subquery。它用子查询来代替 IN,不过它用

于在子查询中没有唯一索引的情况下,如以下形式:value IN (SELECT key_column FROM single_table WHERE some_expr) eg:explain select * from manager where managerid in (select t1.managerid from custmnt t1); iselect_type table t ype possible_keykey key_leref rowExtrd s n s a

1 PRIMARY manager All NULL NULL NULL NULL 625 Using where 2 DEPENDENT SUBQUERY t1 index_subquery managerid managerid 5 func 128 Using index

9、 Range:只检索给定范围的行,使用一个索引来选择行。key列显示使用了哪个索引。key_len

包含所使用索引的最长关键元素。在该类型中ref列为NULL。当使用=、<>、>、>=、<、<=、IS NULL、<=>、BETWEEN或者IN操作符,用常量比较关键字列时,可以使用range: eg:explain select * from manager where managerid > (select count(*) from manager t1); id select_type table type possible_keys key key_len ref rows Extra

1 PRIMARY manager range PRIMARY PRIMARY 4 NULL 312 Using where 2 SUBQUERY t1 index NULL PRIMARY 4 NULL 625 Using index

10、 Index:除了只有索引树被扫描,该联接类型与ALL相同。这通常比ALL快,因为

索引文件通常比数据文件小。当查询只使用作为单索引一部分的列时,MySQL可以使用该联接类型。 eg:explain select * from manager where managerid > (select count(*) from manager t1); id select_type table type possible_keys key key_len ref rows Extra

1 PRIMARY manager range PRIMARY PRIMARY 4 NULL 312 Using where 2 SUBQUERY t1 index NULL PRIMARY 4 NULL 625 Using index

11、 ALL:对于每个来自于先前的表的行组合,进行完整的表扫描。通常可以增加更多

的索引而不要使用ALL,使得行能基于前面的表中的常数值或列值被检索出。

5、 possible_keys

possible_keys列指出MySQL能使用哪个索引在该表中找到行,如果该列是NULL,则没有相关的索引

6、 key、

key列显示MySQL实际决定使用的键(索引)。如果没有选择索引,键是NULL。要想强制MySQL使用或忽视possible_keys列中的索引,在查询中使用FORCE INDEX、USE INDEX或者IGNORE INDEX。

7、 key_len

key_len列显示MySQL决定使用的键长度。如果键是NULL,则长度为NULL。注意通过key_len值我们可以确定MySQL将实际使用一个多列索引(将多个字段如a、b、c建成多列索引a_b_c)的几个部分。

8、 ref

它显示的是列的名字或者恒定值 MySQL将根据这些列来选择行(The ref column shows which columns or constants are used with the key to select rows from the table)

9、 rows:

MySQL认为的它在找到正确的结果之前必须扫描的记录数。显然,这里最理想的数字就是1,比如

1)不建索引:explain select * from table1 where name=1 对应row应该接近table1中的所有记录数 2)给table1的name字段建立索引:explain select * from table1 where name=1 应该更接近查询结果数

10、 Extra

其他详细信息(若Extra 字段的值是 Using where或Using filesort,则说明MySQL无法使用索引)

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- oldu.cn 版权所有 浙ICP备2024123271号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务