当前位置:网站首页>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
边栏推荐
- 【Azure 事件中心】使用Azure AD认证方式创建Event Hub Consume Client + 自定义Event Position
- [数据集][VOC]老鼠数据集voc格式3001张
- LOL英雄联盟卡顿掉帧问题解决办法 2022年8月1日
- Unity gets the actual coordinates of the ui on the screen under the canvas
- 金鱼哥RHCA回忆录:CL210管理计算资源--管理计算节点+章节实验
- WEB 渗透之RCE
- 【WPS-OFFICE-Word】 WPS中样式的运作原理?样式自动更新、自动改变如何处理?样式的管理方法?
- [笔记]机器学习之前言介绍
- Force is brushed buckle problem for the sum of two Numbers
- 【ORACLE】什么时候ROWNUM等于0和ROWNUM小于0,两个条件不等价?
猜你喜欢
随机推荐
Web项目中简单使用线程池
Compose原理-compose中是如何实现事件分法的
力扣刷题之分数加减运算(每日一题7/27)
G6尝试 学习
pg_memory_barrier_impl in Postgresql and C's volatile
pytest接口自动化测试框架 | 基于Pytest的Web UI自动化测试框架介绍
MySQL 啥时候用表锁,啥时候用行锁?这些你都应该知道吧
FreeRTOS中级篇
阿里二面:多线程间的通信方式有几种?举例说明
设备树基本原理与操作方法
MySQL【变量、流程控制与游标】
【ORACLE】什么时候ROWNUM等于0和ROWNUM小于0,两个条件不等价?
OneNote 教程,如何在 OneNote 中设置页面格式?
Postgresql中的pg_memory_barrier_impl和C的volatile
七夕之前,终于整出了带AI的美丽秘笈
Postgresql源码(65)新快照体系Globalvis工作原理分析
Climbing Stairs (7/30)
U-Net生物医学图像分割讲解(Convolutional Networks for BiomedicalImage Segmentation)
MD5是对称加密还是非对称加密,有什么优缺点
MySQL超详细安装教程 手把手教你安装MySQL到使用MySQL 最简单的MySQL安装方式,这种方式装,卸载也简单