How to Enable Slow Query Log

Introduction The Slow Query Log is a vital feature in database management systems like MySQL and MariaDB that helps identify queries taking longer than expected to execute. Enabling and analyzing the Slow Query Log is crucial for optimizing database performance, reducing load times, and improving the overall user experience of applications. By capturing slow-running queries, developers and databas

Nov 17, 2025 - 11:14
Nov 17, 2025 - 11:14
 2

Introduction

The Slow Query Log is a vital feature in database management systems like MySQL and MariaDB that helps identify queries taking longer than expected to execute. Enabling and analyzing the Slow Query Log is crucial for optimizing database performance, reducing load times, and improving the overall user experience of applications. By capturing slow-running queries, developers and database administrators can pinpoint inefficiencies, optimize SQL statements, and tune database parameters effectively.

In this comprehensive tutorial, we will walk you through everything you need to know about enabling the Slow Query Log, best practices for its use, useful tools, real-world examples, and answers to frequently asked questions. Whether you are a developer, DBA, or system administrator, this guide will help you leverage the Slow Query Log for better database performance.

Step-by-Step Guide

1. Understand the Slow Query Log

The Slow Query Log is a log file that records queries which exceed a specified execution time threshold. Typically, it includes the SQL statement, execution time, and other performance metrics. This data is fundamental for identifying bottlenecks in query execution.

2. Prerequisites

Before enabling the Slow Query Log, ensure you have:

  • Access to the database server with sufficient privileges (usually root or admin).
  • Knowledge of your database version since commands can slightly differ.
  • A backup strategy in place to avoid accidental data loss.

3. Check Current Configuration

Begin by checking if the Slow Query Log is already enabled and its current settings.

SHOW VARIABLES LIKE 'slow_query_log';

SHOW VARIABLES LIKE 'long_query_time';

SHOW VARIABLES LIKE 'slow_query_log_file';

This will display whether the log is active, the time threshold for slow queries (in seconds), and the path to the log file.

4. Enable Slow Query Log Temporarily

To enable it without restarting the server (useful for testing), use the following SQL commands:

SET GLOBAL slow_query_log = 'ON';

SET GLOBAL long_query_time = 2; -- Example threshold in seconds

SET GLOBAL slow_query_log_file = '/var/log/mysql/mysql-slow.log';

Note: The file path should be writable by the database process.

5. Enable Slow Query Log Permanently

To keep the Slow Query Log enabled after server restarts, edit the database configuration file:

  • MySQL: Typically /etc/mysql/my.cnf or /etc/my.cnf
  • MariaDB: Usually /etc/mysql/mariadb.conf.d/50-server.cnf

Add or modify the following under the [mysqld] section:

slow_query_log = 1

slow_query_log_file = /var/log/mysql/mysql-slow.log

long_query_time = 2

log_queries_not_using_indexes = 1

The log_queries_not_using_indexes option is optional but helpful for identifying inefficient queries.

6. Restart the Database Server

After saving changes, restart the database server to apply the new settings:

sudo systemctl restart mysql   

For MySQL

sudo systemctl restart mariadb

For MariaDB

7. Verify Slow Query Log Activation

Run this command again to ensure its enabled:

SHOW VARIABLES LIKE 'slow_query_log';

You should see slow_query_log set to ON.

8. Monitor and Analyze the Log

After enabling it, slow queries will be logged at the specified location. Use commands like tail or less to view the log:

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

To analyze the log, you can use tools like mysqldumpslow or pt-query-digest (covered later).

Best Practices

1. Set an Appropriate Threshold

Choose a long_query_time that balances between capturing meaningful slow queries and avoiding excessive logging. For many applications, 1-2 seconds is a good starting point.

2. Monitor Log Size

Slow Query Logs can grow quickly, especially on busy servers. Implement log rotation using system tools like logrotate to prevent disk space issues.

3. Enable Logging for Queries Not Using Indexes

