SQL Inside the Storage Engine: What’s in the buffer pool?
The answer is that pages are stored in buffers in the buffer pool (aka buffer cache or data cache), and the buffers are indeed hashed so they can easily be found by database. You can see what pages are currently in the buffer pool, and their status using thesys.dm_os_buffer_descriptors DMV in 2005:
1
2
| SELECT * FROM sys.dm_os_buffer_descriptors;GO |
database_id file_id page_id page_level allocation_unit_id page_type row_count free_space_in_bytes is_modified----------- -------- -------- ----------- -------------------- -------------- ----------- ------------------- -----------1 1 9 0 6488064 BOOT_PAGE 1 7362 01 1 6 0 6488064 DIFF_MAP_PAGE 2 6 01 1 7 0 6488064 ML_MAP_PAGE 2 6 01 1 104 0 262144 DATA_PAGE 100 4196 01 1 105 0 851968 DATA_PAGE 65 5041 01 1 106 0 262144 DATA_PAGE 197 413 01 1 107 0 262144 DATA_PAGE 207 23 01 1 108 1 262144 INDEX_PAGE 7 7949 0.. |
I cut off the output rather than list all 3258 pages in the buffer pool on my laptop. The DMV gives you back some info from the pages themselves as well as you can see(remember all this is in memory so it’s quick to find).
I played around with the DMV a little bit and came up with a neat script that will tell you may many clean and dirty pages there are in the buffer pool per-database.
I played around with the DMV a little bit and came up with a neat script that will tell you may many clean and dirty pages there are in the buffer pool per-database.
1
2
3
4
5
6
7
8
| SELECT (CASE WHEN ([is_modified] = 1) THEN N'Dirty' ELSE N'Clean' END) AS N'Page State', (CASE WHEN ([database_id] = 32767) THEN N'Resource Database' ELSE DB_NAME ([database_id]) END) AS N'Database Name', COUNT (*) AS N'Page Count'FROM sys.dm_os_buffer_descriptors GROUP BY [database_id], [is_modified] ORDER BY [database_id], [is_modified];GO |
Page State Database Name Page Count---------- ----------------------- ----------Clean master 302Dirty master 1Clean tempdb 88Dirty tempdb 52Clean model 56Clean msdb 622Dirty msdb 5Clean adventureworks 110Clean DemoRestoreOrRepair 64Clean DBMaint2008 88Clean DemoFatalCorruption1 64Clean DemoFatalCorruption2 64Clean broken 64Clean DemoFatalCorruption3 64Clean DemoCorruptMetadata 111Clean DemoDataPurity 88Clean SalesDB 123Clean DemoNCIndex 88Clean shrinktest 88Clean DemoRestoreOrRepairCopy 64Clean DemoSuspect 64Clean FileHeaderTest 96Clean MultiFileDB 96Clean HA2008 88Clean SalesDB_Snapshot 21Clean BootPageTest 88Clean Resource Database 599 |
Later this week I’ll try to blog a script that can tell you how much of a particular table is in memory ([Edit 12/8/11: that script is here]). Enjoy!