如何按表查找内存使用情况?

7 sql-server-2008 sql-server

我想知道每个表使用了多少内存。这些信息是否可以在 DMV 或其他地方获得?

Kin*_*hah 9

你能做的是

  1. 使用以下方法找出缓冲池中消耗最高内存的数据库:

    SELECT COUNT(*) AS cached_pages_count , 
        ( COUNT(*) * 8.0 ) / 1024 AS MB , 
        CASE database_id 
          WHEN 32767 THEN 'ResourceDb' 
          ELSE DB_NAME(database_id) 
        END AS Database_name 
    FROM sys.dm_os_buffer_descriptors 
    GROUP BY database_id
    
    Run Code Online (Sandbox Code Playgroud)
  2. 使用以下查询深入数据库:

    set nocount on;
    set transaction isolation level read uncommitted;
    select
       count(*)as cached_pages_count,
       (COUNT(*) * 8.0) / 1024 AS Total_MB_Occupied, -- convert pages into MB -  the page size is 8 KB for sql server
       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)

参考 :