SQL Server 内存设置和检测

通过设置 SQL Server 最大内存(如图设置10G),长时间运行后,会占用最大的内存:

image.png

https://logicalread.com/sql-server-memory-buffer-pools-pd01/#.WjSuAEqWaM8

执行下面的命令,可以看到所有数据库缓存的pages:

SELECT COUNT(*) AS cached_pages_count, (CASE database_id WHEN 32767 THEN 'ResourceDb' ELSE db_name(database_id)    END) AS Database_name FROM sys.dm_os_buffer_descriptors GROUP BY db_name(database_id) ,database_id ORDER BY cached_pages_count DESC;

执行下面的命令,可以看到数据库下哪一个数据表缓存的pages最多:

SELECT COUNT(*) AS cached_pages_count, name, index_id FROM sys.dm_os_buffer_descriptors AS bd  INNER JOIN  (    SELECT 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_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 WHERE database_id = db_id() GROUP BY name, index_id ORDER BY cached_pages_count DESC;

从列表中分析是否有异常的表缓存的pages过多,并进行针对性的修复和优化。