Skip to content

MySQL slow query logging

MySQL slow query logging is a diagnostic feature that records SQL statements which take longer than a specified threshold to execute.^[600-developer-database-mysql-mysql-index.md] This mechanism helps database administrators and developers identify inefficient queries that require optimization for better performance.

Configuration

The status and variables related to slow query logging can be inspected and modified dynamically.

To check whether the logging is enabled and to see the related settings, you can use the following command:

show variables like '%slow_query_log%';

There are two primary methods for enabling this feature:

  • Temporary (Runtime): You can enable it immediately for the current session using the set global command^[600-developer-database-mysql-mysql-index.md].
    set global slow_query_log=1;
    
  • Permanent: To make the setting persistent across database restarts, you must modify the server's configuration file (usually my.cnf) by adding the following line^[600-developer-database-mysql-mysql-index.md].
    slow_query_log = 1
    

Sources

^[600-developer-database-mysql-mysql-index.md]