Setting log_queries_not_using_indexes to 1 helps identify queries that might cause full table scans, which usually degrade performance.

4. Use the Log for Continuous Improvement

Regularly review the slow query log and optimize problematic queries by adding appropriate indexes, rewriting inefficient SQL, or adjusting schema design.

5. Avoid Enabling on Production Without Testing

Although enabling the Slow Query Log is generally safe, on very high-traffic systems it can add slight overhead. Always test configurations on staging environments first.

6. Secure Log Files

Ensure that slow query log files are protected with correct permissions to avoid unauthorized access to potentially sensitive query information.

Tools and Resources

1. mysqldumpslow

A built-in MySQL utility to summarize and analyze slow query logs. It groups similar queries and provides execution counts, average times, etc.

mysqldumpslow /var/log/mysql/mysql-slow.log

2. pt-query-digest

Part of Percona Toolkit, pt-query-digest provides advanced analysis of slow query logs, including detailed reports and query fingerprinting.

Install Percona Toolkit and run:

pt-query-digest /var/log/mysql/mysql-slow.log

3. MySQL Enterprise Monitor

A commercial product from Oracle that provides real-time monitoring and alerting for MySQL, including slow query analysis.

4. phpMyAdmin

Some web-based database management tools like phpMyAdmin offer interfaces to view and analyze slow query logs.

5. Official Documentation

Refer to the official MySQL and MariaDB documentation for the most accurate and version-specific information:

Real Examples

Example 1: Enable Slow Query Log on MySQL 8.0

Suppose you have a MySQL 8.0 server running on Ubuntu. To enable the slow query log permanently with a threshold of 1 second:

  1. Edit /etc/mysql/mysql.conf.d/mysqld.cnf and add:
[mysqld]

slow_query_log = 1

slow_query_log_file = /var/log/mysql/mysql-slow.log

long_query_time = 1

log_queries_not_using_indexes = 1

  1. Restart MySQL:
sudo systemctl restart mysql
  1. Verify:
mysql -e "SHOW VARIABLES LIKE 'slow_query_log';"

Example 2: Analyze Slow Queries Using pt-query-digest

After accumulating slow queries, run:

pt-query-digest /var/log/mysql/mysql-slow.log > slow_query_report.txt

This generates a detailed report highlighting the most problematic queries and their impact on performance.

Example 3: Log Queries Not Using Indexes

Set log_queries_not_using_indexes = 1 to capture all queries that do not utilize indexes, which often cause slowness. This helps identify queries needing indexing improvements.

FAQs

Q1: Will enabling the Slow Query Log affect database performance?

A: There is a minor overhead when logging slow queries, but it is generally negligible. However, on extremely busy systems, excessive logging can impact performance, so monitor carefully.

Q2: How do I change the slow query time threshold?

A: You can change long_query_time dynamically using:

SET GLOBAL long_query_time = X;

Replace X with the desired number of seconds.

Q3: Can I log queries that use indexes?

A: By default, only slow queries exceeding the threshold are logged. To log all queries, including those using indexes, you would need to enable the general query log, which is more resource-intensive.

Q4: How do I rotate slow query logs?

A: Use system tools like logrotate with a configuration that periodically archives and clears the slow query log to prevent disk space issues.

Q5: Are there alternatives to the Slow Query Log?

A: Yes, tools like Performance Schema in MySQL provide more granular monitoring, but the Slow Query Log remains a simple and effective option.

Conclusion

Enabling the Slow Query Log is an essential step in maintaining and optimizing database performance. By systematically capturing and analyzing slow-running queries, you can identify inefficiencies, improve SQL execution, and enhance application responsiveness. This tutorial provided a thorough guide on how to enable the Slow Query Log, best practices to follow, tools to leverage, and practical examples to get started immediately. Implementing these strategies will help you maintain a healthy database environment and deliver a better experience to your users.