Open_tables Opened_tables table_open_cache

本文介绍如何通过调整MySQL的table_open_cache参数来提高数据库连接速度。当发现Opened_tables数值较大且持续增长时,可能是因为table_open_cache设置过小,导致频繁打开表文件,从而影响性能。通过增大table_open_cache值可以有效解决此问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天开发说应用链接数据库比较慢,定位后发现是table_open_cache太小了引起的问题。

如果你发现Opened_tables比较大并且一直再增大,很可能是table_open_cache设置太小了,导致每次链接数据库都要去打开表,这也会造成链接数据库比较慢。

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.

table_open_cache :The number of open tables for all threads. Increasing this value increases the number of file descriptors 
that mysqld requires. You can check whether you need to increase the table cache by checking the Opened_tables status variable. 
If the value of Opened_tables is large and you do not use FLUSH TABLES often (which just forces all tables to be closed and 
reopened), then you should increase the value of the table_open_cache variable. For more information about the table cache,
测试:

mysql> show variables like 'table_open_cache';
+------------------+-------+
| Variable_name    | Value |
+------------------+-------+
| table_open_cache | 4     |
+------------------+-------+
1 row in set (0.00 sec)

mysql>  show global status like 'open%tables%'; 
+---------------+----------+
| Variable_name | Value    |
+---------------+----------+
| Open_tables   | 4        |
| Opened_tables | 11412131 |
+---------------+----------+
2 rows in set (0.00 sec)
mysql> exit



[root@slave159 bin]# mysql -uroot -h192.168.60.204 -p1234 pva1
-- 说明:3s左右才登陆
mysql>  show global status like 'open%tables%'; 
+---------------+----------+
| Variable_name | Value    |
+---------------+----------+
| Open_tables   | 4        |
| Opened_tables | 11413673 |
+---------------+----------+
2 rows in set (0.01 sec)

mysql> select 11413673-11412131;
+-------------------+
| 11413673-11412131 |
+-------------------+
|              1542 |
+-------------------+
1 row in set (0.00 sec)


mysql> set global table_open_cache=2000;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye

[root@slave159 bin]# mysql -uroot -h192.168.60.204 -p1234 pva1
-- 说明:3s左右才登陆
mysql> show global status like 'open%tables%'; 
+---------------+----------+
| Variable_name | Value    |
+---------------+----------+
| Open_tables   | 574      |
| Opened_tables | 11433986 |
+---------------+----------+
mysql> exit
Bye

[root@slave159 bin]# mysql -uroot -h192.168.60.204 -p1234 pva1
-- 说明:1s内就登陆了
mysql> show global status like 'open%tables%'; 
+---------------+----------+
| Variable_name | Value    |
+---------------+----------+
| Open_tables   | 574      |
| Opened_tables | 11433986 |
+---------------+----------+


