通过mysql执行计划可以查看SQL语句的具体执行过程,达到优化SQL的目的,以提高SQL语句执行效率
执行计划包含的信息
Column | Meaning |
---|
id | The SELECT identifier | select_type | The SELECT type | table | The table for the output row | partitions | The matching partitions | type | The join type | possible_keys | The possible indexes to choose | key | The index actually chosen | key_len | The length of the chosen key | ref | The columns compared to the index | rows | Estimate of rows to be examined | filtered | Percentage of rows filtered by table condition | extra | Additional information |
id 查询序列号,当引用其他行的并集结果时可以为null,通过id可以知道查询子句或操作表的执行顺序 分为3种情况:
- id相同,执行顺序从上而下
- id不同,id序号越大,越先被执行
- id相同与不相同都存在,id越大越先执行,相同的id自上而下执行
select_type 主要用来区分查询类型,是普通查询还是子查询
select_type Value | Meaning |
---|
SIMPLE | Simple SELECT (not using UNION or subqueries) | PRIMARY | Outermost SELECT | UNION | Second or later SELECT statement in a UNION | DEPENDENT UNION | Second or later SELECT statement in a UNION, dependent on outer query | UNION RESULT | Result of a UNION | SUBQUERY | First SELECT in subquery | DEPENDENT SUBQUERY | First SELECT in subquery, dependent on outer query | DERIVED | Derived 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 表示对应行访问的表名或者别名,也允许以下值,
- unionM,N:表示id为M和N参与了并集
- derivedN:表示使用id为N的查询产生的派生表
- subqueryN:表示id为N的子查询结果集
partitions 匹配的分区,非分区表时为null
type type描述链接
possible_keys 查询时可能会用到的索引
key 实际用到的索引,与查询的列相同时,则表示使用的覆盖索引
key_len 表示使用的索引的字节数,长度越短越好
ref 显示索引哪一列被使用了,通常是个常数值,如果显示func,则表示使用的是某个函数的结果
rows 显示必须读取的数据行数,在 InnoDB 中这个值是预估值,这个值越小越好
filtered 表示被过滤行数的预估百分之,为100时,则没有过滤任何值,这个值越小越好
Extra 查询的一些附加信息
- using filesort:使用文件排序,效率比较慢
explain select * from t_user ORDER BY create_time
- using temporary:为了完成查询,mysql创建了一张临时表,通常会在使用 group by 或者 order by 时出现
explain select dept_id from t_user where user_name = 'test' group by dept_id
- Using index:直接从索引树中拿数据时出现
explain select dept_id from t_user
- 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;
|