search
HomeDatabaseMysql TutorialMySQL参数优化---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容量。

 

特此说明:个人感觉,纯属自己的观点,自己的理解,仅供参考。

linux

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How do you alter a table in MySQL using the ALTER TABLE statement?How do you alter a table in MySQL using the ALTER TABLE statement?Mar 19, 2025 pm 03:51 PM

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

How do I configure SSL/TLS encryption for MySQL connections?How do I configure SSL/TLS encryption for MySQL connections?Mar 18, 2025 pm 12:01 PM

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]

How do you handle large datasets in MySQL?How do you handle large datasets in MySQL?Mar 21, 2025 pm 12:15 PM

Article discusses strategies for handling large datasets in MySQL, including partitioning, sharding, indexing, and query optimization.

What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)?What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)?Mar 21, 2025 pm 06:28 PM

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

How do you drop a table in MySQL using the DROP TABLE statement?How do you drop a table in MySQL using the DROP TABLE statement?Mar 19, 2025 pm 03:52 PM

The article discusses dropping tables in MySQL using the DROP TABLE statement, emphasizing precautions and risks. It highlights that the action is irreversible without backups, detailing recovery methods and potential production environment hazards.

How do you create indexes on JSON columns?How do you create indexes on JSON columns?Mar 21, 2025 pm 12:13 PM

The article discusses creating indexes on JSON columns in various databases like PostgreSQL, MySQL, and MongoDB to enhance query performance. It explains the syntax and benefits of indexing specific JSON paths, and lists supported database systems.

How do you represent relationships using foreign keys?How do you represent relationships using foreign keys?Mar 19, 2025 pm 03:48 PM

Article discusses using foreign keys to represent relationships in databases, focusing on best practices, data integrity, and common pitfalls to avoid.

How do I secure MySQL against common vulnerabilities (SQL injection, brute-force attacks)?How do I secure MySQL against common vulnerabilities (SQL injection, brute-force attacks)?Mar 18, 2025 pm 12:00 PM

Article discusses securing MySQL against SQL injection and brute-force attacks using prepared statements, input validation, and strong password policies.(159 characters)

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft