8.4.3.1 How MySQL Opens and Closes TablesMySQL如何打开和关闭表

When you execute a mysqladmin status command, you should see something like this:当您执行mysqladmin status命令时,您应该看到如下内容:

Uptime: 426 Running threads: 1 Questions: 11082
Reloads: 1 Open tables: 12

The Open tables value of 12 can be somewhat puzzling if you have fewer than 12 tables.如果您的表少于12个,那么Open tables值12可能有点令人费解。

MySQL is multithreaded, so there may be many clients issuing queries for a given table simultaneously. MySQL是多线程的,因此可能有许多客户端同时对给定的表发出查询。To minimize the problem with multiple client sessions having different states on the same table, the table is opened independently by each concurrent session. 为了最大限度地减少多个客户端会话在同一个表上具有不同状态的问题,该表由每个并发会话独立打开。This uses additional memory but normally increases performance. 这会使用额外的内存,但通常会提高性能。With MyISAM tables, one extra file descriptor is required for the data file for each client that has the table open. 对于MyISAM表,每个打开该表的客户机的数据文件都需要一个额外的文件描述符。(By contrast, the index file descriptor is shared between all sessions.)(相反,索引文件描述符在所有会话之间共享。)

The table_open_cache and max_connections system variables affect the maximum number of files the server keeps open. table_open_cachemax_connections系统变量会影响服务器保持打开的最大文件数。If you increase one or both of these values, you may run up against a limit imposed by your operating system on the per-process number of open file descriptors. 如果增加其中一个或两个值,则可能会遇到操作系统对每个进程打开的文件描述符数施加的限制。Many operating systems permit you to increase the open-files limit, although the method varies widely from system to system. 许多操作系统允许您增加打开文件的限制,尽管方法因系统而异。Consult your operating system documentation to determine whether it is possible to increase the limit and how to do so.请参阅您的操作系统文档,以确定是否可以增加限制以及如何增加限制。

table_open_cache is related to max_connections. table_open_cachemax_connections相关。For example, for 200 concurrent running connections, specify a table cache size of at least 200 * N, where N is the maximum number of tables per join in any of the queries which you execute. 例如,对于200个并发运行的连接,请指定至少200 * N的表缓存大小,其中N是您执行的任何查询中每个连接的最大表数。You must also reserve some extra file descriptors for temporary tables and files.您还必须为临时表和文件保留一些额外的文件描述符。

Make sure that your operating system can handle the number of open file descriptors implied by the table_open_cache setting. 请确保您的操作系统能够处理table_open_cache设置所暗示的打开文件描述符的数量。If table_open_cache is set too high, MySQL may run out of file descriptors and exhibit symptoms such as refusing connections or failing to perform queries.如果table_open_cache设置得太高,MySQL可能会耗尽文件描述符,并出现拒绝连接或无法执行查询等症状。

Also take into account that the MyISAM storage engine needs two file descriptors for each unique open table. 还要考虑到MyISAM存储引擎需要为每个唯一的打开表提供两个文件描述符。To increase the number of file descriptors available to MySQL, set the open_files_limit system variable. 要增加MySQL可用的文件描述符数量,请设置open_files_limit系统变量。See Section B.3.2.16, “File Not Found and Similar Errors”.请参阅第B.3.2.16节,“未找到文件和类似错误”

The cache of open tables is kept at a level of table_open_cache entries. 打开表的缓存保持在table_open_cache项的级别。The server autosizes the cache size at startup. 服务器在启动时自动调整缓存大小。To set the size explicitly, set the table_open_cache system variable at startup. 要显式设置大小,请在启动时设置table_open_cache系统变量。MySQL may temporarily open more tables than this to execute queries, as described later in this section.MySQL可能会临时打开更多的表来执行查询,如本节后面所述。

MySQL closes an unused table and removes it from the table cache under the following circumstances:在以下情况下,MySQL关闭未使用的表并将其从表缓存中删除:

When the table cache fills up, the server uses the following procedure to locate a cache entry to use:当表缓存填满时,服务器使用以下过程查找要使用的缓存项:

A MyISAM table is opened for each concurrent access. 每次并发访问都会打开一个MyISAM表。This means the table needs to be opened twice if two threads access the same table or if a thread accesses the table twice in the same query (for example, by joining the table to itself). 这意味着,如果两个线程访问同一个表,或者一个线程在同一个查询中访问该表两次(例如,通过将表连接到自身),则需要打开该表两次。Each concurrent open requires an entry in the table cache. 每个并发打开都需要表缓存中的一个条目。The first open of any MyISAM table takes two file descriptors: one for the data file and one for the index file. 任何MyISAM表的第一次打开都需要两个文件描述符:一个用于数据文件,另一个用于索引文件。Each additional use of the table takes only one file descriptor for the data file. 该表的每一次额外使用仅为数据文件使用一个文件描述符。The index file descriptor is shared among all threads.索引文件描述符在所有线程之间共享。

If you are opening a table with the HANDLER tbl_name OPEN statement, a dedicated table object is allocated for the thread. 如果使用HANDLER tbl_name OPEN语句打开表,则会为线程分配一个专用的表对象。This table object is not shared by other threads and is not closed until the thread calls HANDLER tbl_name CLOSE or the thread terminates. 此表对象不被其他线程共享,并且在线程调用HANDLER tbl_name CLOSE或线程终止之前不会关闭。When this happens, the table is put back in the table cache (if the cache is not full). 发生这种情况时,表将放回表缓存中(如果缓存未满)。See Section 13.2.4, “HANDLER Statement”.请参阅第13.2.4节,“处理程序语句”

To determine whether your table cache is too small, check the Opened_tables status variable, which indicates the number of table-opening operations since the server started:要确定表缓存是否太小,请检查Opened_tables状态变量,该变量指示自服务器启动以来打开表的操作数:

mysql> SHOW GLOBAL STATUS LIKE 'Opened_tables';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Opened_tables | 2741  |
+---------------+-------+

If the value is very large or increases rapidly, even when you have not issued many FLUSH TABLES statements, increase the table_open_cache value at server startup.如果该值非常大或快速增加,即使您没有发出许多FLUSH TABLES语句,也应在服务器启动时增加table_open_cache值。