table_open_cache(5.1.3 前稱 table_cache)控制資料表快取數量。本文介紹透過 Open_tables、Opened_tables 計算快取命中率與利用率,精準調優開啟表快取。
mysql> SHOW GLOBAL STATUS LIKE 'open%tables%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Open_tables | 919 |
| Opened_tables | 1951 |
+---------------+-------+
mysql> SHOW VARIABLES LIKE 'table_cache';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| table_cache | 2048 |
+---------------+-------+
命中率 = Open_tables / Opened_tables * 100%
案例:919 / 1951 ≈ 47.1% → 偏低,建議 ≥ 85%。
利用率 = Open_tables / table_cache * 100%
案例:919 / 2048 ≈ 44.9% → 快取未滿但命中率低,可能因頻繁開啟/關閉不同表。
| 現象 | 建議動作 |
|---|---|
| 命中率 < 85% | 增加 table_open_cache |
| 利用率 > 95% 且命中率高 | 增加 table_open_cache |
| 利用率低但命中率低 | 檢查應用是否頻繁切換資料庫/表、連線池配置 |
SET GLOBAL table_open_cache = 4096;
永久生效請修改 my.cnf:
[mysqld]
table_open_cache = 4096
table_open_cache 會增加檔案描述符消耗,需配合 open_files_limit 調整。table_definition_cache 獨立快取表結構定義。