0
点赞
收藏
分享

微信扫一扫

MySQL四种SQL性能分析工具

kolibreath 2022-06-01 阅读 42

目录

MySQL四种SQL性能分析工具

1.SQL性能分析

2.查看SQL执行频率

SHOW GLOBAL STATUS LIKE 'Com_______';

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ad0sRgvJ-1653958015675)(C:/Users/86158/AppData/Roaming/Typora/typora-user-images/image-20220530215002870.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-IeJcm7UH-1653958015676)(C:/Users/86158/AppData/Roaming/Typora/typora-user-images/image-20220530215121823.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ykPMVwIg-1653958015676)(C:/Users/86158/AppData/Roaming/Typora/typora-user-images/image-20220530215139863.png)]

3.慢查询日志

3.1概念

3.2查看慢查询日志变量、文件、时间

SHOW VARIABLES LIKE 'slow_query%';

在这里插入图片描述

SHOW VARIABLES LIKE 'long_query_time';

在这里插入图片描述

3.3.开启慢查询日志

SET GLOBAL slow_query_log=ON;
SET GLOBAL slow_query_log=OFF;

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ceg9MDMi-1653958015677)(C:/Users/86158/AppData/Roaming/Typora/typora-user-images/image-20220530215621819.png)]

3.4设置慢查询时间

设置慢日志的时间为2秒,SQL语句执行时间超过2秒,就会视为慢查询,记录慢查询日志

在这里插入图片描述

在这里插入图片描述

3.5查看慢查询日志文件

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-v8uf62H6-1653958015677)(C:/Users/86158/AppData/Roaming/Typora/typora-user-images/image-20220530215736528.png)]

3.6使用慢查询日志

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1QibvB9f-1653958015678)(C:/Users/86158/AppData/Roaming/Typora/typora-user-images/image-20220530215853599.png)]

在这里插入图片描述

3.7检查慢查询日志

最终我们发现,在慢查询日志中,只会记录执行时间超多我们预设时间(2s)的SQL,执行较快的SQL是不会记录的;
那这样,通过慢查询日志,就可以定位出执行效率比较低的SQL,从而有针对性的进行优化

在这里插入图片描述

4.profile详情

4.1profile

4.2查看是否支持profile

SELECT @@have_profiling ;

在这里插入图片描述

4.3查看profile开关

select @@profiling;

在这里插入图片描述

4.4开启profiling

 SET GLOBAL profiling = 1; -- 全局模式
 
 SET profiling = 1; -- session模式

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-fbMAYTuu-1653958015679)(C:/Users/86158/AppData/Roaming/Typora/typora-user-images/image-20220530220338362.png)]

4.5使用profile

--提前创建好这个表
create table tb_user(
id int primary key auto_increment comment '主键',
name varchar(50) not null comment '用户名',
phone varchar(11) not null comment '手机号',
email varchar(100) comment '邮箱',
profession varchar(11) comment '专业',
age tinyint unsigned comment '年龄',
gender char(1) comment '性别 , 1: 男, 2: 女',
status char(1) comment '状态',
createtime datetime comment '创建时间'
) comment '系统用户表';
INSERT INTO tb_user (name, phone, email, profession, age, gender, status,
createtime) VALUES ('吕布', '17799990000', 'lvbu666@163.com', '软件工程', 23, '1',
'6', '2001-02-02 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status,
createtime) VALUES ('曹操', '17799990001', 'caocao666@qq.com', '通讯工程', 33,
'1', '0', '2001-03-05 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status,
createtime) VALUES ('赵云', '17799990002', '17799990@139.com', '英语', 34, '1',
'2', '2002-03-02 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status,
createtime) VALUES ('孙悟空', '17799990003', '17799990@sina.com', '工程造价', 54,
'1', '0', '2001-07-02 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status,
createtime) VALUES ('花木兰', '17799990004', '19980729@sina.com', '软件工程', 23,
'2', '1', '2001-04-22 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status,
createtime) VALUES ('大乔', '17799990005', 'daqiao666@sina.com', '舞蹈', 22, '2',
'0', '2001-02-07 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status,
createtime) VALUES ('露娜', '17799990006', 'luna_love@sina.com', '应用数学', 24,
'2', '0', '2001-02-08 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status,
createtime) VALUES ('程咬金', '17799990007', 'chengyaojin@163.com', '化工', 38,
'1', '5', '2001-05-23 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status,
createtime) VALUES ('项羽', '17799990008', 'xiaoyu666@qq.com', '金属材料', 43,
'1', '0', '2001-09-18 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status,
createtime) VALUES ('白起', '17799990009', 'baiqi666@sina.com', '机械工程及其自动
化', 27, '1', '2', '2001-08-16 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status,
createtime) VALUES ('韩信', '17799990010', 'hanxin520@163.com', '无机非金属材料工
程', 27, '1', '0', '2001-06-12 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status,
createtime) VALUES ('荆轲', '17799990011', 'jingke123@163.com', '会计', 29, '1',
'0', '2001-05-11 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status,
createtime) VALUES ('兰陵王', '17799990012', 'lanlinwang666@126.com', '工程造价',
44, '1', '1', '2001-04-09 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status,
createtime) VALUES ('狂铁', '17799990013', 'kuangtie@sina.com', '应用数学', 43,
'1', '2', '2001-04-10 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status,
createtime) VALUES ('貂蝉', '17799990014', '84958948374@qq.com', '软件工程', 40,
'2', '3', '2001-02-12 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status,
createtime) VALUES ('妲己', '17799990015', '2783238293@qq.com', '软件工程', 31,
'2', '0', '2001-01-30 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status,
createtime) VALUES ('芈月', '17799990016', 'xiaomin2001@sina.com', '工业经济', 35,
'2', '0', '2000-05-03 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status,
createtime) VALUES ('嬴政', '17799990017', '8839434342@qq.com', '化工', 38, '1',
'1', '2001-08-08 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status,
createtime) VALUES ('狄仁杰', '17799990018', 'jujiamlm8166@163.com', '国际贸易',
30, '1', '0', '2007-03-12 00:00:00');
CREATE INDEX idx_user_name ON tb_user(name); --创建的是一般索引
CREATE UNIQUE INDEX idx_user_phone ON tb_user(phone);
CREATE INDEX idx_user_pro_age_sta ON tb_user(profession,age,status);
CREATE INDEX idx_email ON tb_user(email);
DROP INDEX idx_email ON table_name ;

