当前位置:网站首页>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


边栏推荐
- [Notes] Introduction to machine learning
- MySQL超详细安装教程 手把手教你安装MySQL到使用MySQL 最简单的MySQL安装方式,这种方式装,卸载也简单
- flex布局
- docker mysql 容器中执行mysql脚本文件并解决乱码
- Execute the mysql script file in the docker mysql container and solve the garbled characters
- pytest接口自动化测试框架 | 基于Pytest的Web UI自动化测试框架介绍
- [Dataset][VOC] Rat dataset voc format 3001 sheets
- Postgresql source code (65) analysis of the working principle of the new snapshot system Globalvis
- Unity gets the actual coordinates of the ui on the screen under the canvas
- JumpServer开源堡垒机完成龙芯架构兼容性认证
猜你喜欢

云图说丨初识华为云微服务引擎CSE

首届MogDB征文活动开启啦!

线上一次JVM FullGC搞得整晚都没睡,彻底崩溃

OneNote 教程,如何在 OneNote 中设置页面格式?

阿里巴巴政委体系-第八章、阿里政委工作方法论

Protobuf Grpc使用异常 类型有未导出的方法,并且是在不同的软件包中定义

基于ck+redash构建MySQL慢日志+审计日志展示平台

微信小程序分享功能

Handler 源码解析

Alibaba senior experts create a learning architecture from scratch, including Alibaba's internal technology stack PPT, PFD actual combat
随机推荐
WEB 渗透之RCE
【C语言学习笔记(六)】分支与跳转(if、else、continue、break、switch)
Cobalt Strike (CS) 逆向初探
懵逼!阿里一面被虐了,幸获内推华为技术四面,成功拿到offer,年薪40w
要想成为黑客,离不开这十大基础知识
MySQL详细学习教程(建议收藏)
标准C语言学习总结11
丙二醇二乙酸酯(Propylene Glycol Diacetate)
Postgresql快照优化Globalvis新体系分析(性能大幅增强)
typescript学习笔记
pg_memory_barrier_impl in Postgresql and C's volatile
【ORACLE】什么时候ROWNUM等于0和ROWNUM小于0,两个条件不等价?
Solution for no navigation bar after Word is saved as PDF
选出表中的中位数记录[构造左右边界 || 问题转换]
ScrollView嵌套RV,滑动有阻力不顺滑怎么办?
手把手教你定位线上MySQL慢查询问题,包教包会
Postgresql中的pg_memory_barrier_impl和C的volatile
2022年最新的Android面试大厂必考174题(附带详细答案)
Alibaba senior experts create a learning architecture from scratch, including Alibaba's internal technology stack PPT, PFD actual combat
阿里巴巴政委体系-第九章、阿里政委启示录