快速获取DB服务器当前 MEM CPU的资源消耗

USE DBA_Maintenance
GO
IF EXISTS (SELECT 1 FROM SYS.PROCEDURES WHERE type='p' AND NAME='SP_CheckMemAndCpu')
BEGIN
    DROP PROCEDURE SP_CheckMemAndCpu
END
GO

CREATE PROCEDURE SP_CheckMemAndCpu
AS
BEGIN
    SET NOCOUNT ON
    --check Mem
    select (1.0*total_physical_memory_kb/1024/1024)Total_Mem_G,
            (1.0*available_physical_memory_kb/1024/1024)Available_Physical_Mem_G,
            (1.0*system_cache_kb/1024/1024)System_Cache_G,
            system_high_memory_signal_state,
            system_low_memory_signal_state,
            system_memory_state_desc
         from sys.dm_os_sys_memory
    SELECT OBJECT_name,counter_name,(1.0*cntr_value/1024/1024)'数据库服务器最大使用内存' FROM sys.dm_os_performance_counters   
WHERE counter_name IN ('Target Server Memory (KB)','Total Server Memory (KB)') 
SELECT  total_physical_memory_kb /1024/1024 AS [物理内存(G)] , 
        available_physical_memory_kb /1024/1024 AS [可用物理内存(G)] , 
        system_cache_kb /1024/1024 AS [系统缓存内存总量(G)] , 
        ( kernel_paged_pool_kb + kernel_nonpaged_pool_kb ) /1024 AS [内核池内存总量(MB)] , 
        total_page_file_kb /1024/1024 AS [操作系统报告的提交限制的大小(G)] , 
        available_page_file_kb /1024/1024 AS [未使用的页文件的总量(G)] , 
        system_memory_state_desc AS [内存状态说明] 
FROM    sys .dm_os_sys_memory 


    --check CPU
    SELECT cpu_count,max_workers_count  FROM sys.dm_os_sys_info

declare @cxpacket bigint
declare @sumwaits bigint
select @cxpacket=wait_time_ms
from sys.dm_os_wait_stats
where wait_type='Cxpacket'
select @sumwaits=sum(wait_time_ms)
from sys.dm_os_wait_stats
select convert(numeric(5,4),@cxpacket/@sumwaits) AS '并行运行的Cxpacket等待状态阀值 <5%'


select convert(numeric(5,4),sum(signal_wait_time_ms)/sum(wait_time_ms)) AS '指令等待CPU资源的时间占总时间的百分比 阀值<25%'
from sys.dm_os_wait_stats

-----获取DB服务器上当前正在执行的SQL
SELECT TOP 10
  [session_id],
  [request_id],
  [start_time] AS '开始时间',
  [status] AS '状态',
  [command] AS '命令',
  dest.[text] AS 'sql语句', 
  DB_NAME([database_id]) AS '数据库名',
  [blocking_session_id] AS '正在阻塞其他会话的会话ID',
  der.[wait_type] AS '等待资源类型',
  [wait_time] AS '等待时间',
  [wait_resource] AS '等待的资源',
  [dows].[waiting_tasks_count] AS '当前正在进行等待的任务数',
  [reads] AS '物理读次数',
  [writes] AS '写次数',
  [logical_reads] AS '逻辑读次数',
  [row_count] AS '返回结果行数'
  FROM sys.[dm_exec_requests] AS der 
  INNER JOIN [sys].[dm_os_wait_stats] AS dows 
  ON der.[wait_type]=[dows].[wait_type]
  CROSS APPLY 
  sys.[dm_exec_sql_text](der.[sql_handle]) AS dest 
  WHERE [session_id]>50  
  ORDER BY [cpu_time] DESC

 ----获取DB服务器上CPU被SqlServer使用情况
 DECLARE @ts_now bigint = (SELECT cpu_ticks/(cpu_ticks/ms_ticks)FROM sys.dm_os_sys_info); 
 
SELECT TOP(30) SQLProcessUtilization AS [SQL Server Process CPU Utilization], 
               SystemIdle AS [System Idle Process], 
               100 - SystemIdle - SQLProcessUtilization AS [Other Process CPU Utilization], 
               DATEADD(ms, -1 * (@ts_now - [timestamp]), GETDATE()) AS [Event Time] 
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 N'%<SystemHealth>%') AS x 
      ) AS y 
ORDER BY record_id DESC;

    SET NOCOUNT OFF
    
END
GO
GRANT EXEC ON SP_CheckMemAndCpu TO PUBLIC
GO

实现的效果:

 

posted @ 2016-01-27 17:32  Captains-Felix  阅读(664)  评论(0编辑  收藏  举报