MySQL Slow Query Log
Queries that exceed the long_query_time threshold for performance analysis
Quick Facts
Default Path (Linux)
/var/log/mysql/mysql-slow.logDefault Format
text
JSON Native
No
Rotation
logrotate
Log Example
Default format: Default Text Format
Example Log Entrylog
# Time: 2025-12-20T14:32:18.123456Z
# User@Host: app_user[app_user] @ 192.168.1.100 [] Id: 12345
# Query_time: 15.123456 Lock_time: 0.000123 Rows_sent: 1000 Rows_examined: 1000000
SET timestamp=1734702738;
SELECT * FROM large_table WHERE status = 'active';Structure:
Multi-line format with metadata and queryPaths by Platform
Debian / Ubuntu
/var/log/mysql/mysql-slow.logRHEL / CentOS
/var/log/mysqld-slow.logAvailable Formats
Default Text Format
Default
Example:
# Time: 2025-12-20T14:32:18.123456Z
# User@Host: app_user[app_user] @ 192.168.1.100 [] Id: 12345
# Query_time: 15.123456 Lock_time: 0.000123 Rows_sent: 1000 Rows_examined: 1000000
SET timestamp=1734702738;
SELECT * FROM large_table WHERE status = 'active';Structure:
Multi-line format with metadata and queryFields Reference
| Field | Type | Description | Example |
|---|---|---|---|
Time | datetime | When the query completed | 2025-12-20T14:32:18.123456Z |
User | string | MySQL user who ran the query | app_user |
Host | string | Client host | 192.168.1.100 |
Query_time | float (seconds) | Total query execution time | 15.123456 |
Lock_time | float (seconds) | Time waiting for locks | 0.000123 |
Rows_sent | integer | Rows returned to client | 1000 |
Rows_examined | integer | Rows examined by the query | 1000000 |
query | string | The SQL query | SELECT * FROM large_table WHERE status = 'active' |
Parsing Patterns
Collector Configurations
pt query digestyaml
1# Analyze slow query log with Percona Toolkit2pt-query-digest /var/log/mysql/mysql-slow.log34# Top 10 queries by time5pt-query-digest --limit=10 /var/log/mysql/mysql-slow.log67# Output as JSON8pt-query-digest --output json /var/log/mysql/mysql-slow.logConfiguration
Enable Logging
Enable slow query logging
/etc/mysql/mysql.conf.d/mysqld.cnf
[mysqld]
slow_query_log = 1
slow_query_log_file = /var/log/mysql/mysql-slow.log
long_query_time = 1 # Log queries > 1 second
log_queries_not_using_indexes = 1 # Also log queries without indexesLog Rotation
Tool: logrotate | Config: /etc/logrotate.d/mysql
/etc/logrotate.d/mysql
/var/log/mysql/mysql-slow.log {
daily
rotate 7
missingok
create 640 mysql adm
compress
sharedscripts
postrotate
mysqladmin --defaults-file=/etc/mysql/debian.cnf flush-logs slow
endscript
}Use Cases
Slow query identification
Find queries needing optimization
Query_time
query
Index analysis
Queries examining many rows
Rows_examined
Rows_sent
query
Rows_examined / Rows_sent > 1000Lock contention
Queries waiting on locks
Lock_time
Troubleshooting
Tested On
v8.4.0 on Ubuntu 24.04
mysql_dba - 2025-12-10
Last updated: 2025-12-10 by mysql_dba
3 contributors156 upvotes
Validated
Community Discussions
Help improve this documentation
Found an error or want to add more examples? Contributions are welcome!