Database Slow Query Log Analysis and Optimization Practice
Problem Description
The slow query log is a database log file that records SQL statements whose execution time exceeds a specified threshold, serving as a core tool for performance optimization. Interviews often examine how to configure the slow query log, analyze its content, and the complete process of SQL tuning based on the log.
I. Slow Query Log Configuration and Activation
-
Core Parameter Explanation
slow_query_log: Set to ON to enable the slow query log (default OFF)long_query_time: Defines the slow query threshold (unit: seconds), e.g., 10.0 means queries exceeding 10 seconds are recordedslow_query_log_file: Specifies the storage path for the log filelog_queries_not_using_indexes: Whether to log queries not using indexes (note: may generate a large volume of logs)
-
Configuration Example (MySQL)
-- Dynamic setting (invalid after restart) SET GLOBAL slow_query_log = 1; SET GLOBAL long_query_time = 2; SET GLOBAL slow_query_log_file = '/var/log/mysql/slow.log'; -- Write to configuration file my.cnf for permanent effect [mysqld] slow_query_log = 1 long_query_time = 2 slow_query_log_file = /var/log/mysql/slow.log
II. Slow Query Log Content Parsing
A single slow query record contains the following key information (using MySQL as an example):
# Time: 2023-10-01T10:00:00.123456Z
# User@Host: user[root] @ localhost []
# Query_time: 5.002031 Lock_time: 0.001000 Rows_sent: 1 Rows_examined: 1000000
SET timestamp=1696154400;
SELECT * FROM orders WHERE create_date < '2023-01-01';
- Query_time: Actual SQL execution time (core metric)
- Lock_time: Time waiting for table locks
- Rows_sent: Number of rows returned to the client
- Rows_examined: Number of rows examined at the server layer (higher scans usually indicate worse performance)
III. Slow Query Log Analysis Tools
-
mysqldumpslow (MySQL Official Tool)
# Count the top 3 most time-consuming slow queries mysqldumpslow -s t -t 3 /var/log/mysql/slow.log # Count queries with the longest lock time mysqldumpslow -s l -t 3 /var/log/mysql/slow.log-sSorting method (t: by time, l: by lock time, r: by returned rows)-tLimit output count
-
pt-query-digest (Percona Toolkit)
# Generate detailed analysis report pt-query-digest /var/log/mysql/slow.log > slow_report.txtOutput includes:
- Total query count and execution time distribution
- Response time percentage for each SQL
- Most frequently executed query statements
IV. Practical Optimization Steps Based on Logs
-
Identify High-Cost Queries
- Prioritize optimizing queries with high execution frequency and long average duration (formula: frequency × single duration)
- Focus on queries where Rows_examined is much greater than Rows_sent (may indicate full table scans)
-
Analyze Execution Plan of Specific SQL
EXPLAIN SELECT * FROM orders WHERE create_date < '2023-01-01';- Check if the type column is ALL (full table scan)
- Observe if the key column uses an index
-
Common Optimization Methods
- Add Index: Create an index for the WHERE condition field create_date
ALTER TABLE orders ADD INDEX idx_create_date(create_date); - Rewrite Query: Avoid SELECT *, only return necessary fields
- Pagination Optimization: Change LIMIT 10000,10 to WHERE id > last maximum ID
- Add Index: Create an index for the WHERE condition field create_date
-
Verify Optimization Effectiveness
- Compare the reduction ratio of Query_time and Rows_examined before and after optimization
- Use EXPLAIN to confirm index effectiveness
V. Advanced Practical Techniques
- Dynamic Log Sampling Activation
-- Enable logging of 1% of slow queries during business peak hours SET GLOBAL log_slow_rate_limit = 1; SET GLOBAL log_slow_rate_type = 'query'; - Monitor Log File Size
- Regularly archive historical logs (e.g., daily rotation)
- Use log_rotation_size parameter to limit single file size
Through systematic use of slow query logs, database performance bottlenecks can be precisely located, achieving an optimization cycle from 'reactive firefighting' to 'proactive prevention'.