mysql性能優化之table_open_cache
表現:數據庫查詢效率慢,show processlist 發現比較多的查詢正在opening table。
進一步確認,執行以下語句:
mysql> show global status like 'open%tables%';
+---------------+---------+
| Variable_name | Value |
+---------------+---------+
| Open_tables | 345 |
| Opened_tables | 9734116 |
+---------------+---------+
Opened_tables數值非常大,說明cache太小,導致要頻繁地open table,可以查看下當前的table_open_cache設置:
mysql> show variables like '%table_open_cache%';
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| table_open_cache | 64|
+------------------+-------+
默認是64,一些資料推薦把這個數值設置為(max_connections* 查詢同時用到的表數)。我實踐中發現,一般設置為max_connections就沒問題了(如果還不夠,可以繼續加大,但不能設置大得離譜,可能會引發其他問題)。4G內存的機器,建議設置為2048即時生效的設置:
mysql> set global table_open_cache=2048;
Query OK, 0 rows affected (0.00 sec)
設置後可以觀察一下,如果opening table不再怎麽出現,說明此修改是有效的,將其添加到mysql的配置文件,這樣數據庫重啟後仍可保留此設置。
mysql> show variables like '%table_open_cache%';
+----------------------------+-------+
| Variable_name | Value |
+----------------------------+-------+
| table_open_cache | 400 |
| table_open_cache_instances | 1 |
+----------------------------+-------+
2 rows in set (0.00 sec)
比較適合的值:
Open_tables / Opened_tables >= 0.85
Open_tables / table_open_cache <= 0.95
mysql性能優化之table_open_cache