See what is in the SQL Server buffer pool
Which databases, and which tables and indexes within them, are occupying the buffer pool right now.
The task
sys.dm_os_buffer_descriptors has one row per cached 8 KB page in the buffer pool. Grouped by database, it shows how the instance divides RAM; joined to allocation units and partitions, it names the tables and indexes holding the most pages. That is the evidence to bring to a discussion about memory pressure or a falling page life expectancy. On a server with a large buffer pool the DMV has millions of rows and the query takes a while, so avoid running it every minute.
The snippet
Written for SQL Server.
SELECT DB_NAME(database_id) AS database_name,
COUNT(*) AS cached_pages,
CAST(COUNT(*) * 8 / 1024.0 AS DECIMAL(12,1)) AS cached_mb
FROM sys.dm_os_buffer_descriptors
WHERE database_id <> 32767
GROUP BY database_id
ORDER BY cached_pages DESC;
작동 방식
database_id 32767 is the hidden resource database, excluded here. Each row is one 8 KB page, so pages times 8 divided by 1024 gives megabytes. The per-object variant restricts to the current database (bd.database_id = DB_ID()) and joins through sys.allocation_units to sys.partitions to name the table and index; index_id 0 is a heap, 1 the clustered index, 2 and above nonclustered indexes. The join on container_id = partition_id holds for in-row, row-overflow and LOB allocation units alike, because hobt_id and partition_id have the same value. is_modified = 1 marks dirty pages that have not yet been written back to disk; add SUM(CASE WHEN is_modified = 1 THEN 1 ELSE 0 END) to see how much of the pool is waiting for a checkpoint.
Variations
Top cached objects in the current database
SELECT TOP 25
OBJECT_SCHEMA_NAME(p.object_id) AS schema_name,
OBJECT_NAME(p.object_id) AS object_name,
p.index_id,
COUNT_BIG(*) AS cached_pages,
CAST(COUNT_BIG(*) * 8 / 1024.0 AS DECIMAL(12,1)) AS cached_mb
FROM sys.dm_os_buffer_descriptors bd
JOIN sys.allocation_units au ON au.allocation_unit_id = bd.allocation_unit_id
JOIN sys.partitions p ON p.partition_id = au.container_id
WHERE bd.database_id = DB_ID()
GROUP BY p.object_id, p.index_id
ORDER BY cached_pages DESC;
Questions that come up
An object I query constantly is not in the cache. Why?
Pages leave the pool when something else needs the memory more. Large scans of other tables, a table bigger than the pool, or another database on the same instance can all push it out, and this view only shows the current moment. The per-database query shows whether another database dominates; for scans, an index that lets the queries read fewer pages usually helps more than adding RAM.
Is a low page life expectancy a problem?
Page life expectancy is the number of seconds a page is expected to stay in the pool at the current rate of reads. It starts at zero after a restart and dips during big scans or index rebuilds, so watch its trend rather than one reading, and on NUMA hardware check the per-node values under Buffer Node as well. Old fixed thresholds such as 300 seconds date from servers with a few GB of memory. A sustained drop together with rising PAGEIOLATCH waits in the wait summary is the real sign of memory pressure.
Keep reading
Run these with autocomplete that knows your tables
SQLly is a native SQL workbench for macOS, Windows and Linux. Paste a snippet into a live connection and you get schema-aware IntelliSense, a plan viewer and guards against unsafe edits.