<think>我们正在讨论如何正确配置MySQL的table_open_cache参数。根据引用内容,table_open_cache是MySQL中一个重要的参数,它决定了可以同时打开的表的数量。如果设置过小,可能会导致频繁打开和关闭表,影响性能;如果设置过大,可能会占用过多资源,甚至导致文件描述符不足。根据引用[1]:默认的table_open_cache为64,这个数值通常偏小,尤其在高连接数(max_connections较大)的情况下容易引起性能问题。可以通过观察状态变量`Opened_tables`(表示打开表的次数)和`Open_tables`(表示当前打开表的数量)来判断是否需要调整。如果`Opened_tables`的值很大且持续增长,说明需要增大table_open_cache。引用[1]还提到,一般建议将table_open_cache设置为max_connections的值(或者乘以每个查询中平均涉及的表数)。例如,如果max_connections为100,每个查询平均涉及2个表,那么可以设置为200。但实践中,设置为max_connections的值通常就足够了。引用[5]提到,设置过大可能会遇到文件描述符不足的问题(错误23或24)。这时需要调整open_files_limit参数(增加mysqld可打开的文件数)或者减小table_open_cache或max_connections。另外,引用[4]提到,MySQL将全局的tablecache划分为多个Table_cacheinstance,由参数table_open_cache_instances控制。这个参数在MySQL5.6.6及更高版本中引入,用于减少锁竞争。每个instance负责一部分tablecache,用户线程根据thread_id选择instance。通常,建议将table_open_cache_instances设置为1到16之间,但具体设置需要根据实际情况调整。配置步骤:1.查看当前设置:```sqlSHOWVARIABLESLIKE'table_open_cache';SHOWVARIABLESLIKE'max_connections';SHOWVARIABLESLIKE'table_open_cache_instances';```2.查看状态以判断当前设置是否合理:```sqlSHOWGLOBALSTATUSLIKE'Open%tables';```如果`Open_tables`接近table_open_cache,并且`Opened_tables`持续增长,说明需要增大table_open_cache。3.设置新的值(例如设置为1024):```sqlSETGLOBALtable_open_cache=1024;```注意:这个设置是临时的,重启后会失效。要永久生效,需要在MySQL配置文件(如my.cnf或my.ini)中添加或修改:```[mysqld]table_open_cache=1024```4.同时,如果遇到文件描述符不足的问题,需要调整open_files_limit。在配置文件中设置:```[mysqld]open_files_limit=65535```注意:open_files_limit的值应该大于table_open_cache的值(因为MySQL还需要打开其他文件)。5.关于table_open_cache_instances,通常默认值(例如8)在大多数情况下是合适的。如果需要调整,可以在配置文件中设置:```table_open_cache_instances=8```6.修改配置文件后,需要重启MySQL服务。总结:配置table_open_cache的关键是观察`Opened_tables`的状态,将其调整到一个合理的值,避免频繁打开表。同时注意与max_connections和open_files_limit的协调。注意:每个系统的情况不同,调整后需要观察性能变化。根据上述内容,生成相关问题:1.如何监控table_open_cache是否设置合理?2.调整table_open_cache后,如何验证效果?3.除了table_open_cache,还有哪些与表缓存相关的参数?4.文件描述符不足时,除了调整open_files_limit,还可以如何处理?</think>### 如何正确配置MySQL的table_open_cache参数 配置MySQL的`table_open_cache`参数需综合考虑系统资源、连接数和表使用情况,以下是具体步骤: #### 1. **诊断当前状态** ```sql -- 查看当前缓存设置 SHOW VARIABLES LIKE 'table_open_cache'; -- 检查表打开状态(关键指标) SHOW GLOBAL STATUS LIKE 'Open%tables'; ``` - **Open_tables**:当前打开的表数量(理想值应接近`table_open_cache`)[^1] - **Opened_tables**:累计打开次数(值过高说明缓存不足)[^1] 示例诊断: ``` +---------------+-------+ | Variable_name | Value | +---------------+-------+ | Open_tables | 345 | -- 建议接近table_open_cache值 | Opened_tables | 9,734,116 | -- 过高说明频繁重新打开表 +---------------+-------+ ``` #### 2. **计算合理值** - **基础公式**: ```math \text{推荐值} = \text{max\_connections} \times \text{平均每查询涉及表数} ``` 实践中可简化为: ```math \text{推荐值} \approx \text{max\_connections} \quad [^1] ``` - **上限约束**: 需满足: ```math \text{table\_open\_cache} < \text{open\_files\_limit} - 100 \quad [^5] ``` #### 3. **动态调整** ```sql -- 临时生效(无需重启) SET GLOBAL table_open_cache = 1024; ``` 修改后立即监控`Opened_tables`增长速率是否下降[^1]。 #### 4. **永久生效配置** 在`my.cnf`中添加: ```ini [mysqld] table_open_cache = 1024 # 建议值为max_connections的1-1.5倍 table_open_cache_instances = 8 # 减少锁竞争[^4] open_files_limit = 65535 # 必须大于table_open_cache[^5] ``` #### 5. **优化验证** - 观察指标: - `Opened_tables`增速显著降低 - `SHOW PROCESSLIST`中`Opening tables`状态减少[^1] - 错误处理: 若出现`Can't open file (errno: 24)`,需增加`open_files_limit`[^5] #### 6. **注意事项** - 避免极端值:过大会消耗内存,过小导致性能瓶颈[^3] - 多实例配置:高并发时`table_open_cache_instances`应设为CPU核心数[^4] - 表统计:复杂查询涉及多表时需适当增大缓存 > **配置示例**: > 若`max_connections=500`且无文件限制问题: > ```math > \text{table\_open\_cache} = 500 \sim 750 > ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值