In mysql, a slow query is a SQL statement that is recorded in the log and runs slowly. It refers to a SQL statement query that exceeds the time threshold set by the "long_query_time" parameter. Slow queries are recorded in the slow query log. Through the slow query log, you can find out which query statements have low execution efficiency for optimization.
The operating environment of this tutorial: windows7 system, mysql8 version, Dell G3 computer.
Slow query, as the name suggests, is to record SQL statements that run slowly in the log. It means that mysql records all SQL statements that exceed the time threshold set by the long_query_time parameter. Inquire.
Slow queries are recorded in the slow query log. Through the slow query log, you can find out which query statements have low execution efficiency for optimization. This log can bring great help to the optimization of SQL statements.
By default, the slow query log is turned off. To use the slow query log function, you must first enable the slow query log function.
show VARIABLES like '%slow_query_log%' show VARIABLES like '%slow_query_log_file%' show VARIABLES like '%long_query_time%' show VARIABLES like '%log_queries_not_using_indexes%' show VARIABLES like 'log_output'
set global long_query_time=0; ---默认10秒,这里为了演示方便设置为0 set GLOBAL slow_query_log = 1; --开启慢查询日志 set global log_output='FILE,TABLE' --项目开发中日志只能记录在日志文件中,不能记表中
# 添加慢查询日志 log_output=file slow_query_log=on slow_query_log_file = /tmp/mysql-slow.log log_queries_not_using_indexes=on long_query_time = 1
mysql> SET GLOBAL slow_query_log=ON; Query OK, 0 rows affected (0.05 sec) mysql> SET GLOBAL long_query_time=0.001; Query OK, 0 rows affected (0.00 sec)
mysql> USE test; Database changed mysql> SELECT * FROM tb_student; +----+--------+ | id | name | +----+--------+ | 1 | Java | | 2 | MySQL | | 3 | Python | +----+--------+ 3 rows in set (0.08 sec)
# Time: 2020-06-01T01:59:18.368780Z # User@Host: root[root] @ localhost [::1] Id: 3 # Query_time: 0.006281 Lock_time: 0.000755 Rows_sent: 2 Rows_examined: 1034 use test; SET timestamp=1590976758; SHOW VARIABLES LIKE 'slow_query%';
mysqladmin -uroot -p flush-logs
Note: Both the general query log and the slow query log use this command. When using it, you must pay attention. Once this command is executed, the general query log and the slow query log will only exist in the new log file. . If you need to back up old slow query log files, you must first rename the old logs, then restart the MySQL service or execute the mysqladmin command.[Related recommendations:
mysql video tutorial]
The above is the detailed content of what is mysql slow query. For more information, please follow other related articles on the PHP Chinese website!