skip to content

In MySQL's InnoDB, how do innodb_old_blocks_pct and innodb_old_blocks_time stop a large table scan from evicting hot pages from the buffer pool?

level: middleimportance: should knowfreq 35%

answer

  1. not a strict LRU
  2. new and old sublists
  3. insert at the midpoint
  4. a delay before a page is made young
  5. Pages made young, not young

basics

~20 s

InnoDB inserts newly read pages at the head of an old sublist, 37% of the LRU list by default, and promotes one only if it is touched again after innodb_old_blocks_time (1000 ms). One-pass scan pages therefore age out first.

solid answer

~50 s

InnoDB's buffer pool LRU list is split into a **new** (young) sublist at the head and an **old** sublist at the tail. `innodb_old_blocks_pct`, 37 by default (the original 3/8), sets how much of the list is old. A page read from disk, by a query or by read-ahead, is inserted at the **midpoint**, the head of the old sublist, and eviction takes pages from the tail. A page is *made young*, moved to the head of the new sublist, when it is accessed again after `innodb_old_blocks_time` milliseconds, 1000 by default, have passed since its first access. A table scan typically touches each page a few times in quick succession and never again, so its pages stay old and are evicted first. You check it in the `BUFFER POOL AND MEMORY` section of `SHOW ENGINE INNODB STATUS`: during a scan, `non-youngs/s` should rise while `youngs/s` stays low.

code

sql · 6 lines
sql
SELECT POOL_ID, OLD_DATABASE_PAGES, PAGES_MADE_YOUNG, PAGES_NOT_MADE_YOUNG
FROM INFORMATION_SCHEMA.INNODB_BUFFER_POOL_STATS;

SET GLOBAL innodb_old_blocks_time = 5000;
-- run the nightly report
SET GLOBAL innodb_old_blocks_time = 1000;

go deeper

for a junior

Recall that InnoDB's buffer pool is not a plain LRU and that new pages enter in the middle of the list.

for a middle

Explain the new and old sublists, the 37% default, the 1000 ms promotion delay, and why a one-pass scan therefore ages out from the tail.

for a senior

Read youngs/s and non-youngs/s during and outside a scan, decide which variable to adjust and in which direction, and change it at runtime around a batch.

for a principal

Decide whether batch scans belong on the same server at all or should run elsewhere, rather than tuning the LRU around a workload mix that keeps growing.

## Why InnoDB does not use a strict LRU The **buffer pool** is InnoDB's in-memory cache of data and index pages. Under a strict least-recently-used policy, a single full scan of a table larger than the pool, for example a nightly report or a `mysqldump`, would push every page it reads to the most-recently-used end and evict the hot working set of the OLTP workload, even though the scanned pages are never needed again. InnoDB guards against this with a **midpoint insertion** variant of LRU. ## The two sublists InnoDB manages the buffer pool as one list split at a midpoint: - the **new sublist** at the head, holding pages accessed recently and repeatedly ("young" pages); - the **old sublist** at the tail, holding pages that are candidates for eviction. The rules are: 1. When InnoDB reads a page into the pool, by a user query or by **read-ahead**, it inserts it at the **midpoint**: the head of the old sublist. 2. Pages age toward the tail as other pages are inserted or made young. The page that reaches the tail of the old sublist is evicted. 3. A page in the old sublist that is accessed again is **made young**: moved to the head of the new sublist. With the default delay, that only happens if the access comes after the `innodb_old_blocks_time` window. Read-ahead pages show why rule 1 matters: InnoDB may prefetch a whole extent that nobody reads, and those pages leave from the old sublist without ever touching the hot part of the pool. ## The two settings | Variable | Default | Range | Effect | |---|---|---|---| | `innodb_old_blocks_pct` | `37` | 5 to 95 | Share of the LRU list kept as the old sublist | | `innodb_old_blocks_time` | `1000` (ms) | 0 upward | Window after a page's first access during which further accesses do not make it young | Both are global and dynamic, so they can be changed with `SET GLOBAL` without a restart. - **`innodb_old_blocks_time`** is the scan defence. A scan reads a page and touches it a few times in quick succession while it processes the rows on it. Those touches normally fall inside the 1000 ms window, so the page is never promoted and leaves from the tail. Pages that the OLTP workload touches again seconds later are promoted normally. - **`innodb_old_blocks_pct`** sizes the quarantine. Setting it low, for example `5`, limits data that is read only once to 5% of the pool when you scan tables bigger than memory. Setting it high, up to `95`, makes the algorithm close to a plain LRU. For small tables that fit in memory, leaving the default or raising it is fine. The MySQL manual recommends benchmarking before changing either in production, and notes that the effect of `innodb_old_blocks_time` is harder to predict than that of the percentage. ## Checking that it works The `BUFFER POOL AND MEMORY` section of `SHOW ENGINE INNODB STATUS` reports the LRU behaviour: - `Old database pages`: the current size of the old sublist, in pages. - `Pages made young` and `not young`: cumulative counts of promotions and of accesses that left a page in the old sublist. - `youngs/s` and `non-youngs/s`: per-second averages of those accesses, for old pages only, since the previous report. - `evicted without access`: read-ahead pages thrown away before anyone used them. Read them against the workload: - During a large scan you expect **`non-youngs/s` to rise and `youngs/s` to stay low**. If `non-youngs/s` does not rise, the manual's advice is to increase `innodb_old_blocks_time`. - If `youngs/s` is very low when **no** scan is running, pages may be ageing out before they are touched again; reduce the delay or raise `innodb_old_blocks_pct` so the old sublist is longer. The same counters are exposed as columns of `INFORMATION_SCHEMA.INNODB_BUFFER_POOL_STATS`, such as `PAGES_MADE_YOUNG` and `PAGES_NOT_MADE_YOUNG`, for graphing. ## A reporting migration example When a reporting schema moves from MyISAM, whose row data sat in the operating system's file cache, to InnoDB, its nightly scans start competing with the OLTP tables inside the same buffer pool. The defaults usually absorb this. If the morning after a report shows a cold cache, raising `innodb_old_blocks_time` for the duration of the batch, or lowering `innodb_old_blocks_pct`, keeps the working set resident. How large the pool itself should be is a separate sizing decision.

  • After a MySQL restart, how does InnoDB avoid a long buffer pool warm-up?
    With `innodb_buffer_pool_dump_at_shutdown` and `innodb_buffer_pool_load_at_startup`, both `ON` by default, InnoDB saves the page identifiers of the most recently used pages at shutdown and reads those pages back in the background after startup. `innodb_buffer_pool_dump_pct`, default 25, sets the share of each pool's most recently used pages that is dumped. `Innodb_buffer_pool_load_status` shows the load's progress.
  • What happens if innodb_old_blocks_time is set to 0?
    The delay disappears: any access to a page in the old sublist, including the scan's own repeated touches, makes it young. A scan that touches each page a few times in quick succession then promotes every page it reads, and the buffer pool behaves much more like a plain LRU, so one large scan can push the hot working set toward eviction.

  • InnoDB inserts every newly read page at the head of the LRU list.
  • innodb_old_blocks_time is measured in seconds.
  • Read-ahead pages bypass the LRU list and cannot pollute the buffer pool.
  • Changing innodb_old_blocks_pct requires a server restart.
  • A rising non-youngs/s during a scan means the buffer pool is thrashing.