当前位置: 首页 > news >正文

mysql慢sql排查与分析

当MySQL遇到慢查询(慢SQL)时,我们可以通过以下步骤进行排查和优化:

标题开启慢查询日志:

确保MySQL的慢查询日志已经开启。通过查看slow_query_logslow_query_log_file变量来确认。
在这里插入图片描述
如果没有开启,可以在MySQL配置文件(如my.cnf(linux)或my.ini(windows))中设置这些变量,然后重启MySQL服务。
在这里插入图片描述

# 开启慢查询日志
slow_query_log = ON
# 设置慢查询的时间阈值,单位秒,查询耗时超过此值的SQL会被记录
long_query_time = 1
# 设置log位置
slow_query_log_file = D:/ai-softwares/mysql/mysql-8.0.32-winx64/data/DESKTOP-6IQ27F1-slow.log
# (可选)记录那些没有使用索引的查询
log_queries_not_using_indexes = 1

保存,重启后,可以看到:
在这里插入图片描述
查询几条数据后,查看慢日志文件内容:

D:\ai-softwares\mysql\mysql-8.0.32-winx64\bin\mysqld, Version: 8.0.32 (MySQL Community Server - GPL). started with:
TCP Port: 3306, Named Pipe: MySQL
Time                 Id Command    Argument
# Time: 2024-04-05T06:33:16.321295Z
# User@Host: root[root] @ localhost [::1]  Id:     8
# Query_time: 0.005313  Lock_time: 0.000010 Rows_sent: 100  Rows_examined: 100
use atguigudb1;
SET timestamp=1712298796;
select * from course;
# Time: 2024-04-05T06:33:39.469804Z
# User@Host: root[root] @ localhost [::1]  Id:     8
# Query_time: 0.037054  Lock_time: 0.000037 Rows_sent: 25  Rows_examined: 25
use atguigudb;
SET timestamp=1712298819;
select * from countries;
# Time: 2024-04-05T06:44:06.818619Z
# User@Host: root[root] @ localhost [::1]  Id:     8
# Query_time: 0.000289  Lock_time: 0.000004 Rows_sent: 25  Rows_examined: 25
SET timestamp=1712299446;
select * from countries;
# Time: 2024-04-05T06:44:42.748596Z
# User@Host: root[root] @ localhost [::1]  Id:     8
# Query_time: 0.000215  Lock_time: 0.000003 Rows_sent: 25  Rows_examined: 25
SET timestamp=1712299482;
select * from countries;
# Time: 2024-04-05T06:44:52.762931Z
# User@Host: root[root] @ localhost [::1]  Id:     8
# Query_time: 0.086578  Lock_time: 0.000010 Rows_sent: 19  Rows_examined: 19
SET timestamp=1712299492;
select * from jobs;
# Time: 2024-04-05T06:44:53.632521Z
# User@Host: root[root] @ localhost [::1]  Id:     8
# Query_time: 0.000169  Lock_time: 0.000002 Rows_sent: 19  Rows_examined: 19
SET timestamp=1712299493;
select * from jobs;
# Time: 2024-04-05T06:44:54.245250Z
# User@Host: root[root] @ localhost [::1]  Id:     8
# Query_time: 0.000166  Lock_time: 0.000001 Rows_sent: 19  Rows_examined: 19
SET timestamp=1712299494;
select * from jobs;
# Time: 2024-04-05T06:44:54.966701Z
# User@Host: root[root] @ localhost [::1]  Id:     8
# Query_time: 0.000171  Lock_time: 0.000002 Rows_sent: 19  Rows_examined: 19
SET timestamp=1712299494;
select * from jobs;
# Time: 2024-04-05T06:44:55.613169Z
# User@Host: root[root] @ localhost [::1]  Id:     8
# Query_time: 0.000160  Lock_time: 0.000002 Rows_sent: 19  Rows_examined: 19
SET timestamp=1712299495;
select * from jobs;

分析慢查询日志:

使用mysqldumpslow或其他慢查询日志分析工具来查看和分析慢查询日志中的条目。
查看MySQL安装目录的bin目录下,没有mysqldumpslow.exe文件,有一个mysqldumpslow.pl文件。
在目录该下,cmd运行命令:perl mysqldumpslow.pl --help查看命令帮助。

D:\ai-softwares\mysql\mysql-8.0.32-winx64\bin>perl mysqldumpslow.pl --help
Usage: mysqldumpslow [ OPTS... ] [ LOGS... ]Parse and summarize the MySQL slow query log. Options are--verbose    verbose--debug      debug--help       write this text to standard output-v           verbose-d           debug-s ORDER     what to sort by (al, at, ar, c, l, r, t), 'at' is defaultal: average lock timear: average rows sentat: average query timec: countl: lock timer: rows sentt: query time-r           reverse the sort order (largest last instead of first)-t NUM       just show the top n queries-a           don't abstract all numbers to N and strings to 'S'-n NUM       abstract numbers with at least n digits within names-g PATTERN   grep: only consider stmts that include this string-h HOSTNAME  hostname of db server for *-slow.log filename (can be wildcard),default is '*', i.e. match all-i NAME      name of server instance (if using mysql.server startup script)-l           don't subtract lock time from total time

比如分析时,指定-s c查询次数次数排序,不用-aN隐藏数字,执行下面命令,分析慢查询日志:
perl "D:\ai-softwares\mysql\mysql-8.0.32-winx64\bin\mysqldumpslow.pl" -s c -a "D:/ai-softwares/mysql/mysql-8.0.32-winx64/data/DESKTOP-6IQ27F1-slow.log"
分析结果如下:

