MySQL table_cache优化(一)_MySQL
bitsCN.com
table_cache 参数设置表高速缓存的数目。每个连接进来,都会至少打开一个表缓存。因此, table_cache 的大小应与 max_connections 的设置有关。例如,对于 200 个并行运行的连接,应该让表的缓存至少有 200 × N ,这里 N 是应用可以执行的查询的一个联接中表的最大数量。此外,还需要为临时表和文件保留一些额外的文件描述符。
当 Mysql 访问一个表时,如果该表在缓存中已经被打开,则可以直接访问缓存;如果还没有被缓存,但是在 Mysql 表缓冲区中还有空间,那么这个表就被打开并放入表缓冲区;如果表缓存满了,则会按照一定的规则将当前未用的表释放,或者临时扩大表缓存来存放,使用表缓存的好处是可以更快速地访问表中的内容。
执行 flush tables 会清空缓存的内容。一般来说,可以通过查看数据库运行峰值时间的状态值 Open_tables 和 Opened_tables ,判断是否需要增加 table_cache 的值。其中 open_tables 是当前打开的表的数量, Opened_tables 则是已经打开的表的数量。下面我们的例子显示了这两个状态值的变化情况:
首先,清空表缓存:
mysql> flush tables;
Query OK, 0 rows affected (0.00 sec)
察看当前的表缓存情况:
mysql> show global status like open%_tables;
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Open_tables | 0 |
| Opened_tables | 543 |
+---------------+-------+
2 rows in set (0.00 sec)
在当前连接访问一个表:
mysql> select count(*) from t1;
+----------+
| count(*) |
+----------+
| 4 |
+----------+
1 row in set (0.03 sec)
mysql> show global status like open%_tables;
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Open_tables | 1 |
| Opened_tables | 544 |
+---------------+-------+
2 rows in set (0.00 sec)
Open_tables和opened_tables都增加1。
再访问一个其他表,可以看到两个参数都增加。
mysql> select count(*) from t2;
+----------+
| count(*) |
+----------+
| 1 |
+----------+
1 row in set (0.06 sec)
mysql> show global status like open%_tables;
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Open_tables | 2 |
| Opened_tables | 545 |
+---------------+-------+
2 rows in set (0.00 sec)
再访问表t1的时候:
mysql> select * from t1;
+------+
| id |
+------+
| 2 |
| 1 |
| 1 |
| 3 |
+------+
4 rows in set (0.02 sec)
mysql> show global status like open%_tables;
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Open_tables | 2 |
| Opened_tables | 545 |
+---------------+-------+
2 rows in set (0.00 sec)
两个参数都没有变化,因为该表已经在表缓存中打开了,没有重复打开。
如果发现 open_tables 接近 table_cache 的时候,并且 Opened_tables 这个值在逐步增加,那就说明可能 table_cache 设置的偏小,经常需要将缓存的表清出,将新的表放入缓存,这时可以考虑增加这个参数的大小来改善访问的效率 bitsCN.com

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

This article explores optimizing MySQL memory usage in Docker. It discusses monitoring techniques (Docker stats, Performance Schema, external tools) and configuration strategies. These include Docker memory limits, swapping, and cgroups, alongside

This article addresses MySQL's "unable to open shared library" error. The issue stems from MySQL's inability to locate necessary shared libraries (.so/.dll files). Solutions involve verifying library installation via the system's package m

The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

This article compares installing MySQL on Linux directly versus using Podman containers, with/without phpMyAdmin. It details installation steps for each method, emphasizing Podman's advantages in isolation, portability, and reproducibility, but also

This article provides a comprehensive overview of SQLite, a self-contained, serverless relational database. It details SQLite's advantages (simplicity, portability, ease of use) and disadvantages (concurrency limitations, scalability challenges). C

This guide demonstrates installing and managing multiple MySQL versions on macOS using Homebrew. It emphasizes using Homebrew to isolate installations, preventing conflicts. The article details installation, starting/stopping services, and best pra

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]
