NOTE
1.27 MySQL Tuning
MySQL tuning can begin by finding slow SQL through logs or monitoring, then using EXPLAIN to analyze execution plans and locate bottlenecks.
This is a historical learning note and may contain outdated or incomplete understanding.
1. Overall Approach
- Record slow SQL through slow query logs / monitoring / Druid.
- Analyze with
explain.- Indexes not being used.
- Too many tables in join queries.
- Server parameter values set too low.
- Add indexes.
- MySQL Indexes.md
- Pay attention to scenarios where indexes cannot be used.
- Modify SQL statements.
- Use
joinorexistsdepending on the situation. - Do not use
select *.
- Use
2. Single-Server MySQL Bottleneck
3 million rows, 2,000 concurrent requests.
3. Slow Query Log
https://mariadb.com/kb/en/library/documentation/mariadb-administration/server-monitoring-logs/slow-query-log/slow-query-log-overview/ https://dev.mysql.com/doc/refman/8.0/en/slow-query-log.html
3.1. What It Is
The slow query log records statements whose execution time exceeds a specified threshold.
show variables like '%slow_query_log%';
+---------------------+------------------------+
| Variable_name | Value |
+---------------------+------------------------+
| slow_query_log | OFF |
| slow_query_log_file | <example>-slow.log |
+---------------------+------------------------+
show variables like '%long_query_time%';
+-----------------+-----------+
| Variable_name | Value |
+-----------------+-----------+
| long_query_time | 10.000000 |
+-----------------+-----------+
3.2. How to Enable It
set global slow_query_log=1;
set global long_query_time=3;
Reconnect to the database for the changes to take effect.
3.3. How to View It
- View the log directly.
tail -f /var/lib/mysql/<example>-slow.log -n 200
- Use the
mysqldumpslowcommand.
4. explain
5. Show Profile
5.1. What It Is
show variables like 'profiling'
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| profiling | OFF |
+---------------+-------+
5.2. Enable It
set profiling=on;
5.3. View Results
show profiles;
5.4. Analyze
show profile cpu,block io for query 3;
Drag to pan, scroll to zoom, and click a node to expand or collapse it. Large maps start with deeper levels collapsed; click nodes to expand them.
Discussion
Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub