当前位置:网站首页>Teach you to locate online MySQL slow query problem hand by hand, package teaching package meeting
Teach you to locate online MySQL slow query problem hand by hand, package teaching package meeting
2022-08-03 19:13:00 【Shum technology curtilage】
1. The role of slow query log
The slow query log is not enabled by default. It is recommended to manually enable it to help us locate online problems.
SQL whose execution time exceeds the threshold will be written to the slow query log, which can help us record SQL statements that execute too long, locate online slow SQL problems, and facilitate SQL performance tuning.
2. Configuration of slow query log
2.1 Check whether the slow query log is enabled
show variables like 'slow_query_log';Copy code
The default is OFF, if it is not turned on, it can be turned on manually.
2.2 Enable slow query log
One way is to use the MySQL command to open:
set global slow_query_log=1;Copy code
Another method is to modify the MySQL configuration file, restart the MySQL service, and enable it.
Modify the configuration file my.cnf and add the following command
slow_query_log = ON
2.3 Set the threshold of slow query log
The default threshold for slow query logs is 10, in seconds.
For an online service, 10 seconds is too long, we can modify it manually.
One is to modify it through MySQL commands, such as modifying it to 1 second:
set long_query_time=1;Copy code
Another method is to modify the MySQL configuration file, restart the MySQL service, and enable it.
Modify the configuration file my.cnf and add the following command
long_query_time = 1
2.4 Modify slow query log location
Use the MySQL command to view the slow query log location:
show variables like '%slow_query_log_file%';Copy code
If you want to modify the slow query log location, you can modify the MySQL configuration file, restart the MySQL service, and enable it.
Modify the configuration file my.cnf and add the following command
slow_query_log_file = /usr/local/mysql/data/localhost_slow.log
2.5 Record more slow query SQL
By default, management statements are not recorded in the slow query log. Management statements include ALTER TABLE, ANALYZE TABLE, CHECK TABLE, CREATE INDEX, DROP INDEX, OPTIMIZE TABLE, and REPAIR TABLE.
Management statements are also very important. If you want to be logged, you can modify them through MySQL commands:
set global log_slow_admin_statements=ON;Copy code
By default, statements that do not use an index will not be logged.
Enough to deceive people!If you are not careful, you will fall into the pit. If you don't record the statements that do not use the index, why do you need to query the log slowly?
If you want to record a statement that does not use an index, you can modify it with the command:
set global log_queries_not_using_indexes=ON;Copy code
3. Use of slow query log
Manually create a slow SQL to test the effect. There are 1 million table data in the user table:
select * from user;Copy code
Then take a look at the contents of the slow query log file:
cat /usr/local/mysql/data/localhost_slow.logCopy code
SQL statements and execution times are logged.
4. Analyze slow query logs
Sometimes there are many slow query logs, and it is not very convenient to view them manually. Fortunately, MySQL provides a tool for analyzing slow query logs mysqldumpslow .
Common parameters are-s: Indicates how to sort:c: number of visitsl: lock timer: return recordt: query timeal: average lock timear: Average number of records returnedat: average query time-t: Return the number of previous data;Copy code
4.1 The 10 SQLs with the most returned results:
mysqldumpslow -s r -t 10 /usr/local/mysql/data/localhost_slow.log
4.2 The 10 SQLs with the longest query time:
mysqldumpslow -s t -t 10 /usr/local/mysql/data/localhost_slow.log
边栏推荐
- Shell programming loop statement
- Alibaba senior experts create a learning architecture from scratch, including Alibaba's internal technology stack PPT, PFD actual combat
- 一文搞懂│php 中的 DI 依赖注入
- 网络协议-TCP、UDP区别及TCP三次握手、四次挥手
- 力扣解法汇总899-有序队列
- 【Azure 事件中心】使用Azure AD认证方式创建Event Hub Consume Client + 自定义Event Position
- When does MySQL use table locks and when to use row locks?You should know this
- mysql跨库关联查询(dblink)
- MySQL超详细安装教程 手把手教你安装MySQL到使用MySQL 最简单的MySQL安装方式,这种方式装,卸载也简单
- JumpServer开源堡垒机完成龙芯架构兼容性认证
猜你喜欢
随机推荐
LineSegmentTree线段树
讯方实训云平台——加速教育高质量发展的“数字底座”!
[Dataset][VOC] Rat dataset voc format 3001 sheets
Difference差分数组
online 方式创建索引触发trigger怎么办?
Compose原理-compose中是如何实现事件分法的
【计网】二、物理层
Cobalt Strike (CS) 逆向初探
BinomialTree 二叉树
【微信小程序】NFC 标签打开小程序
Postgresql源码(65)新快照体系Globalvis工作原理分析
ScrollView嵌套RV,滑动有阻力不顺滑怎么办?
阿里资深架构师钟华曰:中台战略思想与架构实战;含内部实施手册
关于2022年度深圳市技术攻关重大项目的申报通知
力扣刷题之有效的正方形(每日一题7/29)
字节跳动三面拿offer:网络+IO+redis+JVM+GC+红黑树+数据结构,助你快速进大厂!!
懵逼!阿里一面被虐了,幸获内推华为技术四面,成功拿到offer,年薪40w
201712-3 CCF Crontab满分题解
mysql跨库关联查询(dblink)
Rust:多线程并发编程