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 如果您的表少于12个,那么Open tables
value of 12 can be somewhat puzzling if you have fewer than 12 tables.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_cache
和max_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_cache
与max_connections
相关。For example, for 200 concurrent running connections, specify a table cache size of at least 例如,对于200个并发运行的连接,请指定至少200 *
, where N
N
is the maximum number of tables per join in any of the queries which you execute. 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 要增加MySQL可用的文件描述符数量,请设置open_files_limit
system variable. 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 cache is full and a thread tries to open a table that is not in the cache.当缓存已满且线程试图打开不在缓存中的表时。
When the cache contains more than 当缓存包含多个table_open_cache
entries and a table in the cache is no longer being used by any threads.table_open_cache
项且缓存中的表不再被任何线程使用时。
When a table-flushing operation occurs. 当发生工作台冲洗操作时。This happens when someone issues a 当有人发出FLUSH TABLES
statement or executes a mysqladmin flush-tables or mysqladmin refresh command.FLUSH TABLES
语句或执行mysqladmin flush-tables
命令或mysqladmin refresh
命令时,就会发生这种情况。
When the table cache fills up, the server uses the following procedure to locate a cache entry to use:当表缓存填满时,服务器使用以下过程查找要使用的缓存项:
Tables not currently in use are released, beginning with the table least recently used.将发布当前未使用的表,从最近使用最少的表开始。
If a new table must be opened, but the cache is full and no tables can be released, the cache is temporarily extended as necessary. 如果必须打开新表,但缓存已满且无法释放任何表,则会根据需要临时扩展缓存。When the cache is in a temporarily extended state and a table goes from a used to unused state, the table is closed and released from the cache.当缓存处于临时扩展状态且表从“已使用”状态变为“未使用”状态时,表将关闭并从缓存中释放。
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
statement, a dedicated table object is allocated for the thread. tbl_name
OPENHANDLER
语句打开表,则会为线程分配一个专用的表对象。tbl_name
OPENThis table object is not shared by other threads and is not closed until the thread calls 此表对象不被其他线程共享,并且在线程调用HANDLER
or the thread terminates. tbl_name
CLOSEHANDLER
或线程终止之前不会关闭。tbl_name
CLOSEWhen 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
值。