8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

正文为中文,英文原文默认隐藏。点击任意中文段落即可展开 / 收起对应英文原文。

打开官方英文原文 ↗

译文由机器翻译生成,仅供理解参考;有疑问请以官方英文原文为准。

中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 100%

10.10.2 MyISAM 键缓存10.10.2 The MyISAM Key Cache

为了最小化磁盘 I/O,MyISAM 存储引擎利用了许多数据库管理系统使用的一种策略。它使用缓存机制将最常访问的表块保存在内存中:To minimize disk I/O, the MyISAM storage engine exploits a strategy that is used by many database management systems. It employs a cache mechanism to keep the most frequently accessed table blocks in memory:

  • 对于索引块,会维护一个称为 键缓存(或 键缓冲区)的特殊结构。该结构包含若干块缓冲区,用于存放使用频率最高的索引块。For index blocks, a special structure called the key cache (or key buffer) is maintained. The structure contains a number of block buffers where the most-used index blocks are placed.

  • 对于数据块,MySQL 不使用特殊缓存。相反,它依赖于本地操作系统的文件系统缓存。For data blocks, MySQL uses no special cache. Instead it relies on the native operating system file system cache.

本节首先描述了 MyISAM 键缓存的基本操作。然后讨论了提高键缓存性能并使您能够更好地控制缓存操作的功能:This section first describes the basic operation of the MyISAM key cache. Then it discusses features that improve key cache performance and that enable you to better control cache operation:

  • 多个会话可以同时访问缓存。Multiple sessions can access the cache concurrently.

  • 您可以设置多个键缓存,并将表索引分配给特定缓存。You can set up multiple key caches and assign table indexes to specific caches.

要控制键缓存的大小,请使用 key_buffer_size 系统变量。如果将此变量设置为零,则不使用键缓存。如果 key_buffer_size 值太小,无法分配最少数量的块缓冲区(8 个),键缓存也不会被使用。To control the size of the key cache, use the key_buffer_size system variable. If this variable is set equal to zero, no key cache is used. The key cache also is not used if the key_buffer_size value is too small to allocate the minimal number of block buffers (8).

当键缓存不可用时,索引文件仅使用操作系统提供的本地文件系统缓冲进行访问。(换句话说,表索引块的访问策略与表数据块的访问策略相同。)When the key cache is not operational, index files are accessed using only the native file system buffering provided by the operating system. (In other words, table index blocks are accessed using the same strategy as that employed for table data blocks.)

索引块是访问MyISAM索引文件的连续单元。通常情况下,索引块的大小等于索引 B 树节点的大小。(索引在磁盘上使用 B 树数据结构表示。树的底部节点是叶节点。叶节点上方的节点是非叶节点。)An index block is a contiguous unit of access to the MyISAM index files. Usually the size of an index block is equal to the size of nodes of the index B-tree. (Indexes are represented on disk using a B-tree data structure. Nodes at the bottom of the tree are leaf nodes. Nodes above the leaf nodes are nonleaf nodes.)

键缓存结构中的所有块缓冲区大小相同。这个大小可以等于、大于或小于表索引块的大小。通常这两个值之一是另一个的倍数。All block buffers in a key cache structure are the same size. This size can be equal to, greater than, or less than the size of a table index block. Usually one these two values is a multiple of the other.

当必须访问任何表索引块的数据时,服务器首先检查该数据是否可在键缓存的某个块缓冲区中获得。如果可以,服务器将访问键缓存中的数据而不是磁盘上的数据。也就是说,它从缓存中读取或写入数据,而不是从磁盘读取或写入。否则,服务器会选择一个包含其他表索引块(或多个块)的缓存块缓冲区,并用所需的表索引块的副本替换其中的数据。一旦新的索引块进入缓存,就可以访问索引数据。When data from any table index block must be accessed, the server first checks whether it is available in some block buffer of the key cache. If it is, the server accesses data in the key cache rather than on disk. That is, it reads from the cache or writes into it rather than reading from or writing to disk. Otherwise, the server chooses a cache block buffer containing a different table index block (or blocks) and replaces the data there by a copy of required table index block. As soon as the new index block is in the cache, the index data can be accessed.

如果恰好被选中用于替换的块已经被修改,该块被视为“脏的”。在这种情况下,在被替换之前,其内容会被刷新到它所在的表索引中。If it happens that a block selected for replacement has been modified, the block is considered “dirty.” In this case, prior to being replaced, its contents are flushed to the table index from which it came.

通常服务器遵循LRU(最近最少使用)策略:在选择替换块时,它会选择最近最少使用的索引块。为了使这一选择更容易,键缓存模块将所有使用过的块维护在一个特殊的列表中(LRU 链),按使用时间排序。当一个块被访问时,它是最近使用的,并放置在列表的末尾。当需要替换块时,列表开头的块是最近最少使用的,并成为最先被清除的候选块。Usually the server follows an LRU (Least Recently Used) strategy: When choosing a block for replacement, it selects the least recently used index block. To make this choice easier, the key cache module maintains all used blocks in a special list (LRU chain) ordered by time of use. When a block is accessed, it is the most recently used and is placed at the end of the list. When blocks need to be replaced, blocks at the beginning of the list are the least recently used and become the first candidates for eviction.

InnoDB 存储引擎也使用 LRU 算法来管理其缓冲池。参见 第 17.5.1 节,“缓冲池”。The InnoDB storage engine also uses an LRU algorithm, to manage its buffer pool. See Section 17.5.1, “Buffer Pool”.