How to locate slow queries in MySQL?

Some interfaces are very slow during stress testing, and the response time of the interface exceeds 2 seconds. In the operation and maintenance monitoring system Skvwalking, you can see which interface is slower in the displayed report. Analyze this interface, You can see which part is relatively slow, so as to know the specific execution time of SQL, locate which SQL has a problem, and recommend the following two solutions for locating slow queries.

Option 1: Open source tools

Debugging tool: Arthas

Operation and maintenance tools: Prometheus, Skywalking

1682476248124_21.png

Solution 2: The slow log that comes with MySQL

The slow query log records the logs of all SQL statements whose execution time exceeds the specified parameter (long_query_time, unit: second, default 10 seconds). If you want to enable the slow query log, you need to configure the following information in the MySQL configuration file (/etc/my.cnf):

#开启 MySQL 慢日志查询开关
slow_query_log=1
#设置慢日志的时间为2秒,SQL语句执行时间超过2秒,就会视为慢查询,记录慢查询日志
long_query_time=2

After the configuration is complete, restart the MySQL server with the following command to test, and check the information recorded in the slow log file /var/lib/mysql/localhost-slow.log.

The slow log that comes with MySQL

Guess you like

Origin blog.csdn.net/cz_00001/article/details/131834936