如何查看 SQL Server 2008 内存中缓存的内容?

Ond*_*rka 13 sql-server-2008 sql-server memory

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

Kin*_*hah 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
Run Code Online (Sandbox Code Playgroud)

优秀参考:存储引擎内部:缓冲池中有什么?通过保罗兰德尔。


Ada*_*amL 5

您可以使用动态管理视图列出当前缓存的页面并通过 database_id 过滤它们:

   select top 100 * from sys.dm_os_buffer_descriptors
Run Code Online (Sandbox Code Playgroud)

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

然而,将结果结合起来取决于你,这似乎不是一件容易的事:)。当您想出有效的方法来做到这一点时,请告诉我们。