如何查看SQL Server 2008中的内存中缓存了什么?


13

有没有一种方法可以找出SQL Server 2008 R2中缓存的内容?我找到了以下不错的文章:http : //blog.sqlauthority.com/2010/06/17/sql-server-data-pages-in-buffer-pool-data-stored-in-memory-cache。但是,我想知道每个表和索引存储了多少数据(例如百分比和KB)。是否有一些简单的方法来获取此类数据?

Answers:


16

您可以使用以下查询找到存储在缓冲池(数据缓存)中的内容:

这里

select
       count(*)as cached_pages_count,
       obj.name as objectname,
       ind.name as indexname,
       obj.index_id as indexid
from sys.dm_os_buffer_descriptors as bd
    inner join
    (
        select       object_id as objectid,
                           object_name(object_id) as name,
                           index_id,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       object_id as objectid,
                           object_name(object_id) as name,
                           index_id,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 outer join sys.indexes ind 
  on  obj.objectid = ind.object_id
 and  obj.index_id = ind.index_id
where bd.database_id = db_id()
  and bd.page_type in ('data_page', 'index_page')
group by obj.name, ind.name, obj.index_id
order by cached_pages_count desc

优秀参考文献:存储引擎内部:缓冲池中有什么?保罗·兰德尔(Paul Randal)。


5

您可以使用动态管理视图列出当前缓存的页面,并按database_id对其进行过滤:

   select top 100 * from sys.dm_os_buffer_descriptors

然后,您可以看到DBCC PAGE列出对象页面的命令。良好参考:http : //www.mssqltips.com/sqlservertip/1578/using-dbcc-page-to-examine-sql-server-table-and-index-data/

但是,要由您来组合结果,这似乎并不容易:)。让我们知道您何时提出有效的方法。


0

试试这个SQL查询:

select count(*)*8/1024 AS 'Cached Size (MB)'        
,case database_id                
when 32767 then 'ResourceDB'                
else db_name(database_id)                
end as 'Database'
from sys.dm_os_buffer_descriptors
where page_type in
(
'INDEX_PAGE'
,'DATA_PAGE'
)
group by db_name(database_id), database_id
order by 'Cached Size (MB)' desc
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.