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


边栏推荐
- 力扣刷题之求两数之和
- CC2530_ZigBee+华为云IOT:设计一套属于自己的冷链采集系统
- 余弦距离介绍
- online 方式创建索引触发trigger怎么办?
- 一文搞懂│php 中的 DI 依赖注入
- awk语法-02-运算、数组、格式化输出
- 阿里巴巴政委体系-第六章、阿里政委体系运作
- Rust:多线程并发编程
- Confused!Ali was abused on the one hand, but was fortunate to be promoted to Huawei's technology, and successfully got the offer, with an annual salary of 40w
- G6尝试 学习
猜你喜欢

【木马免杀】

阿里巴巴政委体系-第五章、阿里政委体系建设

国产虚拟化云宏CNware WinStack安装体验-5 开启集群HA

MySQL如何 drop 大表

Bytes to beat three sides take offer: network + GC + + IO + redis + JVM red-black tree + data structure, to help you quickly into the giant!!!!!

Zhong Hua, senior architect of Ali: China-Taiwan strategic thinking and architecture practice; including internal implementation manual

CS免杀姿势
![[笔记]机器学习之前言介绍](/img/69/e2acd3efd5f513c9c32fca701b66c0.png)
[笔记]机器学习之前言介绍

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

盲僧发现了华点——教你如何使用API接口获取数据
随机推荐
typescript学习笔记
红日安全内网渗透靶场-VulnStack-1
pytest接口自动化测试框架 | Jenkins集成初探
docker mysql 容器中执行mysql脚本文件并解决乱码
unity3d-游戏物体控制方法
力扣刷题之有效的正方形(每日一题7/29)
面试突击:什么是粘包和半包?怎么解决?
【QT】入门心法
Postgresql快照优化Globalvis新体系分析(性能大幅增强)
PreFixSum前缀和
idea——同一项目开启多个实例(不同端口)
力扣刷题之求两数之和
MYSQL误删数据恢复
WEB 渗透之RCE
阿里二面:多线程间的通信方式有几种?举例说明
【ORACLE】什么时候ROWNUM等于0和ROWNUM小于0,两个条件不等价?
The addition and subtraction of the score of the force deduction brush question (a daily question 7/27)
Force is brushed buckle problem for the sum of two Numbers
标准C语言学习总结11
金鱼哥RHCA回忆录:CL210管理计算资源--管理计算节点+章节实验