Skip to content

Instantly share code, notes, and snippets.

@Otterpohl
Created May 13, 2022 13:58
Show Gist options
  • Save Otterpohl/d1dee7aaf20e663f983148414505e046 to your computer and use it in GitHub Desktop.
Save Otterpohl/d1dee7aaf20e663f983148414505e046 to your computer and use it in GitHub Desktop.
Gets how much data is stored in cache per table
/*
https://blog.sqlauthority.com/2010/06/17/sql-server-data-pages-in-buffer-pool-data-stored-in-memory-cache/
*/
SELECT COUNT(*) AS cached_pages_count
,obj.name
,obj.index_id
,i.name AS IndexName
FROM sys.dm_os_buffer_descriptors AS bd
INNER JOIN
(
SELECT p.object_id
,OBJECT_NAME(p.object_id) AS name
,p.index_id
,au.allocation_unit_id
FROM sys.allocation_units AS au
INNER JOIN sys.partitions AS p
ON au.container_id = p.hobt_id
AND
(
au.type = 1
OR au.type = 3
)
UNION ALL
SELECT p.object_id
,OBJECT_NAME(p.object_id) AS name
,p.index_id
,au.allocation_unit_id
FROM sys.allocation_units AS au
INNER JOIN sys.partitions AS p
ON au.container_id = p.partition_id
AND au.type = 2
) AS obj
ON bd.allocation_unit_id = obj.allocation_unit_id
LEFT JOIN sys.sysindexes AS i
ON obj.object_id = i.id
AND obj.index_id = i.indid
WHERE bd.database_id = DB_ID()
GROUP BY obj.name
,obj.index_id
,i.name
ORDER BY cached_pages_count DESC;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment