NOTE

1.18 MySQL Slow Query Log

The MySQL slow query log records SQL statements whose execution time exceeds a threshold, helping identify queries for further analysis.

DatabasesCreated Updated 1 min readhistorical

This is a historical learning note and may contain outdated or incomplete understanding.

1. What It Is

The slow query log records statements whose execution time exceeds a specified threshold.

show variables like '%slow_query_log%';
+---------------------+------------------------+
| Variable_name       | Value                  |
+---------------------+------------------------+
| slow_query_log      | OFF                    |
| slow_query_log_file | <example>-slow.log     |
+---------------------+------------------------+
show variables like '%long_query_time%';
+-----------------+-----------+
| Variable_name   | Value     |
+-----------------+-----------+
| long_query_time | 10.000000 |
+-----------------+-----------+

2. Usage

2.1. How to Enable It

set global slow_query_log=1;
set global long_query_time=3;

Reconnect to the database for the changes to take effect.

2.2. How to View It

  • View the log directly
tail -f /var/lib/mysql/<example>-slow.log -n 200
  • Use the mysqldumpslow command

3. References

https://mariadb.com/kb/en/library/documentation/mariadb-administration/server-monitoring-logs/slow-query-log/slow-query-log-overview/ https://dev.mysql.com/doc/refman/8.0/en/slow-query-log.html

Discussion

Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub