MYSQL默認的table_open_cache為64,這個數值是偏小的,如果max_connections較大,則容易引起性能問題。
表現:數據庫查詢效率慢,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就沒問題了(如果還不夠,可以繼續加大,但不能設置大得離譜,可能會引發其他問題)。即時生效的設置:
mysql> set global table_open_cache=1024;
Query OK, 0 rows affected (0.00 sec)
設置后可以觀察一下,如果 opening table不再怎么出現,說明此修改是有效的,將其添加到mysql的配置文件,這樣數據庫重啟后仍可保留此設置。