mysql执行计划

通过mysql执行计划可以查看SQL语句的具体执行过程,达到优化SQL的目的,以提高SQL语句执行效率

执行计划包含的信息

ColumnMeaning
idThe SELECT identifier
select_typeThe SELECT type
tableThe table for the output row
partitionsThe matching partitions
typeThe join type
possible_keysThe possible indexes to choose
keyThe index actually chosen
key_lenThe length of the chosen key
refThe columns compared to the index
rowsEstimate of rows to be examined
filteredPercentage of rows filtered by table condition
extraAdditional information

id
查询序列号,当引用其他行的并集结果时可以为null,通过id可以知道查询子句或操作表的执行顺序
分为3种情况:

  1. id相同,执行顺序从上而下
  2. id不同,id序号越大,越先被执行
  3. id相同与不相同都存在,id越大越先执行,相同的id自上而下执行

select_type
主要用来区分查询类型,是普通查询还是子查询

select_type ValueMeaning
SIMPLESimple SELECT (not using UNION or subqueries)
PRIMARYOutermost SELECT
UNIONSecond or later SELECT statement in a UNION
DEPENDENT UNIONSecond or later SELECT statement in a UNION, dependent on outer query
UNION RESULTResult of a UNION
SUBQUERYFirst SELECT in subquery
DEPENDENT SUBQUERYFirst SELECT in subquery, dependent on outer query
DERIVEDDerived table

SIMPLE
没有使用任何联合查询或子查询

explain select * from t_user u

PRIMARY
最外层的查询为PRIMARY

explain select user_name from t_user where dept_id = (select id from t_dept where name = 'test')

UNION
第二个或最后一个使用union之后的查询为UNION

explain select * from t_user where user_name = 'test' UNION select * from t_user where user_name = 'zs'

DEPENDENT UNION
与UNION类似,但结果会受外部表影响

explain select * from t_user where id in (select id from t_user where user_name = 'test' UNION select id from t_user where user_name = 'zs')

UNION RESULT
从union获得结果的查询

explain select * from t_user where user_name = 'test' UNION select * from t_user where user_name = 'zs'

*SUBQUERY *
在子查询中的第一个查询

explain select user_name from t_user where dept_id = (select id from t_dept where name = 'test')

DEPENDENT SUBQUERY
与SUBQUERY 类似,但结果受外部表影响

explain select * from t_user where id in (select id from t_user where user_name = 'test' UNION select id from t_user where user_name = 'zs')

DERIVED
派生表,写在from后面的子查询,在8.0中未复现出来

explain select * from (select id,user_name from t_user) t 

table
表示对应行访问的表名或者别名,也允许以下值,

  1. unionM,N:表示id为M和N参与了并集
  2. derivedN:表示使用id为N的查询产生的派生表
  3. subqueryN:表示id为N的子查询结果集

partitions
匹配的分区,非分区表时为null

type
type描述链接

possible_keys
查询时可能会用到的索引

key
实际用到的索引,与查询的列相同时,则表示使用的覆盖索引

key_len
表示使用的索引的字节数,长度越短越好

ref
显示索引哪一列被使用了,通常是个常数值,如果显示func,则表示使用的是某个函数的结果

rows
显示必须读取的数据行数,在 InnoDB 中这个值是预估值,这个值越小越好

filtered
表示被过滤行数的预估百分之,为100时,则没有过滤任何值,这个值越小越好

Extra
查询的一些附加信息

  1. using filesort:使用文件排序,效率比较慢
explain select * from t_user ORDER BY create_time
  1. using temporary:为了完成查询,mysql创建了一张临时表,通常会在使用 group by 或者 order by 时出现
explain select dept_id from t_user where user_name = 'test' group by dept_id
  1. Using index:直接从索引树中拿数据时出现
explain select dept_id from t_user
  1. Using where:使用了where条件进行过滤
explain select dept_id from t_user where create_time = '2022-3-21 15:48:24'

结语
上述案例用的mysql版本为8.0
表结构

CREATE TABLE `t_user` (
  `id` int NOT NULL AUTO_INCREMENT,
  `user_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '用户名',
  `create_time` datetime DEFAULT NULL,
  `dept_id` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idx` (`user_name`) USING BTREE,
  KEY `idx1` (`dept_id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb3;
CREATE TABLE `t_dept` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idx` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值