暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

MySQL InnoDB内存结构之缓冲池

韩哥有话说 2021-04-28
733

InnoDB内存结构之缓冲池

缓冲池是主内存中的一个区域,在InnoDB
访问表和索引数据时会在其中进行 高速缓存。缓冲池允许直接从内存中处理经常使用的数据,从而加快了处理速度。在专用服务器上,通常将多达80%的物理内存分配给缓冲池。

为了提高大容量读取操作的效率,缓冲池被分为多个页面,这些页面可能包含多个行。为了提高缓存管理的效率,缓冲池被实现为页面的链接列表。使用LRU算法的变体将很少使用的数据从缓存中老化掉 。

知道如何利用缓冲池将经常访问的数据保留在内存中是MySQL优化的重要方面。

缓冲池LRU算法

使用最近最少使用(LRU)算法的变体,将缓冲池作为列表进行管理。当需要空间以将新页面添加到缓冲池时,将驱逐最近使用最少的页面,并将新页面添加到列表的中间。此中点插入策略将列表视为两个子列表:

最前面是最近访问过的新页面(“ 年轻 ”) 的子列表在末尾,是最近访问过的旧页面的子列表

缓冲池列表


该算法将常用页面保留在新的子列表中。旧的子列表包含不常用的页面;这些页面是驱逐的候选对象。

默认情况下,该算法的操作如下:

3/8的缓冲池专用于旧的子列表。列表的中点是新子列表的尾部与旧子列表的头相交的边界。InnoDB
将页面读入缓冲池时,它首先将其插入中点(旧子列表的头部)。可以读取页面,因为它是用户启动的操作(例如SQL查询)所必需的,或作为的自动执行的预读操作的一部分 InnoDB
访问旧子列表中的页面会使其变得 “ 年轻 ”,将其移至新子列表的头部。如果由于用户启动的操作而需要读取页面,则将立即进行首次访问,并使页面年轻。如果由于预读操作而读取了该页面,则第一次访问不会立即发生,并且在退出该页面之前可能根本不会发生。随着数据库的运行,通过移至列表的末尾,缓冲池中未被访问的页面将“ 老化 ”。新的和旧的子列表中的页面都会随着其他页面的更新而老化。随着将页面插入中点,旧子列表中的页面也会老化。最终,未使用的页面到达旧子列表的尾部并被逐出。

默认情况下,查询读取的页面会立即移入新的子列表,这意味着它们在缓冲池中停留的时间更长。例如,针对mysqldump操作或SELECT
不带WHERE
子句的 语句 执行的表扫描可以将大量数据带入缓冲池,并驱逐同等数量的旧数据,即使不再使用新数据也是如此。同样,由预读后台线程加载且仅访问一次的页面将移至新列表的开头。这些情况可能会将常用页面推到旧的子列表,在此它们将被逐出。有关优化此行为的信息,请参见 “使缓冲池扫描具有抵抗力”和 “配置InnoDB缓冲池预取(预读)”。

InnoDB
标准监视器输出在BUFFER POOL AND MEMORY
有关缓冲池LRU算法操作的部分中包含几个字段。有关详细信息,请参阅使用InnoDB Standard Monitor监视缓冲池。

缓冲池配置

您可以配置缓冲池的各个方面以提高性能。

理想情况下,您可以将缓冲池的大小设置为与实际一样大的值,从而为服务器上的其他进程留出足够的内存以运行而不会进行过多的分页。缓冲池越大,就越InnoDB
像内存数据库一样,从磁盘读取一次数据,然后在后续读取期间从内存访问数据。请参见 “配置InnoDB缓冲池大小”。
在具有足够内存的64位系统上,可以将缓冲池分成多个部分,以最大程度地减少并发操作之间的内存结构争用。有关详细信息,请参见“配置多个缓冲池实例”。您可以将频繁访问的数据保留在内存中,而不管操作中突然出现的活动高峰如何将大量不经常访问的数据带入缓冲池中。有关详细信息,请参见 “使缓冲池扫描具有抵抗力”。您可以控制如何以及何时执行预读请求,以异步方式将页面预取到缓冲池中,从而预期很快将需要这些页面。有关详细信息,请参见“配置InnoDB缓冲池预取(预读)”。您可以控制何时进行后台冲洗,以及是否根据工作负荷动态调整冲洗速率。有关详细信息,请参见 “配置缓冲池刷新”。您可以配置如何InnoDB
保留当前缓冲池状态,以免在服务器重新启动后进行冗长的预热。有关详细信息,请参见 “保存和恢复缓冲池状态”。

使用InnoDB标准监视器监视缓冲池

InnoDB
可以使用访问的标准监视器输出, SHOW ENGINE INNODB STATUS
提供有关缓冲池操作的度量。缓冲池度量标准位于BUFFER POOL AND MEMORY
“ InnoDB
标准监视器”输出中的部分,其 外观类似于以下内容:

