How to Check Slow Queries in MySQL
Identify slow queries and improve database performance using MySQL logging.
Before you start
Step-by-step instructions
1. Enable slow query log
Enable slow query logging in MySQL configuration.
slow_query_log = 1
slow_query_log_file = /var/log/mysql/slow.log
2. Set slow query threshold
Define what counts as a slow query.
long_query_time = 2
3. Check slow query log
Read the slow query log file.
cat /var/log/mysql/slow.log
4. Analyze slow queries
Use built-in tools to analyze slow queries.
mysqldumpslow /var/log/mysql/slow.log
Common issues
Check your MySQL configuration file for the correct log path.
Increase traffic or lower long_query_time to capture more queries.
Ensure MySQL has write permission to the log directory.
Final check
Monitoring slow queries regularly helps keep your database fast and stable. Optimize queries and indexes for better performance.