您的位置:首页 > 数据库 > SQL

MYSQL实践心得:table_open_cache的设置

2016-10-26 11:24 381 查看
    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的配置文件,这样数据库重启后仍可保留此设置。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: