Mysql InnoDB的内存管理--LRU链表

Mysql Innodb Buffer Pool的LRU设计思路:

Mysql InnoDB的内存管理--LRU链表


1.InnoDB把整个LRU链表按照5:3的比例(由参数innodb_old_blocks_pct控制)分为old区域和new区域,其中LRU_OLD占5/8,LRU_NEW占3/8
2.如果要访问一个不在当前链表里的数据页时,新的数据页会插入到LRU_OLD的队首,如果链表满的话,会从LRU_OLD的队尾相应的淘汰掉一个数据页
3.处于LRU_OLD里的数据页,每次访问时都需要判断一下:
    1)如果该数据页在LRU链表中存在时间超过1s,就将其移动到链表头部(这里面说的链表指的是整个LRU链表)
    2)如果该数据页在LRU链表中存在的时间短于1s,其位置不变
    3)1s这个时间是由参数innodb_old_blocks_time控制的
    
innodb_old_blocks_time:
Dynamic Yes、DEFAULT VALUE 1000、Scope Global、Minimum Value 0、Maximum Value 2**32-1
Non-zero values protect against the buffer pool being filled by data that is referenced only for a brief period, such as during a full table scan. Increasing this value offers more protection against full table scans interfering with data cached in the buffer pool.
Specifies how long in milliseconds a block inserted into the old sublist must stay there after its first access before it can be moved to the new sublist.

innodb_old_blocks_pct:
Dynamic Yes、DEFAULT VALUE 37、Scope Global、Minimum Value 5、Maximum Value 95
Specifies the approximate percentage of the InnoDB buffer pool used for the old block sublist. The range of values is 5 to 95. The default value is 37 (that is, 3/8 of the pool). Often used in combination with innodb_old_blocks_time.

这种设计思路主要是为了防止那种一次性的大的查询(比如全表扫描)把内存的LRU链表全部更新掉,进而影响Buffer Pool的内存命中率