報錯 ERROR 1615 (HY000): Prepared statement needs to be re-prepared 通常發生在大量表結構變更 (DDL) 導致預處理語句緩存失效。本文分析根因並給出生產級調優方案。
ALTER TABLE、TRUNCATE、ANALYZE、統計信息更新) 會使版本號增長。table_definition_cache / table_open_cache 過小,導致表定義被驅逐,無法重新準備,拋出 1615。-- 臨時生效 (重啟失效)
SET GLOBAL table_open_cache = 16384;
SET GLOBAL table_definition_cache = 16384;
-- 永久生效 (my.cnf)
[mysqld]
table_open_cache = 16384
table_definition_cache = 16384
| 參數 | 默認值 | 建議值 | 說明 |
|---|---|---|---|
table_open_cache | 4000 (5.7) / 2000 (5.6) | 實例表數 × 2~3 | 打開表緩存,避免頻繁打開/關閉 .frm/.ibd |
table_definition_cache | 1400 (5.7) / 400 (5.6) | 實例表數 × 1~2 | 表定義 (.frm) 緩存,直接影響 Prepared Statement 重準備 |
SHOW GLOBAL STATUS LIKE 'Opened_tables'; 持續增長說明緩存不足。SHOW GLOBAL STATUS LIKE 'Prepared_stmt_count'; 觀察預處理語句數量。pt-online-schema-change 或 gh-ost 在線變更。cachePrepStmts=true、prepStmtCacheSize 合理複用預處理語句。table_open_cache_overflows、table_definition_cache_overflows 指標。