Managed Service for MySQL® performance analysis and tuning
The following issues usually cause reduced performance of a Managed Service for MySQL® cluster:
MySQL® monitoring and performance diagnostic tools for a Managed Service for MySQL® cluster, along with special MySQL® queries, will help detect these issues.
Getting started
- Select the databases to analyze.
- Enable statistics collection.
- Create a MySQL® user with the PROCESS privilege for these databases. Use this MySQL® user to run all diagnostic queries.
Diagnosing resource shortages
A resource shortage is a likely cause of reduced cluster performance. Cluster monitoring charts (CPU, disk I/O, and network connections) clearly show any resource shortages. If a steadily growing resource usage chart flattens, the resource has hit its limit or run beyond the guaranteed service level.
You can use special queries to find out what causes increased resource usage:
-
To estimate disk I/O usage by different MySQL® threads, run this query:
SELECT t.name AS thread_name, t.processlist_user AS user, t.processlist_info AS query, t.processlist_time AS time, io.bytes AS bytes FROM performance_schema.threads t JOIN ( SELECT thread_id, sum(number_of_bytes) AS bytes FROM performance_schema.events_waits_history_long WHERE object_type='FILE' GROUP BY thread_id) io ON t.thread_id = io.thread_id ORDER BY io.bytes DESCThis query returns a list of MySQL® file threads ranked by the amount of memory they use, from highest to lowest. Threads that serve replication and the InnoDB buffer
for caching tables and indexes usually lead the list. -
To estimate network resource usage by different MySQL® threads, run this query:
SELECT t.name AS thread_name, t.processlist_user AS user, t.processlist_info AS query, t.processlist_time AS time, net.bytes/t.processlist_time AS avg_bytes, net.bytes AS total_bytes FROM performance_schema.threads t JOIN ( SELECT thread_id, Sum(variable_value) bytes FROM performance_schema.status_by_thread WHERE variable_name IN ('Bytes_sent', 'Bytes_received') GROUP BY thread_id ) net ON t.thread_id = net.thread_id WHERE t.processlist_time IS NOT NULL ORDER BY net.bytes DESCThis query returns statistics since the threads were started, so long-lived connections, such as those used for replication, will be closer to the top.
Pay close attention to read and write operations affecting many rows. These can also cause increased network load. For writes, WAL changes will be applied to replicas, which further increases network load.
-
You cannot monitor CPU consumption for individual MySQL® queries, but you can identify queries that run inefficiently (see below).
Diagnosing inefficient query execution
To identify MySQL® queries that run inefficiently, use this query:
SELECT *
FROM sys.statement_analysis
LIMIT 10
It returns the 10 longest-running queries in the entire server history.
Pay attention to queries with high values for ROWS_EXAMINED, ROWS_SORTED, or those with the FULL_SCAN flag.
For more information about the output, see this MySQL® article
Diagnosing locks
Cluster performance may degrade because of locks caused by multiple simultaneous attempts to access the same database resource, e.g., table or row.
To diagnose locking issues, check the query lock wait queues:
-
Table-level lock wait queue:
SELECT * FROM sys.schema_table_lock_waits -
Row-level lock wait queue:
SELECT * FROM sys.innodb_lock_waits
Troubleshooting
You can try to optimize any queries with performance issues you have diagnosed. There are several ways for optimization:
-
Analyze the query plan using the
EXPLAINcommand and use the query optimization techniques from this MySQL® article . -
Optimize InnoDB tables
to reduce disk usage.
If optimizing or removing such queries is not an option, consider upgrading your host class.