緩存 **表結構定義 (.frm) 和文件句柄**,避免頻繁打開/關閉表文件開銷。MyISAM 每表 2 個句柄 (.MYD/.MYI),InnoDB 共享表空間 1 個,獨立表空間 2 個。
SHOW GLOBAL STATUS LIKE 'Open%tables'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | Open_tables | 400 -- 當前打開表數 | Opened_tables | 5000 -- 歷史累計打開表數 (含緩存未命中) +---------------+-------+
Open_tables 接近 table_open_cache 且 Opened_tables 快速增長 → **緩存不足,需調大**。Opened_tables / Uptime (每秒打開表數) > 1 → 調大。table_open_cache * 2 (MyISAM) 或 * 1~2 (InnoDB)。open_files_limit (my.cnf [mysqld] open_files_limit=65535) 及 OS ulimit -n。table_definition_cache 分離定義緩存。| 場景 | 建議值 |
|---|---|
| 小型/開發 (1~2GB 內存) | 1024~2048 |
| 中型 (4~8GB) | 4096~8192 |
| 大型/高併發 (16GB+) | 16384~65535 |
SHOW GLOBAL STATUS LIKE 'Key_read%'; +-------------------+-----------+ | Variable_name | Value | +-------------------+-----------+ | Key_read_requests | 10000000 -- 索引讀請求 | Key_reads | 500 -- 磁碟讀 +-------------------+-----------+
SHOW GLOBAL STATUS LIKE 'Key_blocks_u%'; +-------------------+-------+ | Variable_name | Value | +-------------------+-------+ | Key_blocks_unused | 1000 -- 剩餘塊 | Key_blocks_used | 8000 -- 峰值使用 +-------------------+-------+
Key_blocks_used / (Key_blocks_used + Key_blocks_unused) ≈ **80~90% 理想**。Open_tables, Opened_tables, Key_read_requests, Key_reads, Key_blocks_used, Key_blocks_unused。innodb_buffer_pool_size 佔 60~80% 內存,**key_buffer_size 僅 16~32M** (滿足 mysql 系統表/內部臨時表)。key_buffer_size 佔 20~30% 內存,table_open_cache 配合 open_files_limit 調大。Opened_tables 突增、Key_reads 突增、Table_open_cache_overflows (5.6+ 新增) > 0。
table_cache 重命名為 table_open_cache,舊名兼容但建議用新名。table_open_cache 過大導致 FLUSH TABLES / 遷移表耗時極長。REPAIR/OPTIMIZE 臨時佔用 key buffer,預留 20% 餘量。