Buffers used by current database objects
This SQL query can take some time to execute, especially on a busy database instance. It's designed to provide insights into the buffer usage of your SQL Server, helping you identify which objects are consuming the most memory.
SELECT SCHEMA_NAME(o.Schema_ID) AS [Schema Name],
OBJECT_NAME(p.[object_id]) AS [Object Name],
p.index_id,
CAST(COUNT(*) / 128.0 AS DECIMAL(10, 2)) AS [Buffer size(MB)],
COUNT(*) AS [BufferCount],
p.[Rows] AS [Row Count],
p.data_compression_desc AS [Compression Type]
FROM sys.allocation_units AS a WITH (NOLOCK)
INNER JOIN sys.dm_os_buffer_descriptors AS b WITH (NOLOCK) ON a.allocation_unit_id = b.allocation_unit_id
INNER JOIN sys.partitions AS p WITH (NOLOCK) ON a.container_id = p.hobt_id
INNER JOIN sys.objects AS o WITH (NOLOCK) ON p.object_id = o.object_id
WHERE b.database_id = CONVERT(INT, DB_ID())
AND p.[object_id] > 100
AND OBJECT_NAME(p.[object_id]) NOT LIKE N'plan_%'
AND OBJECT_NAME(p.[object_id]) NOT LIKE N'sys%'
AND OBJECT_NAME(p.[object_id]) NOT LIKE N'xml_index_nodes%'
GROUP BY o.Schema_ID,
p.[object_id],
p.index_id,
p.data_compression_desc,
p.[Rows]
ORDER BY [BufferCount] DESC
OPTION (RECOMPILE);
Understanding the Query's Output:
- Schema Name: The schema to which the object belongs.
- Object Name: The name of the database object (table, index, etc.).
- index_id: The ID of the index on the object.
- Buffer size(MB): The estimated memory consumed by the object in megabytes within the buffer cache.
- BufferCount: The number of 8KB pages allocated to the object in the buffer cache.
- Row Count: The total number of rows in the object.
- Compression Type: Indicates if the data is compressed and, if so, the type of compression used.
This query can be a valuable tool for performance tuning and memory management in your SQL Server environment. By identifying objects with high buffer consumption, you can investigate potential optimizations like indexing strategies, data compression, or query rewrites.