获取MSSQL当前Server的CPU 与 memory使用率

工作时候遇到用户需要查询资源使用情况

没有远程桌面权限就取弄了个脚本改改,来查询了,发在这里保存一下

DECLARE @ts_now BIGINT
	SELECT @ts_now = cpu_ticks / CONVERT(FLOAT, (cpu_ticks / ms_ticks))
	FROM sys.dm_os_sys_info
	
	SELECT top (60) 
		DATEADD(ms, -1 * (@ts_now - [TIMESTAMP]), GETDATE()) AS MonitorTime, 
		SQLProcessUtilization,
		SystemIdle,
		100 - SystemIdle - SQLProcessUtilization AS OtherProcessUtilization
	FROM 
		(SELECT 
			record.value('(./Record/@id)[1]', 'int') AS record_id,
			record.value('(./Record/SchedulerMonitorEvent/SystemHealth/SystemIdle)[1]', 'int') AS SystemIdle,
			record.value('(./Record/SchedulerMonitorEvent/SystemHealth/ProcessUtilization)[1]', 'int') AS SQLProcessUtilization,
			TIMESTAMP
		FROM (
			SELECT TIMESTAMP, CONVERT(XML, record) AS record 
			FROM sys.dm_os_ring_buffers 
			WHERE ring_buffer_type = N'RING_BUFFER_SCHEDULER_MONITOR'
			AND record LIKE '%%') AS x
		) AS y 
	ORDER BY record_id DESC


---- Memory Configuration on the Server Operating System
--SELECT	physical_memory_in_bytes/1024 as [Physical Memory_MB],  
--		virtual_memory_in_bytes/1024 as [Virtual Memory MB] 
--FROM sys.dm_os_sys_info

-- Or alternatively
SELECT  [total_physical_memory_kb]/1024 as [Physical Memory_MB],
[available_physical_memory_kb]/1024 as [Available_Memory_MB]
FROM     sys.dm_os_sys_memory

-- Total Memory used by SQL Server instance from Perf Mon 
SELECT cntr_value/1024 as Mem_MB
FROM sys.dm_os_performance_counters 
WHERE counter_name = 'Total Server Memory (KB)'


你可能感兴趣的:(SQL应用脚本)