Để xem trạng thái khóa (locks), transaction, và hiệu năng InnoDB chi tiết, bạn dùng lệnh SHOW ENGINE INNODB STATUS với modifier \G để hiển thị dọc dễ đọc.
Cú pháp
SHOW ENGINE INNODB STATUS\G
Các section quan trọng trong output
1. SEMAPHORES (Semaphore waits)
---------- SEMAPHORES ----------
OS WAIT ARRAY INFO: reservation count 244, signal count 241
RW-shared spins 23, rounds 37, OS waits 14
RW-excl spins 16, rounds 380, OS waits 9
Spin rounds per wait: 1.61 RW-shared, 23.75 RW-excl, 4.00 RW-sx
- OS waits cao → contention (cạnh tranh khóa) mạnh
- RW-excl (exclusive lock) waits cao → nhiều WRITE conflict
2. TRANSACTIONS (Transaction activity)
------------ TRANSACTIONS ------------
Trx id counter 60219
History list length 32
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 284507225290560, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
- Trx id counter: ID transaction tiếp theo
- History list length: Độ dài undo log (liên quan purge)
- Mỗi transaction hiển thị: state, lock structs, row locks, query đang chạy
3. FILE I/O
-------- FILE I/O --------
I/O thread 0 state: wait Windows aio (insert buffer thread)
...
Pending normal aio reads: [0, 0, 0, 0], aio writes: [0, 0, 0, 0]
2134 OS file reads, 2843 OS file writes, 1186 OS fsyncs
4. BUFFER POOL AND MEMORY
--------------------- BUFFER POOL AND MEMORY ---------------------
Total large memory allocated 8585216
Buffer pool size 512
Free buffers 253
Database pages 256
Modified db pages 0
Pages read 2111, created 215, written 1437
- Free buffers thấp → buffer pool quá nhỏ
- Modified db pages > 0 → dirty pages chưa flush
5. ROW OPERATIONS
------------- ROW OPERATIONS --------------
Number of rows inserted 247, updated 512, deleted 19, read 6593
0.00 inserts/s, 0.00 updates/s, 0.00 deletes/s, 0.00 reads/s
Cách phân tích khóa (Lock Analysis)
Xem transaction đang chờ khóa
Trong section TRANSACTIONS, tìm các transaction có:
lock struct(s) > 0row lock(s) > 0- Trạng thái
LOCK WAIT
Xem chi tiết khóa của một transaction
-- Lấy trx_id từ SHOW ENGINE INNODB STATUS
SELECT * FROM information_schema.INNODB_TRX WHERE trx_id = '...';
-- Xem lock chi tiết
SELECT * FROM information_schema.INNODB_LOCKS WHERE lock_trx_id = '...';
-- Xem wait graph
SELECT * FROM information_schema.INNODB_LOCK_WAITS;
Các lệnh bổ sung hữu ích
| Lệnh | Mục đích |
|---|---|
SHOW PROCESSLIST\G | Xem các kết nối đang chạy, trạng thái, query |
SHOW ENGINE INNODB MUTEX | Xem mutex contention |
SELECT * FROM performance_schema.events_waits_summary_global_by_event_name | Thống kê wait events (MySQL 5.7+) |
SELECT * FROM sys.innodb_lock_waits | View dễ đọc lock waits (MySQL 5.7+ sys schema) |
Lưu ý
- Cần quyền
PROCESSđể chạySHOW ENGINE INNODB STATUS - Output rất dài → Luôn dùng
\Gtrong mysql client - Chạy định kỳ hoặc khi gặp vấn đề performance/lock wait timeout
- Kết hợp với
innodb_status_output=ONvàinnodb_status_output_locks=ONđể log tự động