最好的网站开发公司,常州视频剪辑培训机构,杭州婚恋网站建设,招商网官网平台Explain工具介绍
使用EXPLAIN关键字可以模拟优化器执行SQL语句,分析你的查询语句或是结构的性能瓶颈
在select语句之前增加explain关键字,MySQL会在查询前设置一个标记,执行查询会返回执行计划的信息,而不是执行这条SQL
注意: 如果from中包含子查询,仍会执行该子查询,将结果…Explain工具介绍
使用EXPLAIN关键字可以模拟优化器执行SQL语句,分析你的查询语句或是结构的性能瓶颈
在select语句之前增加explain关键字,MySQL会在查询前设置一个标记,执行查询会返回执行计划的信息,而不是执行这条SQL
注意: 如果from中包含子查询,仍会执行该子查询,将结果放入临时表中
Explain分析示例
explain官方文档
DROP TABLE IF EXISTS actor;
CREATE TABLE actor (id int(11) NOT NULL,name varchar(45) DEFAULT NULL,update_time datetime DEFAULT NULL,PRIMARY KEY (id)
) ENGINEInnoDB DEFAULT CHARSETutf8;INSERT INTO actor (id, name, update_time) VALUES (1,a,2024-02-22 15:27:18), (2,b,2024-02-22 15:27:18), (3,c,2024-02-22 15:27:18);DROP TABLE IF EXISTS film;
CREATE TABLE film (id int(11) NOT NULL AUTO_INCREMENT,name varchar(10) DEFAULT NULL,PRIMARY KEY (id),KEY idx_name (name)
) ENGINEInnoDB DEFAULT CHARSETutf8;INSERT INTO film (id, name) VALUES (3,film0),(1,film1),(2,film2);DROP TABLE IF EXISTS film_actor;
CREATE TABLE film_actor (id int(11) NOT NULL,film_id int(11) NOT NULL,actor_id int(11) NOT NULL,remark varchar(255) DEFAULT NULL,PRIMARY KEY (id),KEY idx_film_actor_id (film_id,actor_id)
) ENGINEInnoDB DEFAULT CHARSETutf8;INSERT INTO film_actor (id, film_id, actor_id) VALUES (1,1,1),(2,1,2),(3,2,1);explain select * from actorexplain两个变种 explain extended: 会在explain的基础上额外提供一些查询优化信息,紧随其后通过show warnings命令可以得到优化后的查询语句,从而看出优化器优化了什么.额外还有filtered列,是一个百分比的值,rows*filtered/100可以估算出将要和explain中前一个表进行连接的行数(前一个表指explain中的id值比当前表id值小的表) explain extended select * from film where id 1;
show warningsexplain partitions: 相比explain多了个partitions字段,如果查询是基于分区表的话,会查询将访问的分区
以上两个变种新增的filtered列和partitions列在5.7以后使用explain都会直接显示出来,不需要再加其他的关键字
explain中的列
id
id列的编号是select的序列号,有几个select就有几个id,并且id的顺序是按select出现的顺序增长的
id列越大执行优先级越高,id相同则从上往下执行,id为null最后执行
select_type
select_type表示对应行是简单还是复杂的查询 simple: 简单查询,查询不包含子查询和union explain select * from film where id 2primary: 复杂查询中最外层的select subquery: 包含在select中的子查询(不在from子句中) derived: 包含在from子句中的子查询.MySQL会将结果存放在一个临时表中,也成为派生表(derived的英文含义) set session optimizer_switchderived_mergeoff;
explain select(select 1 from actor where id 1) from (select * from film where id 1)der;set session optimizer_switchderived_mergeon;union: 在union中的第二个和随后的select explain select 1 union all select 1table
这一列表示explain的一行正在访问那个表.
当from子句中有子查询时,table列是格式,表示当前查询依赖idN的查询,于是先执行idN的查询
当有union时,union result的table列的值为union1,2,1和2表示参与union的select行id.
type
这一列表示关联类型或访问类型, 即MySQL决定如何查找表中的行,查找数据行记录的大概范围.
依次最优到最差分别为:systemconsteq_refrefrangeindexALL
关于SQL性能优化目标,Java开发手册嵩山版: Null: MySQL能够在优化阶段分解查询语句,在执行阶段用不着再访问表或索引.例如:再索引列中选取最小值,可以单独查找索引来完成(B树叶子节点最左节点就是最小值).不需要在执行时访问表
explain select min(id) from filmsystem,const: MySQL能对查询的某部分进行优化并将其转化成一个常量(可以看show warnings的结果).用于primary key或unique key的所有列与常数比较时,所有表最多有一个匹配行,读取1次速度比较快. system时const的特例, 表中只有一条元素匹配时为system
explain select * from (select * from film where id 1)tmp;
show warningseq_ref: primary key或unique key索引的所有部分被连接使用,最多只会返回一条符合条件的记录.这可能是再const之外最好的连接类型了,简单的select查询不会出现这种type ref: 相比eq_ref,不适用唯一索引,而是使用普通索引或者唯一性索引的部分前缀,索引要和某个值相比较,可能会找到多个符合条件的记录 简单的select查询,name是普通索引(非唯一索引) explain select * from film where name film1关联表查询,idx_film_actor_id是film_id和actor_id的联合索引,这里使用到了film_actor的左边前缀film_id部分 explain select film_id from film left join film_actor on film.id film_actor.film_idrange: 范围扫描通常出现在in(),between,,,等操作中.使用一个索引来检索给定范围的记录
explain select * from actor where id 1index: 扫描全索引就能拿到结果,一般是扫描某个二级索引,这种扫描不会从索引树根节点开始快速查找,而是直接对二级索引的叶子节点遍历和扫描,速度还是比较慢的,这种查询一般会使用覆盖索引,二级索引一般比较小,所以这种秦广比ALL快一些
explain select * from filmALL: 即全表扫描,扫描你的聚簇索引的所有叶子节点,通常情况下这需要增加索引来优化了.
explain select * from actorpossible_keys
这一列显示查询可能使用那些索引来查找
explain时可能踹向那possible_keys有值,而key显示为Null的情况,这种情况是因为表中数据不多,mysql认为索引对此查询帮助不大,选择了全表扫描
如果改列是Null,则没有相关的索引.在这种情况下,可以通过检查where子句看是否可以创建一个适当的索引来提高查询性能,然后用explain查看效果
key
这一列显示mysql实际采用那个索引来优化对该表的访问.
如果没有使用索引,则该列是Null.如果想强制mysql使用或护士possible_keys列中的索引,在查询中使用force index,ignore index
key_len
这一列显示了mysql在索引里使用的字节数,通过这个值可以算出具体使用了索引中的那些列
举例来说,film_actor的联合索引idx_film_actor_id由film_id和actor_id两个int列组成,并且每个int都是4字节.通过结果中的key_len4可以推断查询使用了第一个列:film_id来执行索引查找
explain select * from film_actor where film_id 2key_len计算规则如下:
字符串:char(n)和varchar(n),5.0.3以后的版本中,n代表字符数,而不是字节数, 如果是utf-8,一个数字或字母1个字节,一个汉字占3个字节 char(n):如果存汉字长度就是3n字节varchar(n):如果存汉字长度就是3n2字节,加的2字节用来存储字符串长度,因为varchar是可变长度字符串 数值类型 tinyint:1字节smallint:2字节int:4字节bigint:8字节 时间类型 date:3字节timestamp:4字节datetime:8字节 如果字段允许为NULL,需要1字节记录是否为NULL
索引最大长度是768字节,当字符串过长时,mysql会做一个类似左前缀索引的处理,将前半部分的字符提取出来做索引
ref
这一列显示在key列记录的索引中, 表查找值所使用到的列或常量,常见的由:const;字段名
rows
这一列是mysql估计要读取并检测的行数,注意这个不是结果集里的行数
filtered
该列是一个百分比的值,rows*filtered/100可以估算出将要和explain中前一个表进行连接的行数(前一个表指explain中的id值比当前表id值小的表)
Extra
这一列展示的是额外信息.常见的重要值如下:
Using index:使用覆盖索引
覆盖索引定义:mysql执行计划explain结果里的key有使用索引,如果select后面查询的字段都可以从这个索引的树中获取,这种情况下就避免了回表.一般可以说用到了覆盖索引.extra里一般都有using index;覆盖索引一般针对的是辅助索引:整个查询结果只通过覆盖索引就能拿到结果,不需要通过辅助索引树找到主键,再通过主键去主键索引树获取其他字段的值
explain select film_id from film_actor where film_id 1Using where: 使用where语句来处理结果,并且查询的列未被索引覆盖
explain select * from actor where name aUsing index condition: 查询的列不完全被索引覆盖,where条件中是一个前导列范围
explain select * from film_actor WHERE film_id 1Using temporary: mysql需要创建一张临时表来处理查询.出现这种情况一般是要进行优化的.首先想到用索引来优化 actor.name没有索引,此时创建了张临时表来distinct explain select distinct name from actorfilm.name建立了idx_name索引,此时查询extra是using index,没有用临时表 explain select distinct name from filmUsing filesort: 将用外部排序而不是索引排序,数据较小时从内存排序,否则需要再磁盘完成排序.这种情况下一般也是要考虑使用索引来优化的. actor.name未创建索引,会浏览actor整张表,保存排序关键字name和对应的id,然后排序name并检索行记录 explain select * from actor order by namefilm.name建立了idx_name索引.此时查询时extra时using index explain select * from film order by nameSelect tables optimized away: 使用某些聚合函数(比如max,min)来访问存在索引的某个字段时
explain select min(id) from film索引最佳实战
示例表
CREATE TABLE employees (id int(11) NOT NULL AUTO_INCREMENT,name varchar(24) NOT NULL DEFAULT COMMENT 姓名,age int(11) NOT NULL DEFAULT 0 COMMENT 年龄,position varchar(20) NOT NULL DEFAULT COMMENT 职位,hire_time timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 入职时间,PRIMARY KEY (id),KEY idx_name_age_position (name,age,position) USING BTREE
) ENGINEInnoDB AUTO_INCREMENT4 DEFAULT CHARSETutf8 COMMENT员工记录表;INSERT INTO employees(name,age,position,hire_time) VALUES(LiLei,22,manager,NOW());
INSERT INTO employees(name,age,position,hire_time) VALUES(HanMeimei, 23,dev,NOW());
INSERT INTO employees(name,age,position,hire_time) VALUES(Lucy,23,dev,NOW());全值匹配 explain select * from employees where name LiLeiexplain select * from employees where name LiLei and age 22最左前缀法则 如果联合索引,要遵守最左前缀法则.指的是查询从索引的最左列开始并且不跳过索引中的列. explain select * from employees where name LiLei and age 22;
explain select * from employees where age 30 and position dev;
explain select * from employees where position dev不在索引列上做任何操作(计算,函数,自动/手动类型转换),会导致索引失效而转向全表扫描 explain select * from employees where name LiLei;
explain select * from employees where left(name,3) LiLei给hire_time增加一个普通索引: alter table employees add index idx_hire_time(hire_time)using btree
explain select * from employees where date(hire_time) 2024-03-13转化为日期范围查询,有可能会走索引 explain select * from employees where hire_time 2024-03-13 00:00:00 and hire_time 2024-03-13 23:59:59还原最初索引状态 alter table employees drop index idx_hire_time存储引擎不能使用索引中范围条件右边的列 explain select * from employees where name LiLei and age 22 and position dev;
explain select * from employees where name LiLei and age 22 and position dev;尽量使用覆盖索引(只访问索引的查询(索引列包含查询列)),减少select *语句 explain select name,age,position from employees where name LiLei and age 22 and position dev;
explain select * from employees where name LiLei and age 22 and position dev;mysql在使用不等于(!/),not int,not exists的时候无法使用索引会导致全表扫描.小于,大于,小于等于,大于等于这些,MySQL内部优化器会根据检索比例,表大小等多个因素整体评估是否使用索引 explain select * from employees where name ! LiLeiis null,is not null一般情况下也无法使用索引 explain select * from employees where name is nulllike以通配符开头(‘%xxx’)mysql索引失效会变成全表扫描操作 explain select * from employees where name like %Li;
explain select * from employees where name like Li%问题:解决以通配符开头索引不被使用的办法 使用覆盖索引,查询字段必须是建立覆盖索引字段 explain select name, age, position from employees where name like %Li;如果不能使用覆盖索引则可能需要借助搜索引擎 字符串不加单引号索引失效 explain select * from employees where name 1000;
explain select * from employees where name 1000;少用or或in,用它查询时,mysql不一定使用索引,mysql内部优化器会根据检索比例,表大小等多个因素整体评估是否使用索引,详见范围查询优化 explain select * from employees where name LiLei or name HanMeiMei范围查询优化 给age添加单值索引 alter table employees add index idx_age(age) using btreeexplain select * from employees where age 1 and age 2000没走索引的原因:mysql内部优化器会根据检索比例,表大小等多个因素整体评估是否使用索引.比如,可能是由于单次数据量查询过大导致优化器最终选择不走索引;优化方法:可以将大的范围拆分成多个小范围 explain select * from employees where age 1 and age 1000;
explain select * from employees where age 1000 and age 2000;还原最初索引状态 alter table employees drop index idx_age索引使用总结
like ‘kk%’ 相当于常量,‘%kk’ 和’%kk%相当于范围