mysql 执行计划_mysql执行计划

执行计划使用explain sql查询。

1、 构造数据

usecoshaho002;drop table if existsinfo;create tableinfo(

idint primary keyAUTO_INCREMENT,

namevarchar(32),

agetinyint,

sexvarchar(8),

addressvarchar(32),

phonevarchar(32),

birthday date,

descriptionvarchar(128)

);alter table info add unique(phone);alter table info add indexname_index(name);alter table info add indexname_age_sex_index(name, age, sex);alter table info addfulltext(description);

describe info;INSERT INTO info VALUES(null, 'hkx1', 1, 'M', 'sc', '1234567890', '1989-01-03', 'chengxuyuan huawei1'),

(null, 'hkx2', 2, 'F', 'sc', '1234567891', '1989-02-03', 'chengxuyuan huawei2'),

(null, 'hkx3', 3, 'M', 'sc', '1234567892', '1989-03-03', 'chengxuyuan huawei3'),

(null, 'hkx4', 4, 'F', 'sc', '1234567893', '1989-07-05', 'chengxuyuan huawei4'),

(null, 'hkx5', 5, 'F', 'sc', '1234567894', '1989-07-04', 'chengxuyuan huawei5'),

(null, 'hkx6', 6, 'M', 'sc', '1234567895', '1989-07-06', 'chengxuyuan huawei6'),

(null, 'hkx7', 7, 'F', 'sc', '1234567896', '1989-07-07', 'chengxuyuan huawei7'),

(null, 'hkx1', 8, 'F', 'sc', '1234567897', '1989-07-08', 'chengxuyuan huawei8'),

(null, 'hkx2', 9, 'F', 'sc', '1234567898', '1989-07-03', 'chengxuyuan huawei9'),

(null, 'hkx3', 10, 'M', 'sc', '1234567899', '1989-01-03', 'chengxuyuan huawei10'),

(null, 'hkx4', 11, 'F', 'sc', '1234567810', '1989-07-03', 'chengxuyuan huawei11'),

(null, 'hkx5', 5, 'F', 'sc', '1234567820', '1989-07-03', 'chengxuyuan huawei12'),

(null, 'hkx6', 6, 'M', 'sc', '1234567830', '1989-02-03', 'chengxuyuan huawei13'),

(null, 'hkx7', 10, 'F', 'sc', '1234567840', '1989-09-03', 'chengxuyuan huawei14'),

(null, 'hkx8', 1, 'M', 'sc', '1234567850', '1989-01-03', 'chengxuyuan huawei1'),

(null, 'hkx9', 13, 'F', 'sc', '1234567860', '1989-06-03', 'chengxuyuan huawei2'),

(null, 'hkx0', 2, 'M', 'sc', '1234567870', '1989-07-03', 'chengxuyuan huawei3');select * from info;

2、 查询执行计划

usecoshaho002;select * frominfo;

explainselect * from info where id = 2;

explainselect * from info where name = 'hkx1';

explainselect * from info where name like 'hkx1%';

explainselect * from info where name like '%1';

explainselect * from info where age = 2;

explainselect * from info where name = 'hkx1' and sex = 'M';

explainselect * from info where name = 'hkx1' and age = 0;

explainselect * from info where name = 'hkx1' and age = 0 and sex ='M';

explainselect * from info where phone = '1234567890';

explainselect * from info where description = 'chengxuyuan huawei1';

explainselect * from info where name = 'hkx1' union select * from info;

2951d566f6b7b6cf978b55c76d87bd49.png

可以看到,查询结果包含id,select_type,table,partitions,type,possible_keys,key,key_len,ref,rows,filtered,Extra组成。

1、 type

type表访问类型,和查询效率紧密相关。type效率从好到坏依次为:

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

一般来说,得保证查询至少达到range级别,最好能达到ref。

ALL:全表扫描。

index:扫描全部索引树。

range:扫描部分索引(索引范围扫描)。常见于between、、like等的查询。

ref:非唯一性索引扫描,返回匹配某个单独值的所有行。常见于使用非唯一索查询,组合索引查询;

eq_ref:唯一性索引扫描,对于每个索引键,表中只有一条记录与之匹配。常见于主键或唯一索引扫描;

const,system:表最多有一个匹配行,它将在查询开始时被读取,列值当做常量处理。如将使用主键查询。(system是const特例,表只有一行记录)

NULL:MySQL在优化过程中分解语句,执行时甚至不用访问表或索引。

2、  id

select查询的序列号

3、 select_type

select查询的类型,主要是区别普通查询和联合查询、子查询之类的复杂查询。

(1) SIMPLE:查询中不包含子查询或者UNION

(2) 查询中若包含任何复杂的子部分,最外层查询则被标记为:PRIMARY

(3) 在SELECT或WHERE列表中包含了子查询,该子查询被标记为:SUBQUERY

(4) 在FROM列表中包含的子查询被标记为:DERIVED(衍生)

(5) 若第二个SELECT出现在UNION之后,则被标记为UNION;若UNION包含在 FROM子句的子查询中,外层SELECT将被标记为:DERIVED

(6) 从UNION表获取结果的SELECT被标记为:UNION RESULT

4、 table

该行查询所引用的表。

5、 possible_keys

指出MySQL能使用哪个索引在该表中找到行。查询涉及到的字段上若存在索引,则该索引将被列出,但不一定被查询使用。如果是空的,没有相关的索引。这时要提高性能,可通过检验WHERE子句,看是否引用某些字段,或者检查字段不是适合索引。

6、 key

显示MySQL实际决定使用的键。如果没有索引被选择,键是NULL。

7、 key_len

显示MySQL决定使用的键长度。表示索引中使用的字节数,可通过该列计算查询中使用的索引的长度。如果键是NULL,长度就是NULL。文档提示特别注意这个值可以得出一个多重主键里mysql实际使用了哪一部分。

注:key_len显示的值为索引字段的最大可能长度,并非实际使用长度,即key_len是根据表定义计算而得,不是通过表内检索出的。

8、 ref

显示哪个字段或常数与key一起被使用。

9、 rows

这个数表示mysql要遍历多少数据才能找到,表示MySQL根据表统计信息及索引选用情况,估算的找到所需的记录所需要读取的行数,在innodb上可能是不准确的。

10、 Extra

包含不适合在其他列中显示但十分重要的额外信息。

(1)Only index:意味着信息只用索引树中的信息检索出的,这比扫描整个表要快。

(2)using where:使用上了where限制,表示MySQL服务器在存储引擎受到记录后进行“后过滤”(Post-filter),如果查询未能使用索引,Using where的作用只是提醒我们MySQL将用where子句来过滤结果集。

(3)impossible where: 表示用不着where,一般就是没查出来啥。

(4)Using filesort:MySQL中无法利用索引完成的排序操作称为文件排序,当我们试图对一个没有索引的字段进行排序时,就是filesoft。它跟文件没有任何关系,实际上是内部的一个快速排序。

(5)Using temporary:表示MySQL需要使用临时表来存储结果集,常见于排序和分组查询,使用filesort和temporary的话会很吃力,WHERE和ORDER BY的索引经常无法兼顾,如果按照WHERE来确定索引,那么在ORDER BY时,就必然会引起Using filesort,这就要看是先过滤再排序划算,还是先排序再过滤划算。

参考:http://blog.csdn.net/xifeijian/article/details/19773795

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值