D:\ai-softwares\mysql\mysql-8.0.32-winx64\bin>perl "D:\ai-softwares\mysql\mysql-8.0.32-winx64\bin\mysqldumpslow.pl" -s c -a "D:/ai-softwares/mysql/mysql-8.0.32-winx64/data/DESKTOP-6IQ27F1-slow.log"Reading mysql slow query log from D:/ai-softwares/mysql/mysql-8.0.32-winx64/data/DESKTOP-6IQ27F1-slow.log
Count: 5  Time=0.02s (0s)  Lock=0.00s (0s)  Rows=19.0 (95), root[root]@localhostselect * from jobsCount: 3  Time=0.01s (0s)  Lock=0.00s (0s)  Rows=25.0 (75), root[root]@localhostselect * from countriesCount: 1  Time=0.00s (0s)  Lock=0.00s (0s)  Rows=0.0 (0), 0users@0hostsD:\ai-softwares\mysql\mysql-8.0.32-winx64\bin\mysqld, Version: 8.0.32 (MySQL Community Server - GPL). started with:TCP Port: 3306, Named Pipe: MySQL# Time: 2024-04-05T06:33:16.321295Z# User@Host: root[root] @ localhost [::1]  Id:     8# Query_time: 0.005313  Lock_time: 0.000010 Rows_sent: 100  Rows_examined: 100use atguigudb1;SET timestamp=1712298796;select * from course

可以看到,日志分析,可以通过命令定制化分析,并且对于每个select,有执行的个数、耗时、锁表的时间、查询的行数、用户与host信息:
Count: 3 Time=0.01s (0s) Lock=0.00s (0s) Rows=25.0 (75), root[root]@localhost
可以知道,相比于慢查询日志,它可以对其进行整合,比如将相同的查询SQL计数为count。

EXPLAIN命令:

对于日志中记录的慢查询,使用EXPLAIN命令来查看查询的执行计划。分析查询是否使用了合适的索引,以及是否存在全表扫描等低效操作。
explain select * from jobs;
在这里插入图片描述
可以看到type=ALL,说明是全表扫描,没有进行索引。

优化查询

根据EXPLAIN的输出结果,优化查询语句,比如添加或修改索引。
避免在查询中使用*,而是指定需要的列。
减少JOIN操作的数量或复杂性,特别是在大数据集上。
考虑将计算密集型的操作移到应用层进行。

相关文章:

  • Blender怎么样启动默认移动和Cavity效果
  • 理解 Golang 变量在内存分配中的规则
  • ics-05-攻防世界
  • 爬取高校专业信息的Python爬虫简介与实践
  • 【C++ STL算法】sort 排序
  • 隐私计算实训营学习七:隐语SCQL的架构详细拆解
  • 数据库的基本操作
  • 面试题多态结合线程
  • 【TI毫米波雷达】IWR6843AOP的官方文件资源名称BUG,选择xwr68xx还是xwr64xx,及需要注意的问题
  • 【测试面试题】14题常见APP测试面试题(参考答案)
  • 加州大学欧文分校英语基础语法专项课程02:Questions, Present Progressive and Future Tenses 学习笔记
  • Inotify
  • Linux网络编程二(TCP图解三次握手及四次挥手、TCP滑动窗口、MSS、TCP状态转换、多进程/多线程服务器实现)
  • DNS以及dnsmasq 搭建 dns 局域网(2)
  • 每日五道java面试题之ZooKeeper篇(一)
  • 分享一款快速APP功能测试工具
  • angular2开源库收集
  • Dubbo 整合 Pinpoint 做分布式服务请求跟踪
  • exports和module.exports
  • gcc介绍及安装
  • Gradle 5.0 正式版发布
  • happypack两次报错的问题
  • JSDuck 与 AngularJS 融合技巧
  • Laravel深入学习6 - 应用体系结构:解耦事件处理器
  • leetcode378. Kth Smallest Element in a Sorted Matrix
  • MySQL常见的两种存储引擎:MyISAM与InnoDB的爱恨情仇
  • MySQL用户中的%到底包不包括localhost?
  • VirtualBox 安装过程中出现 Running VMs found 错误的解决过程
  • 阿里中间件开源组件:Sentinel 0.2.0正式发布
  • 编写高质量JavaScript代码之并发
  • 飞驰在Mesos的涡轮引擎上
  • 分布式事物理论与实践
  • 服务器从安装到部署全过程(二)
  • 看图轻松理解数据结构与算法系列(基于数组的栈)
  • 你不可错过的前端面试题(一)
  • 你真的知道 == 和 equals 的区别吗?
  • 区块链将重新定义世界
  • 如何胜任知名企业的商业数据分析师?
  • 三分钟教你同步 Visual Studio Code 设置
  • 通过git安装npm私有模块
  • 小程序01:wepy框架整合iview webapp UI
  • mysql面试题分组并合并列
  • ​中南建设2022年半年报“韧”字当头,经营性现金流持续为正​
  • #pragma once与条件编译
  • #经典论文 异质山坡的物理模型 2 有效导水率
  • #我与Java虚拟机的故事#连载09:面试大厂逃不过的JVM
  • (Python) SOAP Web Service (HTTP POST)
  • (附源码)spring boot公选课在线选课系统 毕业设计 142011
  • (南京观海微电子)——I3C协议介绍
  • (一) storm的集群安装与配置
  • (转)拼包函数及网络封包的异常处理(含代码)
  • *p=a是把a的值赋给p,p=a是把a的地址赋给p。
  • .[backups@airmail.cc].faust勒索病毒的最新威胁:如何恢复您的数据?
  • .mysql secret在哪_MYSQL基本操作(上)
  • .net framework 4.0中如何 输出 form 的name属性。