--buffer中缓存每个数据库所占的buffer  

SET TRAN ISOLATION LEVEL READ UNCOMMITTED  

SELECT  

   ISNULL(DB_NAME(database_id), 'ResourceDb') AS DatabaseName  

   , CAST(COUNT(row_count) * 8.0 / (1024.0) AS DECIMAL(28,2))  

                                                  AS [Size (MB)]  

FROM sys.dm_os_buffer_descriptors  

GROUP BY database_id  

ORDER BY DatabaseName  



--当前数据库中每个表所占缓存的大小和页数  


SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED  

SELECT  

    OBJECT_NAME(p.[object_id]) AS [TableName]  

    , (COUNT(*) * 8) / 1024   AS [Buffer size(MB)]  

    , ISNULL(i.name, '-- HEAP --') AS ObjectName  

    ,  COUNT(*) AS NumberOf8KPages  

FROM sys.allocation_units AS a  

INNER JOIN sys.dm_os_buffer_descriptors AS b  

   ON a.allocation_unit_id = b.allocation_unit_id  

INNER JOIN sys.partitions AS p  

INNER JOIN sys.indexes i ON p.index_id = i.index_id  

                        AND p.[object_id] = i.[object_id]  

   ON a.container_id = p.hobt_id  

WHERE b.database_id = DB_ID()  

 AND p.[object_id] > 100  

GROUP BY p.[object_id], i.name  

ORDER BY NumberOf8KPages DESC  




--数据库级别等待的IO  

SET TRAN ISOLATION LEVEL READ UNCOMMITTED  

SELECT DB_NAME(database_id) AS [DatabaseName]  

 , SUM(CAST(io_stall / 1000.0 AS DECIMAL(20,2))) AS [IO stall (secs)]  

 , SUM(CAST(num_of_bytes_read / 1024.0 / 1024.0 AS DECIMAL(20,2)))  

                                                       AS [IO read (MB)  

 , SUM(CAST(num_of_bytes_written / 1024.0 / 1024.0  AS DECIMAL(20,2)))  

                                                    AS [IO written (MB)  

 , SUM(CAST((num_of_bytes_read + num_of_bytes_written)  

                  / 1024.0 / 1024.0 AS DECIMAL(20,2))) AS [TotalIO (MB)  

FROM sys.dm_io_virtual_file_stats(NULL, NULL)  

GROUP BY database_id  

ORDER BY [IO stall (secs)] DESC  


--按文件查看IO情况  

SET TRAN ISOLATION LEVEL READ UNCOMMITTED  

SELECT DB_NAME(database_id) AS [DatabaseName]  

 , file_id  

 , SUM(CAST(io_stall / 1000.0 AS DECIMAL(20,2))) AS [IO stall (secs)]  

 , SUM(CAST(num_of_bytes_read / 1024.0 / 1024.0 AS DECIMAL(20,2)))  

                                                    AS [IO read (MB)]  

 , SUM(CAST(num_of_bytes_written / 1024.0 / 1024.0  AS DECIMAL(20,2)))  

                                                 AS [IO written (MB)]  

                                                   , SUM(CAST((num_of_bytes_read + num_of_bytes_written)  

               / 1024.0 / 1024.0 AS DECIMAL(20,2))) AS [TotalIO (MB)]  

FROM sys.dm_io_virtual_file_stats(NULL, NULL)  

GROUP BY database_id, file_id  

ORDER BY [IO stall (secs)] DESC