Oracle Buffer Cache
The buffer cache (database buffer cache) is the area of the SGA that holds blocks of data files. When a server process needs a block, it looks in the buffer cache first and reads from the data file only if the block is not there. Modified blocks (dirty blocks) are written to the data file later by the DBWn processes.
Key specs
| Item | Value |
|---|---|
| Size setting | DB_CACHE_SIZE, or set SGA_TARGET or MEMORY_TARGET for automatic management |
| Multiple pools | DEFAULT, KEEP (objects you want to keep), RECYCLE (objects read once and done) |
| Caches for non-standard block sizes | DB_2K_CACHE_SIZE ~ DB_32K_CACHE_SIZE |
| Eviction rule | Touch-count-based LRU |
| Large scan protection | Direct path read (bypasses the buffer cache), RECYCLE pool |
| Dirty block writes | DBWn (can be several) |
| Size advice | V$DB_CACHE_ADVICE |
| Operating system cache | Bypass with FILESYSTEMIO_OPTIONS = SETALL or ASM |
How it works
Touch count. Each buffer counts its accesses (touch count). Repeated accesses within a short time (3 seconds by default) count as one. A newly read block enters the middle of the LRU list, and when looking for eviction candidates, buffers with a high touch count are moved to the hot side and kept.
Direct path read. When you fully scan a table that is large relative to the buffer cache, the server process reads blocks directly into its own PGA instead of the buffer cache (the direct path read wait event). Parallel queries use this method by default too. A large scan does not push out the buffer cache, but scanning the same table several times gets no cache benefit. Before reading, the dirty blocks of that segment must first be written to disk (checkpoint).
KEEP and RECYCLE. Small reference tables used often are assigned BUFFER_POOL KEEP, and large tables read randomly just once are assigned BUFFER_POOL RECYCLE, so they do not push each other out. The size of each pool is set by DB_KEEP_CACHE_SIZE and DB_RECYCLE_CACHE_SIZE.
Operating system cache. If data files are on a file system, they can go through the operating system cache by default. Turning on direct I/O and asynchronous I/O with FILESYSTEMIO_OPTIONS = SETALL, or placing data files on ASM, skips the operating system cache. In this case double caching goes away, but the buffer cache must be sized generously.
Configuration and checks
SELECT component, current_size / 1024 / 1024 AS mb FROM v$sga_dynamic_components WHERE component LIKE '%buffer cache%';
-- How physical reads would change if the cache size changed
SELECT size_for_estimate AS mb, estd_physical_read_factor, estd_physical_reads
FROM v$db_cache_advice WHERE name = 'DEFAULT' ORDER BY size_for_estimate;
-- Objects currently in the buffer cache
SELECT o.object_name, count(*) AS blocks
FROM v$bh b JOIN dba_objects o ON o.data_object_id = b.objd
GROUP BY o.object_name ORDER BY 2 DESC FETCH FIRST 10 ROWS ONLY;
ALTER TABLE codes STORAGE (BUFFER_POOL KEEP);
In an execution plan, consistent gets (blocks read from the buffer cache) and physical reads (blocks read from disk) show the buffer use of a single statement.
Comparison with other DBMSs
| Oracle | PostgreSQL | InnoDB | SQL Server | |
|---|---|---|---|---|
| Eviction rule | Touch-count LRU | Clock sweep | Midpoint insertion LRU | LRU-2 |
| Large scan protection | Direct path read, RECYCLE | Ring buffer | old region | Reflects the second access time |
In the articles
NOTE The criteria for choosing direct path reads and their defaults may differ by version.