About slow query log in mysql

slow query log

The slow query log is mainly used to record SQL statements whose execution time exceeds a certain period of time, which can help database maintenance personnel find SQL statements with relatively long execution time and low execution efficiency, and perform targeted optimization on these SQL statements.


Enable slow query

my.cnfThe slow query log can my.inibe configured in the file or file.


[mysqld]
slow_query_log = 1
slow_query_log_file = /data/mysql/log/query_log/slow_statement.log
long_query_time = 10
log_output = FILE

Each configuration item is described as follows:

slow_query_log: Specifies whether to enable the slow query log. If the specified value is 1 or no value is specified, the slow query log will be enabled; if the specified value is 0 or this option is not configured, the slow query log will not be enabled.

slow_query_log_file: The file location of the slow query log.

long_query_time: Specify the number of seconds to record the slow query log when the execution time of the SQL statement exceeds the number of seconds.

log_output: It is the same as the log_output option of the query log, and will not be described here.

Note: log_outputIt can be configured to record the log in the data table or in the file. When the log is recorded in the data table, the slow query time recorded in the data table can only be accurate to seconds; if it is recorded in the log file, the log The slow query time recorded in the file can be accurate to microseconds . It is recommended to record slow query logs to files in actual work.

After the configuration is complete, restart the MySQL server for the configuration to take effect.

In addition to configuring and enabling the slow query log in the file, you can also execute the following command on the MySQL command line to enable the slow query log.


mysql> SET GLOBAL slow_query_log = 1;
Query OK, 0 rows affected (0.00 sec)
mysql> SET GLOBAL slow_query_log_file = '/data/mysql/log/query_log/slow_statement.log';
Query OK, 0 rows affected (0.00 sec)
mysql> SET GLOBAL long_query_time = 10;
Query OK, 0 rows affected (0.00 sec)
mysql> SET GLOBAL log_output = 'FILE';
Query OK, 0 rows affected (0.00 sec)

After the slow query log is successfully enabled /data/mysql/log/query_log, slow_statement.loga file will be generated in the directory.

View slow query log

If the slow query log is configured to output to a file, it will be saved in a plain text file, and you can directly view the content of the plain text file.

  • Construct a SQL statement that takes more than 10 seconds to query.
SELECT BENCHMARK(99999999, MD5('mysql'));

insert image description here
This statement took about time: 19.102s

Let's look at the records of the slow query log file as follows:
insert image description here

Delete slow query logs

Like query logs, slow query logs are stored in the server disk in the form of plain text files and can be deleted directly. If you need to regenerate the slow query log, you can run FLUSH LOGSthe command , or execute mysqladmin flush-logsthe command on the server command line.


(1) Delete the slow query log.

rm -rf /data/mysql/log/query_log/slow_statement.log

After deletion, check /data/mysql/log/query_logthe files in the directory.

The results show that the slow_statement.log file has been successfully deleted.

(2) Refresh the log on the MySQL command line.

mysql> FLUSH LOGS;
Query OK, 0 rows affected (0.01 sec)

Or execute the following command on the server command line to refresh the log.


mysqladmin -uroot -p flush-logs
Enter password:

After the log is successfully refreshed, check /data/mysql/log/query_logthe files in the directory again.

MySQL recreates the slow_statement.log file.

Turn off the slow query log

To turn off the slow query log, you only need to configure slow_query_log=0 in the my.cnf file or my.ini file or delete this option directly.


[mysqld]
slow_query_log = 0

You can also run the following command on the MySQL command line to disable the slow query log.

mysql> SET GLOBAL slow_query_log = 0;
Query OK, 0 rows affected (0.00 sec)

After closing the slow query log, delete the slow query log file, and then execute the log refresh operation, MySQL will no longer recreate the slow query log file.

Guess you like

Origin blog.csdn.net/weixin_36754290/article/details/129203039