table_definition_cache

从MySQL5.1开始,数据表文件描述符被分开为数据文件及数据表定义两部分,表定义文件缓存可以放在专属的table_definition_cache中

表定义文件缓存相比表文件描述符缓存所消耗的内存更小,其默认值是400

状态值:

Open_table_definitions:表定义文件.frm被缓存的数量

Opened_table_definitions:历史上总共被缓存过的,frm文件数量
如何计算table_open_cache

查找open_tables和opened_tables的当前值

MySQL的>显示全局状态,例如’open%’;


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.

找出表缓存命中率

表缓存命中率= table_open_cache * 100 / Opened_tables


In general it should be more than 50%. So you need to increase value of table_open_cache, if you are under this value, though there are lots of reasons to have a high value of Opened_tables. Like FLUSH TABLES will close all open tables and reopen it which significantly increases Opened_tables value.

计算table_open_cache的调整值并进行设置

Table_open_cache = total_tables * Threads_connected


As all the threads (user) are not generally access all tables. I think you should set 50% of the value calculated. Because too big value of this variable has some other side effects. So the formula becomes

Table_open_cache = total_tables * Threads_connected * .50

除了table_open_cache,您还应该调整open_files_limit系统变量.

一般来说,它是table_open_cache的2倍.
open_files_limit = Table_open_cache * 2


open_files_limit is not a dynamic variable. So you should set it in my.cnf file and restart MySQL.
Make sure that your operating system can cope with the number of open file descriptors required by the table_open_cache setting.

参考. https://techinfobest.com/optimize-mysql-table_open_cache/
在这里:MySQL文档为我们提供了基于table_open_cache大小设置table_definition_cache的公式. (是的,我知道,该链接适用于5.7版本,但在5.5 doc版本中没有任何关于公式的提及),所以你需要先定义table_open_cache的大小,这就是我给出上面链接的原因.
https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_table_definition_cache
接下来,您必须监视COM_STMT_REPREPARE和Open_table_definitions的状态,以查看服务器中发生的情况.如果您的应用程序正在使用预准备语句并获取消息错误代码[1615]预备语句需要重新准备,则表明MySQL正在刷新缓存的定义表.(https://dev.mysql.com/doc/refman/5.5/en/error-messages-server.html#error_er_need_reprepare)
最后:关于为什么MySQL被重新定位的信息
https://dev.mysql.com/doc/refman/5.5/en/statement-repreparation.html