mysql性能优化之table_open_cache

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就没问题了(如果还不够,可以继续加大,但不能设置大得离谱,可能会引发其他问题)。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


猜你喜欢

转载自blog.51cto.com/13120271/2130558
今日推荐