4.5.1查看每条SQL耗时

show profiles; -- 查看每一条SQL的耗时基本情况

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-C3EXbIck-1653958015680)(C:/Users/86158/AppData/Roaming/Typora/typora-user-images/image-20220530220619349.png)]

select * from tb_user;
select * from tb_user where id = 1;
select * from tb_user where name = '白起';

在这里插入图片描述

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7HrqPhUM-1653958015680)(C:/Users/86158/AppData/Roaming/Typora/typora-user-images/image-20220530220648198.png)]

4.5.2查看指定SQL各阶段耗时

show profile for query query_id; -- 查看指定query_id的SQL语句各个阶段的耗时情况

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-CTFtQyJY-1653958015681)(C:/Users/86158/AppData/Roaming/Typora/typora-user-images/image-20220530220813608.png)]

4.5.3查看指定SQL各阶段CPU使用情况

show profile cpu for query query_id; -- 查看指定query_id的SQL语句CPU的使用情况

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-WMQYzGkn-1653958015681)(C:/Users/86158/AppData/Roaming/Typora/typora-user-images/image-20220530220900698.png)]

5.explain

5.1使用explain

-- 直接在select语句之前加上关键字 explain / desc
EXPLAIN SELECT 字段列表 FROM 表名 WHERE 条件 ;

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-E89YIzhV-1653958015681)(C:/Users/86158/AppData/Roaming/Typora/typora-user-images/image-20220530221342980.png)]

5.2explain字段含义

Explain 执行计划中各个字段的含义:

5.3id字段(id相同情况)

create table student( 
id int auto_increment primary key comment '主键ID', 
name varchar(10) comment '姓名', 
no varchar(10) comment '学号' ) comment '学生表'; 
insert into student values (null, '黛绮丝', '2000100101'),(null, '谢逊', '2000100102'),(null, '殷天正', '2000100103'),(null, '韦一笑', '2000100104'); 

create table course( 
id int auto_increment primary key comment '主键ID', 
name varchar(10) comment '课程名称'
 ) comment '课程表';
insert into course values (null, 'Java'), (null, 'PHP'), (null , 'MySQL') , (null, 'Hadoop'); 

create table student_course( 
id int auto_increment comment '主键' primary key, 
studentid int not null comment '学生ID', 
courseid int not null comment '课程ID', 
constraint fk_courseid foreign key (courseid) references course (id), constraint fk_studentid foreign key (studentid) references student (id) )comment '学生课程中间表'; 
insert into student_course values (null,1,1),(null,1,2),(null,1,3),(null,2,2), (null,2,3),(null,3,4);

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kiOBFzuv-1653958015682)(C:/Users/86158/AppData/Roaming/Typora/typora-user-images/image-20220531083154442.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UOYW19if-1653958015682)(C:/Users/86158/AppData/Roaming/Typora/typora-user-images/image-20220531083205896.png)]

-- 为student表起别名字s
select s.*,c.* from student s ,course , student_course sc where s.id == sc.studnetid  and c.id = sc.courseid;

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0QxIaM5p-1653958015682)(C:/Users/86158/AppData/Roaming/Typora/typora-user-images/image-20220531083245618.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xR71A5VO-1653958015683)(C:/Users/86158/AppData/Roaming/Typora/typora-user-images/image-20220531083641065.png)]

5.4id字段(id不同情况)

 select id from course c where c.name ='MySQL';
 select studentid from student_course sc where sc.courseid = 3;
 select * from student s where s.id in (1,2);

SELSECT

在这里插入图片描述
在这里插入图片描述

 select * from student s where s.id in (select studentid from student_course sc where sc.courseid =(selECt id from course c where c.name ='MySQL'));

在这里插入图片描述

5.5type

在这里插入图片描述

在这里插入图片描述

5.6possible_key、key、key_len、rows

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-CU1EpWpZ-1653958015683)(C:/Users/86158/AppData/Roaming/Typora/typora-user-images/image-20220531084545528.png)]

举报

相关推荐

0 条评论