1. 程式人生 > 其它 >MySQL Config--引數TABLE_OPEN_CACHE設定過小導致Thread Running較高

MySQL Config--引數TABLE_OPEN_CACHE設定過小導致Thread Running較高

問題描述

某伺服器例項Thread Running屏藩報警,高峰期Thread Running數超過200:

通過檢視活躍程序發現大量程序處於" Opening tables "或" closing tables " 狀態,當前例項上存在945個數據庫(schema)和11萬多資料表(table)。

當前資料庫例項配置為:

## 查詢引數配置
SELECT * 
FROM information_schema.GLOBAL_VARIABLES AS T1
WHERE T1.VARIABLE_NAME IN(
  'INNODB_OPEN_FILES',
  'OPEN_FILES_LIMIT',
  'TABLE_OPEN_CACHE_INSTANCES',
  'TABLE_DEFINITION_CACHE',
  'TABLE_OPEN_CACHE'
)
## 查詢結果
+----------------------------+----------------+
| VARIABLE_NAME              | VARIABLE_VALUE |
+----------------------------+----------------+
| INNODB_OPEN_FILES          | 10000          |
| OPEN_FILES_LIMIT           | 65535          |
| TABLE_OPEN_CACHE_INSTANCES | 1              |
| TABLE_DEFINITION_CACHE     | 4096           |
| TABLE_OPEN_CACHE           | 4000           |
+----------------------------+----------------+

當前資料庫例項狀態值為:

## 查詢STATUS
SELECT * 
FROM information_schema.GLOBAL_STATUS AS T1
WHERE T1.VARIABLE_NAME LIKE '%OPEN%';

## 輸出結果
+----------------------------+----------------+
| VARIABLE_NAME              | VARIABLE_VALUE |
+----------------------------+----------------+
| COM_HA_OPEN                | 0              |
| COM_SHOW_OPEN_TABLES       | 0              |
| INNODB_NUM_OPEN_FILES      | 10000          |
| OPEN_FILES                 | 20             |
| OPEN_STREAMS               | 0              |
| OPEN_TABLE_DEFINITIONS     | 4096           |
| OPEN_TABLES                | 4000           |
| OPENED_FILES               | 5117677500     |
| OPENED_TABLE_DEFINITIONS   | 4874024138     |
| OPENED_TABLES              | 6077542840     |
| SLAVE_OPEN_TEMP_TABLES     | 0              |
| TABLE_OPEN_CACHE_HITS      | 179625030889   |
| TABLE_OPEN_CACHE_MISSES    | 6077541902     |
| TABLE_OPEN_CACHE_OVERFLOWS | 6077426207     |
+----------------------------+----------------+

相關引數解釋:

OPEN_TABLES: The number of tables that are open.

OPENED_TABLES: The number of tables that have been opened. If Opened_tables is big, your table_open_cache value is probably too small.

Open_files: The number of files that are open. This count includes regular files opened by the server. It does not include other types of files such as sockets or pipes. Also, the count does not include files that storage engines open using their own internal functions rather than asking the server level to do so.

Opened_files: The number of files that have been opened with my_open() (a mysys library function). Parts of the server that open files without using this function do not increment the count.

參考資料:https://dev.mysql.com/doc/refman/8.0/en/server-status-variables.html#statvar_Opened_files

由於OPENED_FILES和OPENED_TABLES的值均遠大於OPEN_FILES和OPEN_TABLES,因此懷疑TABLE_OPEN_CACHE引數值設定過低導致,將TABLE_OPEN_CACHE從4000調整為30000後,發現Thread Running監控趨於平穩:

在Percona Server 5.7.26版本中,使用SHOW STATUS顯示的結果中部分STATUS的值為0,但使用information_schema.GLOBAL_STATUS 表能獲取到值,懷疑存在BUG。