View slow SQL in MySQL

Enter the MySQL command line tool. You can log in to MySQL by typing mysql -u username -p in the terminal and then entering your password.

Enter the following command to enable the slow query log:

SET GLOBAL slow_query_log = 'ON';
SET GLOBAL long_query_time = 1;
SET GLOBAL slow_query_log_file = '/var/log/mysql/slow-query.log';

This command will open the slow query log, set the slow query threshold to 1 秒, and save the log file in /var/log/mysql/slow-query.log.

Check the slow query log. You can use the following command to view the log file:

tail -f /var/log/mysql/slow-query.log

This command will output the latest content of the slow query log in real time.

In addition, MySQL also provides some tools to analyze slow query logs, such as mysqldumpslowand pt-query-digest. Using these tools makes it easier to analyze and optimize slow SQL.

Guess you like

Origin blog.csdn.net/Lance_welcome/article/details/129696017