更多学习资料戳!!!
在 mysqld 中对 table_cache 参数的定义如下:
[zzx@localhost ~]$ mysqld --verbose --help|grep table_cache= --table_cache=# The number of open tables for all threads.
复制代码
这个参数表示数据库用户打开表的缓存数量。每个连接进来,都会至少打开一个表缓存。因此,table_cache 与 max_connections 有关,例如,对于 200 个并行运行的连接,应该让表的缓存至少有 200xN,这里 N 是可以执行的查询的一个联接中表的最大数量。此外,还需要为临时表和文件保留一些额外的文件描述符。
可以通过检查 mysqld 的状态变量 open_tables 和 opend_tables 确定这个参数是否过小,这两个参数的区别是前者表示当前打开的表缓存数,如果执行 FLUSH TABLES 操作,则此系统会关闭一些当前没用使用的表缓存而使得此状态值减小;后者表示曾经打开的表缓存数,会一直进行累加,如果执行 FLUSH TABLES 操作,值不会减少。下面的例子验证了这个过程。
(1)首先清空表缓存,记录两个状态的值:
mysql> flush tables; Query OK, 0 rows affected (0.00 sec) mysql> show global status like 'open_tables';; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | Open_tables | 0 | +---------------+-------+1 row in set (0.00 sec) ERROR: No query specified mysql> show global status like 'opened_tables';; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | Opened_tables | 35 | +---------------+-------+ 1 row in set (0.00 sec)
复制代码
(2)然后,执行一个 SQL,对表 t 进行查询:
mysql> select count(1) from t; +----------+ | count(1) | +----------+ | 5 | +----------+ 1 row in set (0.00 sec)
复制代码
(3)接着再查看这两个参数的值:
mysql> show global status like 'open_tables';; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | Open_tables | 1 | +---------------+-------+ 1 row in set (0.00 sec) ERROR: No query specified mysql> show global status like 'opened_tables';; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | Opened_tables | 36 | +---------------+-------+ 1 row in set (0.00 sec)
复制代码
(4)可以发现,两个参数值都因为对表 t 的查询而状态加 1。这时,再次执行刚才对表 t 的查询:
mysql> select count(1) from t; +----------+ | count(1) |+----------+ | 5 | +----------+ 1 row in set (0.00 sec) mysql> show global status like 'open_tables';; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | Open_tables | 1 | +---------------+-------+ 1 row in set (0.00 sec) ERROR: No query specified mysql> show global status like 'opened_tables';; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | Opened_tables | 36 | +---------------+-------+ 1 row in set (0.00 sec)
复制代码
(5)此时这两个参数的值并没有变化,因为表 t 的描述符已经在此连接中打开过一次,因此保存了表缓存中。因此,状态值“open_tables”对于设置 table_cache 值有着更有价值的参考。
评论