热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

explain执行计划:

e

explain执行计划:

Column JSON Name Meaning

id select_id The SELECT identifier

select_type None The SELECT type

table table_name The table for the output row

partitions partitions The matching partitions

type access_type The join type

possible_keys possible_keys The possible indexes to choose

key key The index actually chosen

key_len key_length The length of the chosen key

ref ref The columns compared to the index

rows rows Estimate of rows to be examined

filtered filtered Percentage of rows filtered by table condition

Extra None Additional information

的语法与用法

explain [extended] select … from … where …

如果使用了extended,那么在执行完explain语句后,可以使用show warnings语句查询相应的优化信息。

比如我们执行 select uid from user where uname=’scofield’ order by uid 执行结果会有

| id | select_type | table | type | possible_keys | key | key_len |ref | rows | Extra | 这些东西。

其中 table 表示是哪个表的数据。

type比较重要。type显示的是访问类型,依次是

system > const >eq_ref > ref > fulltext > ref_or_null > index_merge > unique_subquery >index_subquery > range > index > ALL

一般情况,至少要达到 range 级别,最好是 ref 级别。否则可能会有性能问题。

possible_keys 是指可以应用到该表的索引,如果为NULL则没有。

key 是指用到的索引。

key_len 是索引的长度,在不影响查询精度的情况下,值越小越好。

ref 是指索引的那一列被使用了。一般会是个常数。

rows 是指有多少行。

extra 是指额外的信息。也是比较重要的。如果值为 distinct ,说明mysql 找到了域行联合匹配的行,就不再查找了。

如果值为 not exits : mysql优化了 left join ,一旦找到了 left join 匹配的行,便不再进行搜索了。

如果值为 rang checked for each : 没有找到理想的索引。

如果为 using filesort ,则需要改进sql了。这说明 mysql执行 需要 文件排序。这是比较影响效率的。

如果为 using temporary , 这是使用了 临时表。 这种情况也比较影响效率,sql需要改进。或者从应用层进行改进。

如果为 where used 说明使用了where语句。如果 type为 all 或者 index ,一般会出现这样的结果。这样的问题,一般是查询需要改进。

在一般稍大的系统中,基本尽可能的减少 join ,子查询 等等。mysql就使用最简单的查询,这样效率最高。至于 join 等,可以放在应用层去解决

二.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 | | system | NULL | NULL |NULL | NULL | 1 | |

| 2 | DERIVED | | system | NULL | NULL |NULL | NULL | 1 | |

| 3 | DERIVED | t3 | const | PRIMARY,idx_t3_id | PRIMARY | 4 | | 1 | |

±—±------------±-----------±-------±------------------±--------±--------±-----±-----±------+

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

2. select_type

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

(1) SIMPLE

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

mysql> explain select * from t3 where id=3952602;

±—±------------±------±------±------------------±--------±--------±------±-----±------+

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

±—±------------±------±------±------------------±--------±--------±------±-----±------+

| 1 | SIMPLE | t3 | const | PRIMARY,idx_t3_id | PRIMARY | 4 |const | 1 | |

±—±------------±------±------±------------------±--------±--------±------±-----±------+

(2). PRIMARY

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

mysql> explain select * from (select * from t3 where id=3952602) a ;

±—±------------±-----------±-------±------------------±--------±--------±-----±-----±------+

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

±—±------------±-----------±-------±------------------±--------±--------±-----±-----±------+

| 1 | PRIMARY | | system | NULL | NULL |NULL | NULL | 1 | |

| 2 | DERIVED | t3 | const | PRIMARY,idx_t3_id | PRIMARY | 4 | | 1 | |

±—±------------±-----------±-------±------------------±--------±--------±-----±-----±------+

(3).UNION

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

mysql> explain select * from t3 where id=3952602 union all select * from t3;

±—±-------------±-----------±------±------------------±--------±--------±------±-----±------+

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

±—±-------------±-----------±------±------------------±--------±--------±------±-----±------+

| 1 | PRIMARY | t3 | const | PRIMARY,idx_t3_id | PRIMARY | 4 | const | 1 | |

| 2 | UNION | t3 | ALL | NULL | NULL | NULL | NULL | 1000 | |

|NULL | UNION RESULT | | ALL | NULL | NULL |NULL | NULL | NULL | |

±—±-------------±-----------±------±------------------±--------±--------±------±-----±------+

(4).DEPENDENT UNION

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

mysql> explain select * from t3 where id in (select id from t3 where id=3952602 union all select id from t3) ;

±—±-------------------±-----------±-------±------------------±--------±--------±------±-----±-------------------------+

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

±—±-------------------±-----------±-------±------------------±--------±--------±------±-----±-------------------------+

| 1 | PRIMARY | t3 | ALL | NULL | NULL | NULL | NULL | 1000 | Using where |

| 2 | DEPENDENT SUBQUERY | t3 | const | PRIMARY,idx_t3_id |PRIMARY | 4 | const | 1 | Using index |

| 3 | DEPENDENT UNION | t3 | eq_ref | PRIMARY,idx_t3_id |PRIMARY | 4 | func | 1 | Using where; Using index |

|NULL | UNION RESULT | | ALL | NULL | NULL | NULL | NULL | NULL | |

±—±-------------------±-----------±-------±------------------±--------±--------±------±-----±-------------------------+



推荐阅读
author-avatar
alive
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有