从sys.dm_os_memory_clerks开始。
SELECT [type] ,
SUM(virtual_memory_reserved_kb) AS [VM Reserved] ,
SUM(virtual_memory_committed_kb) AS [VM Committed] ,
SUM(awe_allocated_kb) AS [AWE Allocated] ,
SUM(shared_memory_reserved_kb) AS [SM Reserved] ,
SUM(shared_memory_committed_kb) AS [SM Committed] ,
SUM(multi_pages_kb) AS [Multipage Allocator] ,
SUM(single_pages_kb) AS [SinlgePage Allocator],
SUM(virtual_memory_reserved_kb)/(CASE WHEN SUM(virtual_memory_committed_kb)=0 THEN 1 ELSE SUM(virtual_memory_committed_kb) END ) AS [Reserved/Commit],
SUM(single_pages_kb)+SUM(multi_pages_kb) AS Stolen,
SUM(virtual_memory_committed_kb)+SUM(single_pages_kb) AS [SinlgePage Allocator]
FROM sys.dm_os_memory_clerks
GROUP BY [type]
ORDER BY [type]
其中type为Memory Clerk的名称,可以知道内存的用途。
内存中的数据页由哪些表格组成,各占多少?
sys.dm_os_buffer_descriptors
DECLARE @name NVARCHAR(100)
DECLARE @cmd NVARCHAR(1000)
DECLARE dbnames CURSOR
FOR
SELECT NAME
FROM master.dbo.sysdatabases
OPEN dbnames
FETCH NEXT FROM dbnames INTO @name
WHILE @@FETCH_STATUS = 0
BEGIN
SET @cmd = 'select b.database_id,db=db_name(b.database_id),p.object_id,p.index_id,buffer_count=count(*) from '
+ @name + '.sys.allocation_units a, ' + @name
+ '.sys.dm_os_buffer_descriptors b, ' + @name
+ '.sys.partitions p where a.allocation_unit_id=b.allocation_unit_id and a.container_id=p.hobt_id and b.database_id=db_id('''
+ @name
+ ''')
group by b.database_id,p.object_id,p.index_id order by b.database_id,buffer_count desc '
EXEC (@cmd)
FETCH NEXT FROM dbnames INTO @name
END
CLOSE dbnames
DEALLOCATE dbnames
GO
会缓存执行计划的对象:
proc:存储过程
prepared:预定义语句
Adhoc:动态查询
ReplProc:复制筛选过程
Trigger:触发器
View:视图
Default:默认值
UsrTab:用户表
SysTab:系统表
Check:Check约束
Rule:规则
可以查看各种对象各占多少内存:
SELECT objtype ,
SUM(size_in_bytes) / 1024 AS sum_size_in_KB ,
COUNT(bucketid) AS cache_counts
FROM sys.dm_exec_cached_plans
GROUP BY objtype
分析具体存储哪些对象:
SELECT usecounts ,
refcounts ,
size_in_bytes ,
cacheobjtype ,
objtype ,
TEXT
FROM sys.dm_exec_cached_plans cp
CROSS APPLY sys.dm_exec_sql_text(plan_handle)
ORDER BY objtype DESC ;
GO
--使用DMV分析SQL SERVER 启动以来做read最多的语句
qs.total_physical_reads ,
qs.execution_count ,
qs.total_physical_reads / qs.execution_count AS [Avg IO] ,
SUBSTRING(qt.text, qs.statement_start_offset / 2,
( CASE WHEN qs.statement_end_offset = -1
THEN LEN(CONVERT(NVARCHAR(MAX), qt.text)) * 2
ELSE qs.statement_end_offset
END - qs.statement_start_offset ) / 2) AS query_text ,
qt.dbid ,
dbname = DB_NAME(qt.dbid) ,
qt.objectid ,
qs.sql_handle ,
qs.plan_handle
FROM sys.dm_exec_query_stats qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS qt
ORDER BY qs.total_physical_reads DESC
qs.total_logical_reads ,
qs.execution_count ,
qs.total_logical_reads / qs.execution_count AS [Avg IO] ,
SUBSTRING(qt.text, qs.statement_start_offset / 2,
( CASE WHEN qs.statement_end_offset = -1
THEN LEN(CONVERT(NVARCHAR(MAX), qt.text)) * 2
ELSE qs.statement_end_offset
END - qs.statement_start_offset ) / 2) AS query_text ,
qt.dbid ,
dbname = DB_NAME(qt.dbid) ,
qt.objectid ,
qs.sql_handle ,
qs.plan_handle
FROM sys.dm_exec_query_stats qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS qt
ORDER BY qs.total_logical_reads DESC
--用DBCC强制释放部分SQL SERVER 内存缓存:
DBCC FREESYSTEMCACHE
DBCC FREESESSIONCACHE
DBCC FREEPROCCACHE
SELECT total_physical_memory_kb / 1024 AS [物理内存(MB)] ,
available_physical_memory_kb / 1024 AS [可用物理内存(MB)] ,
system_cache_kb / 1024 AS [系统缓存内存总量(MB)] ,
( kernel_paged_pool_kb + kernel_nonpaged_pool_kb ) / 1024 AS [内核池内存总量(MB)] ,
total_page_file_kb / 1024 AS [操作系统报告的提交限制的大小(MB)] ,
available_page_file_kb / 1024 AS [未使用的页文件的总量(MB)] ,
system_memory_state_desc AS [内存状态说明]
FROM sys.dm_os_sys_memory