1、掌握MySQL执行计划所有字段含义以及内容,为后续SQL调优提供帮助
2、对SQL调优有初步认识
项目开发中,性能往往都是是我们重点关注的问题,其实很多时候一个SQL往往是整个请求中瓶颈最大的地方,因此我们必须了解SQL语句的执行过程、数据库中是如何扫描表、如何使用索引的、是否命中索引等信息来帮助我们做SQL语句的优化。MySQL提供了explain/desc语句,来显示这条SQL语句的执行计划,执行计划可以帮助我们查看SQL语句的执行情况,我们可以根据反馈的结果来进行SQL的优化。
use test;
CREATE TABLE `test`.`role` (
`id` int(11) NOT NULL,
`name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
CREATE TABLE `test`.`user` (
`id` int(11) NOT NULL,
`name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`role_id` int(11) NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
explain和desc效果一样,都是帮我们列出SQL语句的执行计划。
mysql> explain select * from user;
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
| 1 | SIMPLE | user | NULL | ALL | NULL | NULL | NULL | NULL | 1 | 100.00 | NULL |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)
mysql> desc select * from user;
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
| 1 | SIMPLE | user | NULL | ALL | NULL | NULL | NULL | NULL | 1 | 100.00 | NULL |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)
mysql>
两个语句的效果是一模一样的,我们后面就统一使用explain来查看SQL语句的执行计划。
id字段存在三种情况:
1)id相同:id越靠前的表越先执行
explain select * from user u left join role r on u.role_id=r.id;
2)id不同:id越大的表越先执行
explain select * from user u where u.role_id=(select id from role r where r.id=1);
3)id有相同,也有不同:id相同越大的表越先执行,在id相同的表中,id越靠前的表越先执行
explain select * from user;
explain select * from user u1 where u1.id =(select id from user u2 where u2.id=1);
explain select * from user u1 union select * from user u2;
EXPLAIN SELECT
*
FROM
user u1
WHERE
u1.id IN (
SELECT id FROM user u2 WHERE u2.id = 1
UNION
SELECT id FROM user u3 WHERE u3.id = 2
UNION
SELECT id FROM user u4 WHERE u4.id = 2
);
explain select * from user u1 where u1.id =(select id from user u2 where u2.id=1);
explain select * from user u1
where u1.name =(
select name from user u2 where u2.name=(select name from user u3 where u3.name='zs')
);
explain select * from user u1 where u1.role_id=(select id from role r1 where u1.id=1);
explain select * from
(select * from user u1 where u1.role_id=1 union select * from user u2 where u2.name='zs') temp;
表示该SQL语句是作用于那张表的,取值为:表名、表别名、衍生表名等。
explain select * from user;
explain select * from user u1;
涉及到分区的表
create table goods_partitions (
id int auto_increment,
name varchar(12),
primary key(id)
)
partition by range(id)(
partition p0 values less than(10000),
partition p1 values less than MAXVALUE
);
show variables like '%dir%';
查看物理存储文件,发现多了不同的文件来存储
explain select * from goods_partitions;
整个goods_partitions使用到了两个分区。
explain select * from goods_partitions where id<1000;
反应一段SQL语句性能指标的重要参数,可以通过此参数来判断是否使用到了索引、是否全表扫描、是否范围查询等。
插入测试数据:
insert into role values(1,'保洁');
insert into role values(2,'保安');
insert into role values(3,'厨师');
insert into user values(1,'zs',1);
explain select 1;
use mysql; -- 切换到mysql数据库
explain select * from db where host='localhost';
explain select * from user where id=1;
explain select * from user where name='zs';
分别根据name和id查询,发现只有id的type为const。
给name字段加上唯一索引(必须要是唯一索引,普通索引不行):
create unique index user_name_unique on user(name);
测试完毕删除唯一索引:
drop index user_name_unique on user;
explain select * from user u left join role r on u.role_id=r.id;
代表有其他表引用了r表的主键。
如果主表有多条记录与之匹配那么type将不再是eq_ref
mysql> select * from user;
+----+------+---------+
| id | name | role_id |
+----+------+---------+
| 1 | zs | 1 |
+----+------+---------+
1 row in set (0.00 sec)
mysql> select * from role;
+----+--------+
| id | name |
+----+--------+
| 1 | 保洁 |
| 2 | 保安 |
| 3 | 厨师 |
+----+--------+
3 rows in set (0.00 sec)
mysql> explain select * from user u left join role r on u.role_id=r.id;
+----+-------------+-------+------------+--------+---------------+---------+---------+----------------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+--------+---------------+---------+---------+----------------+------+----------+-------+
| 1 | SIMPLE | u | NULL | ALL | NULL | NULL | NULL | NULL | 1 | 100.00 | NULL |
| 1 | SIMPLE | r | NULL | eq_ref | PRIMARY | PRIMARY | 4 | test.u.role_id | 1 | 100.00 | NULL |
+----+-------------+-------+------------+--------+---------------+---------+---------+----------------+------+----------+-------+
2 rows in set, 1 warning (0.00 sec)
mysql>
user表示是1条记录,role是3条记录
mysql> select * from user;
+----+------+---------+
| id | name | role_id |
+----+------+---------+
| 1 | zs | 1 |
| 2 | ls | 1 |
+----+------+---------+
2 rows in set (0.00 sec)
mysql> select * from role;
+----+--------+
| id | name |
+----+--------+
| 1 | 保洁 |
| 2 | 保安 |
| 3 | 厨师 |
+----+--------+
3 rows in set (0.00 sec)
mysql> explain select * from user u left join role r on u.role_id=r.id;
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------------------------------------------+
| 1 | SIMPLE | u | NULL | ALL | NULL | NULL | NULL | NULL | 2 | 100.00 | NULL |
| 1 | SIMPLE | r | NULL | ALL | PRIMARY | NULL | NULL | NULL | 3 | 100.00 | Using where; Using join buffer (Block Nested Loop) |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------------------------------------------+
2 rows in set, 1 warning (0.00 sec)
mysql>
发现type字段变为了ALL,而不是原来的eq_ref。
创建普通索引:
create index user_name_index on user(name);
查询执行计划:
explain select * from user where name='zs';
测试完毕删除索引:
drop index user_name_index on user;
我们执行如下两句sql查看执行计划:
explain select * from user u where u.id>20; -- 使用索引列进行范围查询
explain select * from user u where u.role_id>20; -- 使用普通列进行范围查询
给role_id列添加索引,再次执行sql,查看执行计划:
create index user_role_id_index on user(role_id);
explain select * from user u where u.role_id>20;
explain select id from user;
explain select * from user;
查询效率从高到底的取值为:
-- 所有的type字段取值:
NULL > system > const > eq_ref > ref > fulltext > ref_or_null > index_merge > unique_subquery > index_subquery > range > index > ALL
-- 一般情况下type字段取值:
system > const > eq_ref > ref > range > index > ALL
查询语句中,可能应用到的索引,并非实际使用到的索引。实际使用到的索引根据key字段来反应。
例如:
-- 给name列加索引
create index idx_name on user(name);
explain select * from user where user='1' or user='2';
可能用到了idx_name索引,但实际没有使用到。
测试完毕删除索引:
drop index idx_name on user;
key字段反应sql语句实际使用的索引,为null代表没有使用索引
-- 根据id查询
explain select * from user where id=1;
-- 根据普通列查询
explain select * from user where name='zs';
-- 给name列加上索引
create index idx_name on user(name);
-- 根据索引查询
explain select * from user where name='zs';
测试完毕删除索引:
drop index idx_name on user;
表示索引中使用的字节数
explain select * from user where id=1;
我的id类型为int类型,因此占用4个字节。
我们把id类型改为bigint(Long),再次查看索引使用字节数:
alter table user modify column id bigint;
explain select * from user where id=1;
测试完毕更改回来:
alter table user modify column id int;
表示某表的某个字段引用到了本表的索引字段
mysql> select * from user;
+----+------+---------+
| id | name | role_id |
+----+------+---------+
| 1 | zs | 1 |
+----+------+---------+
1 row in set (0.00 sec)
mysql> select * from role;
+----+--------+
| id | name |
+----+--------+
| 1 | 保洁 |
| 2 | 保安 |
| 3 | 厨师 |
+----+--------+
3 rows in set (0.00 sec)
mysql> explain select * from user u left join role r on u.role_id=r.id;
+----+-------------+-------+------------+--------+---------------+---------+---------+----------------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+--------+---------------+---------+---------+----------------+------+----------+-------+
| 1 | SIMPLE | u | NULL | ALL | NULL | NULL | NULL | NULL | 1 | 100.00 | NULL |
| 1 | SIMPLE | r | NULL | eq_ref | PRIMARY | PRIMARY | 4 | test.u.role_id | 1 | 100.00 | NULL |
+----+-------------+-------+------------+--------+---------------+---------+---------+----------------+------+----------+-------+
2 rows in set, 1 warning (0.00 sec)
mysql>
表示u表的role_id引用了本表(r表)的索引字段(PRIMARY)
使用其他索引列关联表:
create index role_name_index on role(name); -- 给name列加索引。
explain select * from user u left join role r on u.name=r.name; -- 使用name列来关联
表示u表的name字段引用了本表(r表)的索引字段(role_name_index
测试完毕删除索引:
drop index role_name_index on role;
根据表统计信息及选用情况,大致估算出找到所需的记录或所需读取的行数。
explain select * from user;
explain select * from role;
user表中有1条记录,role表中有3条记录。
返回结果与实际结果的差值占总记录数的百分比
insert into user values(2,'ls','4');
explain select * from user u inner join role r on u.role_id=r.id;
r表实际记录3条,上述sql语句关联查询出来的结果只能得出一条结果集,因此命中率为33.33%。
查询此SQL语句查询的记录数。
select * from user u inner join role r on u.role_id=r.id;
只有一条记录
显示其他扩展信息
explain select name from user order by name;
explain select name from user group by name;
create index user_name_index on user(name); -- 创建索引
explain select name from user order by name;
测试完毕删除索引:
drop index user_name_index on user;
explain select * from user where name='zs';
explain select * from user where id=1;
性能对比:
Using index > NULL > Using where >= Using temporary > Using filesort
MySQL执行计划的内容是SQL调优时的一个重要依据,我们想要优化SQL语句就必须得先掌握执行计划。这一篇主要是理论知识,也没什么好总结的。
总结一句话吧,想做MySQL调优,执行计划是必须要掌握的,切记切记!