Open_tables(當前緩存打開表數)與 Opened_tables(歷史累計打開表數)是評估 table_open_cache 是否合理的核心指標。本文給出指標含義、健康度判斷公式、實際案例分析與配置建議。
| 指標 | 含義 | 增長觸發條件 |
|---|---|---|
| Open_tables | 當前表緩存中打開的表實例數 | 新表被打開並放入緩存 |
| Opened_tables | 實例啟動以來累計打開表的次數 | 緩存未命中、臨時表、表結構變更導致重新打開 |
SHOW GLOBAL STATUS LIKE 'Open%tables'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | Open_tables | 919 | | Opened_tables | 1951 | +---------------+-------+
# 1. 緩存命中率 Hit_Rate = Open_tables / Opened_tables * 100% -- 例:919 / 1951 = 47.1% ⚠️ 過低,緩存嚴重不足 # 2. 緩存利用率 Usage_Rate = Open_tables / table_open_cache * 100% -- 需配合 SHOW VARIABLES LIKE 'table_open_cache'; -- 例:table_open_cache=2048 → 919/2048 = 44.9% 利用率尚可
Open_tables / Opened_tables * 100% >= 85% -- 命中率達標 Open_tables / table_open_cache * 100% <= 95% -- 利用率未滿,有擴容空間
案例:電商訂單庫,高峰期 2000 QPS
# 觀察數據 Open_tables = 1850 Opened_tables = 42000 (1小時增長) table_open_cache = 2048 # 判斷 Hit_Rate = 1850/42000 = 4.4% 極低 Usage_Rate = 1850/2048 = 90.3% 接近滿載 # 根因 table_open_cache 遠小於熱表數量,大量表被頻繁踢出又重新打開。 # 調優 SET GLOBAL table_open_cache = 10000; -- 即時生效 # 同步更新 my.cnf # 觀察 1 小時後 Opened_tables 增長趨緩,Hit_Rate 升至 92%。
Open_tables 峰值 P。table_open_cache = P * 1.3(預留 30% 增長緩衝)。open_files_limit = table_open_cache * 3,系統 ulimit -n 同步跟上。Hit_Rate,保持 > 85%。科學監控 Open_tables/Opened_tables,按「峰值 × 1.3」配置 table_open_cache,是 MySQL 表緩存層穩定運行的黃金法則。