----------------------
BUFFER POOL AND MEMORY
----------------------
Total large memory allocated 2198863872
Dictionary memory allocated 776332
Buffer pool size 131072
Free buffers 124908
Database pages 5720
Old database pages 2071
Modified db pages 910
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 4, not young 0
0.10 youngs/s, 0.00 non-youngs/s
Pages read 197, created 5523, written 5060
0.00 reads/s, 190.89 creates/s, 244.94 writes/s
Buffer pool hit rate 1000 / 1000, young-making rate 0 / 1000 not
0 / 1000
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read
ahead 0.00/s
LRU len: 5720, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]

下表描述了InnoDB
标准监视器报告的缓冲池度量标准。

注意

InnoDB
 标准监视器输出中 提供的每秒平均值基于自InnoDB
上次打印标准监视器输出以来经过的时间 。

表14.2 InnoDB缓冲池指标

NameDescription
Total memory allocatedThe total memory allocated for the buffer pool in bytes.
Dictionary memory allocatedThe total memory allocated for the InnoDB
 data dictionary in bytes.
Buffer pool sizeThe total size in pages allocated to the buffer pool.
Free buffersThe total size in pages of the buffer pool free list.
Database pagesThe total size in pages of the buffer pool LRU list.
Old database pagesThe total size in pages of the buffer pool old LRU sublist.
Modified db pagesThe current number of pages modified in the buffer pool.
Pending readsThe number of buffer pool pages waiting to be read into the buffer pool.
Pending writes LRUThe number of old dirty pages within the buffer pool to be written from the bottom of the LRU list.
Pending writes flush listThe number of buffer pool pages to be flushed during checkpointing.
Pending writes single pageThe number of pending independent page writes within the buffer pool.
Pages made youngThe total number of pages made young in the buffer pool LRU list (moved to the head of sublist of “new” pages).
Pages made not youngThe total number of pages not made young in the buffer pool LRU list (pages that have remained in the “old” sublist without being made young).
youngs/sThe per second average of accesses to old pages in the buffer pool LRU list that have resulted in making pages young. See the notes that follow this table for more information.
non-youngs/sThe per second average of accesses to old pages in the buffer pool LRU list that have resulted in not making pages young. See the notes that follow this table for more information.
Pages readThe total number of pages read from the buffer pool.
Pages createdThe total number of pages created within the buffer pool.
Pages writtenThe total number of pages written from the buffer pool.
reads/sThe per second average number of buffer pool page reads per second.
creates/sThe per second average number of buffer pool pages created per second.
writes/sThe per second average number of buffer pool page writes per second.
Buffer pool hit rateThe buffer pool page hit rate for pages read from the buffer pool memory vs from disk storage.
young-making rateThe average hit rate at which page accesses have resulted in making pages young. See the notes that follow this table for more information.
not (young-making rate)The average hit rate at which page accesses have not resulted in making pages young. See the notes that follow this table for more information.
Pages read aheadThe per second average of read ahead operations.
Pages evicted without accessThe per second average of the pages evicted without being accessed from the buffer pool.
Random read aheadThe per second average of random read ahead operations.
LRU lenThe total size in pages of the buffer pool LRU list.
unzip_LRU lenThe total size in pages of the buffer pool unzip_LRU list.
I/O sumThe total number of buffer pool LRU list pages accessed, for the last 50 seconds.
I/O curThe total number of buffer pool LRU list pages accessed.
I/O unzip sumThe total number of buffer pool unzip_LRU list pages accessed.
I/O unzip curThe total number of buffer pool unzip_LRU list pages accessed.

注意事项

youngs/s
指标仅适用于旧页面。它基于对页面的访问次数而不是页面数。可以对给定页面进行多次访问,所有访问都计入在内。如果youngs/s
在没有大的扫描发生时看到非常低的 值,则可能需要减少延迟时间或增加用于旧子列表的缓冲池的百分比。增加百分比会使旧的子列表变大,因此该子列表中的页面需要更长的时间才能移到尾部,这增加了再次访问这些页面并使它们变年轻的可能性。
non-youngs/s
指标仅适用于旧页面。它基于对页面的访问次数而不是页面数。可以对给定页面进行多次访问,所有访问都计入在内。如果non-youngs/s
执行大型表扫描时看不到较高的值(较高的youngs/s
 值),请增加延迟值。
young-making
比率说明了对所有缓冲池页面的访问,而不仅仅是访问了旧子列表中的页面。该young-making
速率和 not
速率通常不会加总到整个缓冲池的命中率。旧子列表中的页面命中会导致页面移动到新的子列表,但是新子列表中的页面命中只会导致页面与列表的头部保持一定距离时才移动到列表的头部。
not (young-making rate)
是由于innodb_old_blocks_time
未满足所定义的延迟,或者由于新子列表中的页面点击未导致页面移动到头部而导致页面访问未使页面变年轻的平均点击率 。此速率说明了对所有缓冲池页面的访问,而不仅仅是访问旧子列表中的页面。

缓冲池服务器状态变量和 INNODB_BUFFER_POOL_STATS
表提供了许多与InnoDB
标准监视器输出中相同的缓冲池度量 标准。有关更多信息,请参见 示例,“查询INNODB_BUFFER_POOL_STATS表”。

最后修改时间:2021-04-28 12:08:20
文章转载自韩哥有话说,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论