
“Navigating through the complex landscape of thread contention in MySQL 8 becomes a strategic task, and with the Performance Schema at your disposal, you are equipped to unearth the intricacies of thread behavior, ensuring a seamless, optimized, and high-performing database environment.”
Tuning the Performance Schema in MySQL for specific use cases, like troubleshooting InnoDB thread contention, involves enabling specific instruments and consumers while disabling others to save on overhead.
Regarding InnoDB thread contention, you’re likely interested in mutexes, read-write locks, and conditions. The Performance Schema can be fine-tuned to capture these events.
Diagnosing Thread Contention in MySQL 8 using Performance Schema: A Comprehensive Guide
1. Instruments to Enable
Enable the necessary instruments for InnoDB mutexes, read-write locks, and conditions:
UPDATE performance_schema.setup_instruments
SET ENABLED = 'YES', TIMED = 'YES'
WHERE NAME LIKE 'wait/synch/mutex/innodb/%'
OR NAME LIKE 'wait/synch/rwlock/innodb/%'
OR NAME LIKE 'wait/synch/cond/innodb/%';
2. Consumers are storage components of the Performance Schema. For thread contention, you’d be mainly interested in events_waits and related consumers:
UPDATE performance_schema.setup_consumers
SET ENABLED = 'YES'
WHERE NAME IN ('events_waits_current', 'events_waits_history', 'events_waits_history_long', 'events_waits_summary_by_thread_by_event_name');
3. Configuration Changes in my.cnf
In your my.cnf (or my.ini for Windows), you’d want to ensure the Performance Schema is enabled and given ample sizing for its various buffers and tables:
[mysqld]
# Enable Performance Schema
performance_schema=ON
# Size of performance schema buffers
performance_schema_events_waits_history_long_size=10000
performance_schema_events_waits_history_size=1000
# Size of the memory pools for performance schema tables
performance_schema_memory_size=100M
# Enabling the necessary instruments
performance-schema-instrument='wait/synch/mutex/innodb/%=ON'
performance-schema-instrument='wait/synch/rwlock/innodb/%=ON'
performance-schema-instrument='wait/synch/cond/innodb/%=ON'
# Enabling necessary consumers
performance-schema-consumer-events-waits-current=ON
performance-schema-consumer-events-waits-history=ON
performance-schema-consumer-events-waits-history-long=ON
performance-schema-consumer-events-waits-summary-by-thread-by-event-name=ON
After making the changes to your my.cnf, restart your MySQL server.
Recommendation and Caution
- Do remember that enabling the Performance Schema, especially with many instruments, can have an overhead. It’s essential to test these changes in a non-production environment to gauge the overhead and ensure there’s no negative impact on performance.
- It’s usually advisable not to keep all these instruments enabled all the time in a production environment. Instead, enable them when you need to perform specific diagnostics and then turn them off.
- While the Performance Schema’s overhead is typically low, it’s not zero. The more instruments you enable, the more overhead you might incur. Monitor the system’s performance and adjust as necessary.
Thread contention can seriously impact MySQL’s performance. When threads are contending for resources (like locks), it implies they’re waiting and not making progress, which can slow down query response times.
To troubleshoot thread contention using the Performance Schema in MySQL 8, you’ll want to look into specific wait events, especially around mutexes, read-write locks, and conditions. Here’s how you can investigate these wait events:
1. Mutex Contention
A mutex is a synchronization rudimentary that avoids multiple threads from concurrently executing critical sections of code which access shared data.
-- Top mutexes causing contention
SELECT EVENT_NAME, COUNT_STAR, SUM_TIMER_WAIT
FROM performance_schema.events_waits_summary_global_by_event_name
WHERE EVENT_NAME LIKE 'wait/synch/mutex/innodb/%'
ORDER BY SUM_TIMER_WAIT DESC
LIMIT 10;
2. Read-Write Lock Contention
Read-write locks are a type of synchronization rudimentary that allows multiple threads to read shared data concurrently but ensures exclusive access for threads that want to modify (write to) it.
-- Top RW-locks causing contention
SELECT EVENT_NAME, COUNT_STAR, SUM_TIMER_WAIT
FROM performance_schema.events_waits_summary_global_by_event_name
WHERE EVENT_NAME LIKE 'wait/synch/rwlock/innodb/%'
ORDER BY SUM_TIMER_WAIT DESC
LIMIT 10;
3. Thread Conditions Contention
Conditions are used with mutexes and RW-locks to allow threads to wait until a particular condition becomes true.
-- Top conditions causing contention
SELECT EVENT_NAME, COUNT_STAR, SUM_TIMER_WAIT
FROM performance_schema.events_waits_summary_global_by_event_name
WHERE EVENT_NAME LIKE 'wait/synch/cond/innodb/%'
ORDER BY SUM_TIMER_WAIT DESC
LIMIT 10;
4. Threads Waiting the Longest
To see which threads are waiting the most, you can inspect:
-- Threads with the most wait time
SELECT THREAD_ID, EVENT_NAME, SUM_TIMER_WAIT
FROM performance_schema.events_waits_summary_by_thread_by_event_name
WHERE EVENT_NAME LIKE 'wait/synch/%'
ORDER BY SUM_TIMER_WAIT DESC
LIMIT 10;
Recommendation and Caution:
- First and foremost, Always ensure that the Performance Schema is enabled and that the necessary instruments and consumers are turned on before running these queries.
- Additionally, ,Analyzing thread contention requires a holistic approach where you correlate these metrics with other system and database metrics. The above queries give insight into where contention may be happening, but the root cause might be elsewhere.
- Moreover, Be wary of overhead. While the Performance Schema’s overhead is usually low, constantly probing can still impact performance, especially in a high-throughput environment. Use judiciously in production environments.