MySQL参数优化---Table Cache
如果Opened_tables状态变量很大或者在增长,可能是因为表缓存不够大,那么可以人为增加table_cache系统变量(或者是MySQL 5.1 中
The Table Cache
--------------------------------------------------------------------------------
表缓存的对象:表
每个在缓存中的对象 包含相关表 .frm文件的解析结果,加上一些其它的数据。
准确地说,在对象里的其它数据的内容依赖于表的存储引擎。例如:
MyISAM,是表的数据和索引的文件描述符。
Merge, 可能是多个文件描述符,因为Merge表可以有很多的底层表。
--------------------------------------------------------------------------------
表缓存特性:资源重用
例如:
当一个查询请求访问一张MyISAM表, MySQL 也许可以从缓存的对象中获取到文件描述符。尽管这样做可以避免打开一个文件描述符的开销,但这个开销其实并不大。打开和关闭文件描述符在本地存储是很快的,服务器可以轻松地完成每秒100万次的操作。对MyISAM表来说,表缓存的真正好处是:可以让服务器避免修改MyISAM文件头来标记表“正在使用中”。
--------------------------------------------------------------------------------
表缓存-->InnoDB
表缓存的设计是服务器和存储引擎之间分离不彻底的产物,属于历史问题。表缓存对InnoDB重要性就小多了,因为InnoDB不依赖它来做那么多的事(例如持有文件描述符,InnoDB有自己的表缓存版本)。尽管如此,InnoDB也能从缓存解析的.frm文件中获益。
--------------------------------------------------------------------------------
Table Cache 参数的演变
在MySQL 5.1版本中及之后的版本,表缓存分离成两部分:
一个是打开表的缓存 ---> table_open_cache
一个是表定义的缓存 ---> table_definition_cache
其结果是,,表定义(解析.frm文件的结果)从其它资源中分离出来了,例如表描述符。打开的表依然是每个线程,每个表用的,但是表定义是全局的,可以被所有连接有效地共享。
通常可以把table_definition_cache 设置得足够高,以缓存所有的表定义。除非有上万张表,否则这可能是最简单的方法。
--------------------------------------------------------------------------------
参数的简介:
Variable Name Variable Scope Dynamic Variable
table_open_cache Global Yes
table_definition_cache Global Yes
其中table_definition_cache默认值为400,取值范围400-524288
--------------------------------------------------------------------------------
判断参数是否需调整
如果Opened_tables状态变量很大或者在增长,可能是因为表缓存不够大,那么可以人为增加table_cache系统变量(或者是MySQL 5.1 中的table_open_cache)。然而,当创建和删除临时表时,要注意这个计数器的增长,如果经常需要创建和删除临时表,那么该计数器就会不停地增长。
mysql> show status like 'Opened_files';
+---------------+------------+
| Variable_name | Value |
+---------------+------------+
| Opened_files | 1170770489 |
+---------------+------------+
--------------------------------------------------------------------------------
Table Cache 设置过大的缺点
把表缓存设置过大的缺点:当服务器有很多的MyISAM表时,可能会导致关机时间较长,因为关机前索引块必须完成刷新,表都必须标记为不再打开。同样的原因,也可能使FLUSH TABLES WITH READ LOCK操作花费很长一段时间。
--------------------------------------------------------------------------------
Table Cache 总结:
表缓存实际上用的内存并不多,相反却可以有效节约资源。虽然打开一个新的表,相对于其他MySQL操作来说代价不算高,但它们的开销是累加的。所以缓存表有时可以提升效率。
--------------------------------------------------------------------------------
个人感觉:
对于,线上数据库大多是MyISAM存储引擎,应相应将table_open_cache的值调大些。
至于table_definition_cache, 看数据库的table容量。
特此说明:个人感觉,纯属自己的观点,自己的理解,仅供参考。

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]
