If you are ever curious about what and how exactly the Database pages got stored in the buffer pool, If you ever did…Yes, there is a fantastic DMV “dm_os_buffer_descriptors” which exposes very cool information of storage Engine. Interestingly it also gives you information regarding whether a page is a dirty or a clean! which I think is really cool…
Basically you can see the distribution of pages in Buffer cache based on the database using this DMV!
Note: When a data page is read from disk, the page is copied into the SQL Server buffer pool and cached for reuse where each cached data page has one buffer descriptor.
See below screenshot for what it returned on my test Instance:
For Dirty Pages, keep focus on “Is_Modified” column.
Well, Below is an useful script for determining Dirty Pages/Clean Pages per database:
SELECT
(CASE WHEN ([database_id] = 32767) THEN ‘Resource Database’ ELSE DB_NAME (database_id) END) AS ‘Database Name’,
SUM(CASE WHEN ([is_modified] = 1) THEN 1 ELSE 0 END) AS DirtyPageCount,
SUM(CASE WHEN ([is_modified] = 1) THEN 0 ELSE 1 END) AS CleanPageCount
FROM sys.dm_os_buffer_descriptors
GROUP BY database_id
ORDER BY DB_NAME(database_id)
GO
See below for what am I talking about:
Isn’t it awesome?
well, How to get total cached pages/ DB using this DMV?
SELECT COUNT(*)AS cached_pages_count
,CASE database_id
WHEN 32767 THEN ‘ResourceDb’
ELSE db_name(database_id)
END AS database_name
FROM sys.dm_os_buffer_descriptors
GROUP BY db_name(database_id) ,database_id
ORDER BY cached_pages_count DESC;