Master database optimization: Understand, enable, and leverage the MySQL slow query log on Ubuntu to boost performance.
Estimated Time: Approximately 30 - 45 minutes (initial setup & basic analysis)
The MySQL slow query log is an incredibly valuable tool for identifying performance bottlenecks in your database. It records SQL queries that take longer than a specified amount of time to execute. By analyzing these "slow" queries, you can pinpoint inefficient operations and take steps to optimize them.
Why Monitor Slow Queries?
This guide will take you from enabling the slow query log on your Ubuntu MySQL server to interpreting its output and implementing common optimization techniques.
30 - 45 minutes
(For initial setup and basic analysis. Ongoing optimization is continuous.)
Intermediate
Assumes familiarity with basic MySQL queries, terminal commands, and server administration.
sudo
privileges.
While the slow log is powerful, there are important points to remember:
Use the slow log for diagnosis, not as a permanent "all queries" log on production.
First, let's see if the slow query log is already enabled and what its current configuration is.
sudo mysql -u root -p -e "SHOW VARIABLES LIKE 'slow_query_log%'; SHOW VARIABLES LIKE 'long_query_time'; SHOW VARIABLES LIKE 'log_output';"
Always create a backup of your MySQL configuration file before making changes. The main configuration file on Ubuntu is typically `/etc/mysql/mysql.conf.d/mysqld.cnf`.
sudo cp /etc/mysql/mysql.conf.d/mysqld.cnf /etc/mysql/mysql.conf.d/mysqld.cnf.bak
Now, we'll edit the MySQL configuration file to enable the slow query log and set its parameters.
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
Locate the `[mysqld]` section. Add or uncomment the following lines. We recommend a `long_query_time` of `1` or `2` seconds for initial diagnosis, which can be adjusted later.
# General and Slow logging
slow_query_log = 1
slow_query_log_file = /var/log/mysql/mysql-slow.log
long_query_time = 1
log_output = FILE
# Optional: Log queries that don't use indexes, even if fast. Use with caution!
# log_queries_not_using_indexes = 1
Save the file (`Ctrl+O`, `Enter`) and exit `nano` (`Ctrl+X`).
Ensure the directory and log file exist and have the correct permissions so MySQL can write to it.
sudo mkdir -p /var/log/mysql
sudo touch /var/log/mysql/mysql-slow.log
sudo chown mysql:mysql /var/log/mysql/mysql-slow.log
sudo chmod 640 /var/log/mysql/mysql-slow.log
For the configuration changes to take effect, you must restart the MySQL service.
sudo systemctl restart mysql
Check MySQL status:
sudo systemctl status mysql
Confirm the log is active and test it with a deliberately slow query.
1. Verify variables in MySQL:
sudo mysql -u root -p -e "SHOW VARIABLES LIKE 'slow_query_log'; SHOW VARIABLES LIKE 'long_query_time';"
2. Run a test slow query:
sudo mysql -u root -p -e "SELECT SLEEP(2);"
3. Check the slow log file:
sudo tail /var/log/mysql/mysql-slow.log
Each entry in the slow log provides crucial details about the slow query:
# Time: 2023-10-27T10:30:05.123456Z
# User@Host: someuser[someuser] @ localhost [127.0.0.1] Id: 12345
# Query_time: 2.000010 Lock_time: 0.000000 Rows_sent: 1 Rows_examined: 0
SET timestamp=1698421805;
SELECT SLEEP(2);
Manually sifting through a large slow log is impractical. `mysqldumpslow` is a command-line tool that summarizes slow log contents.
1. Get a summary sorted by average query time (top 10):
sudo mysqldumpslow -s at -t 10 /var/log/mysql/mysql-slow.log
Other useful `mysqldumpslow` options:
Once you've identified slow queries, it's time to optimize! This often involves an iterative process of testing and refining.
Indexes allow MySQL to quickly locate rows without scanning the entire table. Use the `EXPLAIN` keyword to understand how MySQL executes your query.
Using `EXPLAIN`:
EXPLAIN SELECT * FROM users WHERE email = 'test@example.com';
Creating an index:
CREATE INDEX idx_users_email ON users (email);
When to index:
When not to index (or use with caution):
Adjusting MySQL's configuration can significantly impact performance, especially for InnoDB engines.
Sometimes, the solution isn't software; it's hardware.
For data that doesn't change frequently but is accessed heavily, caching at the application layer can offload the database entirely.
After implementing fixes, verify their effectiveness:
You've successfully set up MySQL slow query logging, learned to analyze its output, and explored a range of powerful optimization techniques. Database performance tuning is an ongoing process—your application and data will evolve, and so too should your optimization efforts.
Keep these practices in mind for continuous improvement:
Need Expert Database Performance Tuning or Optimization?
Contact Us