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.

Databases1 min readhistorical

This is a historical learning note and may contain outdated or incomplete understanding.

1. Overall Approach

  1. Record slow SQL through slow query logs / monitoring / Druid.
  2. Analyze with explain.
    • Indexes not being used.
    • Too many tables in join queries.
    • Server parameter values set too low.
  3. Add indexes.
  4. Modify SQL statements.
    • Use join or exists depending on the situation.
    • Do not use select *.

Database Optimization.md

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 mysqldumpslow command. UTOOLS1577621298906.png

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;

Discussion

Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub