Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> MySQL

Kiểm tra tỷ lệ đọc/ghi (read/write ratio) trong MySQL

Để xem thống kê hoạt động đọc/ghi trong MySQL, sử dụng lệnh SHOW STATUS hoặc SHOW GLOBAL STATUS.

Cách 1: Xem tất cả counters Com_*

SHOW STATUS LIKE 'Com_%';

Trả về 168+ biến đếm các lệnh: Com_select, Com_insert, Com_update, Com_delete, Com_commit,...

Cách 2: Chỉ xem 4 thao tác chính (CRUD)

SHOW GLOBAL STATUS 
WHERE Variable_name IN ('Com_select', 'Com_insert', 'Com_update', 'Com_delete');

Kết quả mẫu:

+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Com_delete    | 5     |
| Com_insert    | 340   |
| Com_select    | 286   |
| Com_update    | 13    |
+---------------+-------+

Tính tỷ lệ Read/Write

  • Reads = Com_select = 286
  • Writes = Com_insert + Com_update + Com_delete = 340 + 13 + 5 = 358
  • Read/Write Ratio = 286 : 358 ≈ 0.8 : 1 (ghi nhiều hơn đọc)

Ứng dụng thực tế

  • Quy hoạch chỉ mục (index): Read-heavy → nhiều index; Write-heavy → ít index
  • Cấu hình replication: Master-write, Slave-read
  • Tuning buffer pool, log file size
  • Chiến lược cache (Redis, Memcached)

Lưu ý

  • Counters reset khi restart MySQL
  • SHOW GLOBAL STATUS xem toàn server, SHOW SESSION STATUS xem session hiện tại
  • Kết hợp với Uptime để tính QPS (queries per second)