一.概念

 
 SOS_SCHEDULER_YIELD等待类型是三个任务自愿遗弃当前的财富占用,让给其余职责使用。 
 那个等待类型与CPU有一向关联,与内部存款和储蓄器与也是有直接关联,与CPU有关联是因为在sql
server里是由此职责调解SCHEDULE安德拉来波及CPU。
通过SCHEDULERAV4下的Worker线程来拍卖SQL职务。为啥跟内有着关系呢,是因为获取的能源须求内部存款和储蓄器来承载。 
  Yelding的产生:是指SCHEDULE牧马人上运维的Worker都以非抢占式的, 在
SCHEDULE本田CR-V上Worker由于财富等待,让出当前Worker给其他Worker就叫Yielding。
关于SCHEDULEEscort_YIELD发生的法规查看  sqlserver
职责调解与CPU。SOS_SCHEDULER_YIELD 等待的图景能够理解到:

  (1)CPU有压力

  (2) SQL Server CPU scheduler 使用方便管理就能够功效高。

1.1 从实例等级来查看等待数

select wait_type,
waiting_tasks_count,
wait_time_ms ,
max_wait_time_ms,
signal_wait_time_ms
from sys.dm_os_wait_stats
where wait_type like 'SOS_SCHEDULER_YIELD%' 
order by wait_type

  查询如下图所示: 

402com永利平台登录 1

  这么些等待类型排行第二,从呼吁的次数来讲有693670伍16回,也正是说该线程用完了4ms的年华片,主动放弃cpu。若无大气的runnable队列可能多量的signal
wait,注脚不断定是cpu难点。因为那多少个指标是cpu压力的三个体现
。要求检查实践安排中是还是不是存在多量围观操作。

1.2 通过dmv scheaduler的描述查看cpu压力

SELECT scheduler_id, current_tasks_count, runnable_tasks_count, work_queue_count, pending_disk_io_count
FROM sys.dm_os_schedulers
WHERE scheduler_id < 255

  如下图所示:

402com永利平台登录 2

  尽管您放在心上到runnable_tasks_count计数有两位数,持续十分长日子(一段时间内),你就能够精通CPU压力。两位数字常常被认为是一件坏事
无法应对现阶段负荷。其它能够透过质量监视器%Processor Time
来查看CPU的现象。

1.3 通过案例实时查看sql语句级的财富等待

SELECT * FROM sys.dm_exec_requests  WHERE wait_type LIKE 'SOS_SCHEDULER_YIELD%'

  – 或查究财富等待的
  SELECT session_id ,status ,blocking_session_id
  ,wait_type ,wait_time ,wait_resource
  ,transaction_id
  FROM sys.dm_exec_requests
  WHERE status = N’suspended’;

  如下图所示
运维sys.dm_exec_requests 表,由于字段多截取了三断。会话202的sql
语句上一回等待类型是SOS_SCHEDULER_YIELD。之所以会产出YIELD,是因为SCHEDULE奥迪Q7下的Worker已经发起了task
命令,但鉴于财富等待
如锁恐怕磁盘输入/输出等,Worker又是非抢占式,所以让出了当前的Worker。

402com永利平台登录 3

402com永利平台登录 4

402com永利平台登录 5

1.4 减少sos_scheduler_yield 等待

  正如上边所商讨的,这种等待类型与CPU压力有关。扩展更多CPU是回顾的消除方案,然则完结那么些建设方案并不便于。当以此等待类型极高时,你能够设想其余的事务。这里经过从缓存中找到与CPU相关的最值钱的SQL语句。

–查询编写翻译以来 cpu耗费时间总数最多的前50条(Total_woker_time) 第一种查询
select
‘total_worker_time(ms)’=(total_worker_time/1000),
q.[text], –DB_NAME(dbid),OBJECT_NAME(objectid),
execution_count,
‘max_worker_time(ms)’=(max_worker_time/1000),
‘last_worker_time(ms)’=(last_worker_time/1000),
‘min_worker_time(ms)’=(min_worker_time/1000),
‘max_elapsed_time(ms)’=(max_elapsed_time/1000),
‘min_elapsed_time(ms)’=(min_elapsed_time/1000),
‘last_elapsed_time(ms)’=(last_elapsed_time/1000),
total_physical_reads,
last_physical_reads,
min_physical_reads,
max_physical_reads,
total_logical_reads,
last_logical_reads,
max_logical_reads,
creation_time,
last_execution_time
from
(select top 50 qs.* from sys.dm_exec_query_stats qs order by
qs.total_worker_time desc)
as highest_cpu_queries cross apply
sys.dm_exec_sql_text(highest_cpu_queries.plan_handle) as q
order by highest_cpu_queries.total_worker_time DESC

 

试应用环境:SQL2008 R2、SQL2012、SQL2014

[sql] view plaincopy
--语句1:获取前20逻辑读取次数或逻辑写入次数或CPU 时间  
SELECT TOP 20 SUBSTRING(qt.TEXT, (qs.statement_start_offset/2)+1,  
((CASE qs.statement_end_offset  
WHEN -1 THEN DATALENGTH(qt.TEXT)  
ELSE qs.statement_end_offset  
END - qs.statement_start_offset)/2)+1),  
qs.execution_count,  
qs.total_logical_reads, qs.last_logical_reads,  
qs.total_logical_writes, qs.last_logical_writes,  
qs.total_worker_time,  
qs.last_worker_time,  
qs.total_elapsed_time/1000000 total_elapsed_time_in_S,  
qs.last_elapsed_time/1000000 last_elapsed_time_in_S,  
qs.last_execution_time,  
qp.query_plan  
FROM sys.dm_exec_query_stats qs  
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) qt  
CROSS APPLY sys.dm_exec_query_plan(qs.plan_handle) qp  
ORDER BY qs.total_logical_reads DESC -- 逻辑读取次数  
 --ORDER BY qs.total_logical_writes DESC -- 逻辑写入次数  
 --ORDER BY qs.total_worker_time DESC -- CPU 时间  


--语句2:获取前20执行的 SP 命令的总工作时间 (CPU 压力)  
    SELECT TOP 20 qt.text AS 'SP Name', qs.total_worker_time AS 'TotalWorkerTime',   
    qs.total_worker_time/qs.execution_count AS 'AvgWorkerTime',  
    qs.execution_count AS 'Execution Count',   
    ISNULL(qs.execution_count/DATEDIFF(Second, qs.creation_time, GetDate()), 0) AS 'Calls/Second',  
    ISNULL(qs.total_elapsed_time/qs.execution_count, 0) AS 'AvgElapsedTime',   
    qs.max_logical_reads, qs.max_logical_writes,   
    DATEDIFF(Minute, qs.creation_time, GetDate()) AS 'Age in Cache'  
    FROM sys.dm_exec_query_stats AS qs  
    CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS qt  
    WHERE qt.dbid = db_id() -- 当前数据库  
    ORDER BY qs.total_worker_time DESC  

--语句3: 获取前20 执行的 SP 命令逻辑写入/分钟  
    SELECT TOP 20 qt.text AS 'SP Name', qs.total_logical_writes, qs.total_logical_writes/qs.execution_count AS 'AvgLogicalWrites',  
    qs.total_logical_writes/DATEDIFF(Minute, qs.creation_time, GetDate()) AS 'Logical Writes/Min',    
    qs.execution_count AS 'Execution Count',   
    qs.execution_count/DATEDIFF(Second, qs.creation_time, GetDate()) AS 'Calls/Second',   
    qs.total_worker_time/qs.execution_count AS 'AvgWorkerTime',  
    qs.total_worker_time AS 'TotalWorkerTime',  
    qs.total_elapsed_time/qs.execution_count AS 'AvgElapsedTime',  
    qs.max_logical_reads, qs.max_logical_writes, qs.total_physical_reads,   
    DATEDIFF(Minute, qs.creation_time, GetDate()) AS 'Age in Cache',  
    qs.total_physical_reads/qs.execution_count AS 'Avg Physical Reads', qt.dbid  
    FROM sys.dm_exec_query_stats AS qs  
    CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS qt  
    WHERE qt.dbid = db_id() -- 当前数据库  
    ORDER BY qs.total_logical_writes DESC  

--语句4: 获取前20执行的 SP 命令的逻辑读取(内存压力)   
    SELECT TOP 20 qt.text AS 'SP Name', total_logical_reads,   
    qs.execution_count AS 'Execution Count', total_logical_reads/qs.execution_count AS 'AvgLogicalReads',  
    qs.execution_count/DATEDIFF(Second, qs.creation_time, GetDate()) AS 'Calls/Second',   
    qs.total_worker_time/qs.execution_count AS 'AvgWorkerTime',  
    qs.total_worker_time AS 'TotalWorkerTime',  
    qs.total_elapsed_time/qs.execution_count AS 'AvgElapsedTime',  
    qs.total_logical_writes,  
    qs.max_logical_reads, qs.max_logical_writes, qs.total_physical_reads,   
    DATEDIFF(Minute, qs.creation_time, GetDate()) AS 'Age in Cache', qt.dbid   
    FROM sys.dm_exec_query_stats AS qs  
    CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS qt  
    WHERE qt.dbid = db_id() -- 当前数据库  
    ORDER BY total_logical_reads DESC  

--语句5: 获取前20执行的 SP 命令由物理读取 (读取 I/O 压力)  
    SELECT TOP 20 qt.text AS 'SP Name', qs.total_physical_reads, qs.total_physical_reads/qs.execution_count AS 'Avg Physical Reads',  
    qs.execution_count AS 'Execution Count',  
    qs.execution_count/DATEDIFF(Second, qs.creation_time, GetDate()) AS 'Calls/Second',    
    qs.total_worker_time/qs.execution_count AS 'AvgWorkerTime',  
    qs.total_worker_time AS 'TotalWorkerTime',  
    qs.total_elapsed_time/qs.execution_count AS 'AvgElapsedTime',  
    qs.max_logical_reads, qs.max_logical_writes,    
    DATEDIFF(Minute, qs.creation_time, GetDate()) AS 'Age in Cache', qt.dbid   
    FROM sys.dm_exec_query_stats AS qs  
    CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS qt  
    WHERE qt.dbid = db_id() -- 当前数据库  
    ORDER BY qs.total_physical_reads DESC  

--语句6: 获取前20执行的 SP 命令执行计数  
    SELECT TOP 20 qt.text AS 'SP Name', qs.execution_count AS 'Execution Count',    
    qs.execution_count/DATEDIFF(Second, qs.creation_time, GetDate()) AS 'Calls/Second',  
    qs.total_worker_time/qs.execution_count AS 'AvgWorkerTime',  
    qs.total_worker_time AS 'TotalWorkerTime',  
    qs.total_elapsed_time/qs.execution_count AS 'AvgElapsedTime',  
    qs.max_logical_reads, qs.max_logical_writes, qs.total_physical_reads,   
    DATEDIFF(Minute, qs.creation_time, GetDate()) AS 'Age in Cache'  
    FROM sys.dm_exec_query_stats AS qs  
    CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS qt  
    WHERE qt.dbid = db_id() -- Filter by current database  
    ORDER BY qs.execution_count DESC  

查看10秒时间内存储过程执行次数和CPU时间
[sql] view plaincopy在CODE上查看代码片派生到我的代码片
SELECT DB_NAME(st.dbid) DBName  
      ,OBJECT_SCHEMA_NAME(st.objectid,dbid) SchemaName  
      ,OBJECT_NAME(st.objectid,dbid) StoredProcedure  
      ,max(cp.usecounts) Execution_count  
      ,sum(qs.total_worker_time) total_cpu_time  
      ,sum(qs.total_worker_time) / (max(cp.usecounts) * 1.0)  avg_cpu_time  
 into #temp  
 FROM sys.dm_exec_cached_plans cp join sys.dm_exec_query_stats qs on cp.plan_handle = qs.plan_handle  
      CROSS APPLY sys.dm_exec_sql_text(cp.plan_handle) st  
 where DB_NAME(st.dbid) is not null and cp.objtype = 'proc'  
 group by DB_NAME(st.dbid),OBJECT_SCHEMA_NAME(objectid,st.dbid), OBJECT_NAME(objectid,st.dbid)   
 order by sum(qs.total_worker_time) desc  

WAITFOR DELAY '00:00:10'   

SELECT DB_NAME(st.dbid) DBName  
      ,OBJECT_SCHEMA_NAME(st.objectid,dbid) SchemaName  
      ,OBJECT_NAME(st.objectid,dbid) StoredProcedure  
      ,max(cp.usecounts) Execution_count  
      ,sum(qs.total_worker_time) total_cpu_time  
      ,sum(qs.total_worker_time) / (max(cp.usecounts) * 1.0)  avg_cpu_time  
 into #temp2  
 FROM sys.dm_exec_cached_plans cp join sys.dm_exec_query_stats qs on cp.plan_handle = qs.plan_handle  
      CROSS APPLY sys.dm_exec_sql_text(cp.plan_handle) st  
 where DB_NAME(st.dbid) is not null and cp.objtype = 'proc'  
 group by DB_NAME(st.dbid),OBJECT_SCHEMA_NAME(objectid,st.dbid), OBJECT_NAME(objectid,st.dbid)   
 order by sum(qs.total_worker_time) desc  

SELECT a.DBNAMe, a.SchemaName, a.StoredProcedure,  
b.Execution_count - a.Execution_count as ExecCnt,  
b.total_cpu_time - a.total_cpu_time as CPU   
FROM #temp a inner join #temp2 b on a.DBName = b.DBname and a.SchemaName = b.SchemaName and a.StoredProcedure = b.StoredProcedure  
ORDER BY 5 desc  

drop table #temp  
drop table #temp2   

 

SELECT  creation_time  N’语句编写翻译时间’
        ,last_execution_time  N’上次推行时间’
        ,total_physical_reads N’物理读取总次数’
        ,total_logical_reads/execution_count N’每一遍逻辑读次数’
        ,total_logical_reads  N’逻辑读取总次数’
        ,total_logical_writes N’逻辑写入总次数’
        , execution_count  N’施行次数’
        , total_worker_time/1000 N’所用的CPU总时间ms’
        , total_elapsed_time/一千  N’总开支时间ms’
        , (total_elapsed_time / execution_count)/1000 
N’平均时间ms’
        ,SUBSTRING(st.text, (qs.statement_start_offset/2) + 1,
         ((CASE statement_end_offset
          WHEN -1 THEN DATALENGTH(st.text)
          ELSE qs.statement_end_offset END
            – qs.statement_start_offset)/2) + 1) N’执行语句’
FROM dm_exec_query_stats AS qs
CROSS APPLY dm_exec_sql_text(qs.sql_handle) st
where SUBSTRING(st.text, (qs.statement_start_offset/2) + 1,
         ((CASE statement_end_offset
          WHEN -1 THEN DATALENGTH(st.text)
          ELSE qs.statement_end_offset END
            – qs.statement_start_offset)/2) + 1) not like ‘%fetch%’
ORDER BY  total_elapsed_time / execution_count DESC;

  

Microsoft SQL Server 贰零零柒提供了有的工具来监督数据库。方法之一是动态管理视图。动态管理视图
(DMV) 和动态管理函数 (DMF)
重临的服务器状态消息可用来监控服务器实例的运营意况、会诊难题和优化质量。

 

试应用遭遇:SQL贰零壹零 Wrangler2、SQL二〇一一、SQL2016

[sql] view
plaincopy

  1. –语句1:获取前20逻辑读取次数或逻辑写入次数或CPU 时间  
  2. SELECT TOP 20 SUBSTRING(qt.TEXT, (qs.statement_start_offset/2)+1,  
  3. ((CASE qs.statement_end_offset  
  4. WHEN -1 THEN DATALENGTH(qt.TEXT)  
  5. ELSE qs.statement_end_offset  
  6. END – qs.statement_start_offset)/2)+1),  
  7. qs.execution_count,  
  8. qs.total_logical_reads, qs.last_logical_reads,  
  9. qs.total_logical_writes, qs.last_logical_writes,  
  10. qs.total_worker_time,  
  11. qs.last_worker_time,  
  12. qs.total_elapsed_time/1000000 total_elapsed_time_in_S,  
  13. qs.last_elapsed_time/1000000 last_elapsed_time_in_S,  
  14. qs.last_execution_time,  
  15. qp.query_plan  
  16. FROM sys.dm_exec_query_stats qs  
  17. CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) qt  
  18. CROSS APPLY sys.dm_exec_query_plan(qs.plan_handle) qp  
  19. ORDER BY qs.total_logical_reads DESC — 逻辑读取次数  
  20.  –ORDER BY qs.total_logical_writes DESC — 逻辑写入次数  
  21.  –ORDER BY qs.total_worker_time DESC — CPU 时间  
  22.   
  23.   
  24. –语句2:获取前20实践的 SP 命令的总工程师时 (CPU 压力)  
  25.     SELECT TOP 20 qt.text AS ‘SP Name’, qs.total_worker_time AS ‘TotalWorkerTime’,   
  26.     qs.total_worker_time/qs.execution_count AS ‘AvgWorkerTime’,  
  27.     qs.execution_count AS ‘Execution Count’,   
  28.     ISNULL(qs.execution_count/DATEDIFF(Second, qs.creation_time, GetDate()), 0) AS ‘Calls/Second’,  
  29.     ISNULL(qs.total_elapsed_time/qs.execution_count, 0) AS ‘AvgElapsedTime’,   
  30.     qs.max_logical_reads, qs.max_logical_writes,   
  31.     DATEDIFF(Minute, qs.creation_time, GetDate()) AS ‘Age in Cache’  
  32.     FROM sys.dm_exec_query_stats AS qs  
  33.     CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS qt  
  34.     WHERE qt.dbid = db_id() — 当前数据库  
  35.     ORDER BY qs.total_worker_time DESC  
  36.   
  37. –语句3: 获取前20 实行的 SP 命令逻辑写入/分钟  
  38.     SELECT TOP 20 qt.text AS ‘SP Name’, qs.total_logical_writes, qs.total_logical_writes/qs.execution_count AS ‘AvgLogicalWrites’,  
  39.     qs.total_logical_writes/DATEDIFF(Minute, qs.creation_time, GetDate()) AS ‘Logical Writes/Min’,    
  40.     qs.execution_count AS ‘Execution Count’,   
  41.     qs.execution_count/DATEDIFF(Second, qs.creation_time, GetDate()) AS ‘Calls/Second’,   
  42.     qs.total_worker_time/qs.execution_count AS ‘AvgWorkerTime’,  
  43.     qs.total_worker_time AS ‘TotalWorkerTime’,  
  44.     qs.total_elapsed_time/qs.execution_count AS ‘AvgElapsedTime’,  
  45.     qs.max_logical_reads, qs.max_logical_writes, qs.total_physical_reads,   
  46.     DATEDIFF(Minute, qs.creation_time, GetDate()) AS ‘Age in Cache’,  
  47.     qs.total_physical_reads/qs.execution_count AS ‘Avg Physical Reads’, qt.dbid  
  48.     FROM sys.dm_exec_query_stats AS qs  
  49.     CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS qt  
  50.     WHERE qt.dbid = db_id() — 当前数据库  
  51. 永利皇宫开户,    ORDER BY qs.total_logical_writes DESC  
  52.   
  53. –语句4: 获取前20实践的 SP 命令的逻辑读取(内部存款和储蓄器压力)   
  54.     SELECT TOP 20 qt.text AS ‘SP Name’, total_logical_reads,   
  55.     qs.execution_count AS ‘Execution Count’, total_logical_reads/qs.execution_count AS ‘AvgLogicalReads’,  
  56.     qs.execution_count/DATEDIFF(Second, qs.creation_time, GetDate()) AS ‘Calls/Second’,   
  57.     qs.total_worker_time/qs.execution_count AS ‘AvgWorkerTime’,  
  58.     qs.total_worker_time AS ‘TotalWorkerTime’,  
  59.     qs.total_elapsed_time/qs.execution_count AS ‘AvgElapsedTime’,  
  60.     qs.total_logical_writes,  
  61.     qs.max_logical_402com永利平台登录,reads, qs.max_logical_writes, qs.total_physical_reads,   
  62.     DATEDIFF(Minute, qs.creation_time, GetDate()) AS ‘Age in Cache’, qt.dbid   
  63.     FROM sys.dm_exec_query_stats AS qs  
  64.     CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS qt  
  65.     WHERE qt.dbid = db_id() — 当前数据库  
  66.     ORDER BY total_logical_reads DESC  
  67.   
  68. –语句5: 获取前20实行的 SP 命令由物理读取 (读取 I/O 压力)  
  69.     SELECT TOP 20 qt.text AS ‘SP Name’, qs.total_physical_reads, qs.total_physical_reads/qs.execution_count AS ‘Avg Physical Reads’,  
  70.     qs.execution_count AS ‘Execution Count’,  
  71.     qs.execution_count/DATEDIFF(Second, qs.creation_time, GetDate()) AS ‘Calls/Second’,    
  72.     qs.total_worker_time/qs.execution_count AS ‘AvgWorkerTime’,  
  73.     qs.total_worker_time AS ‘TotalWorkerTime’,  
  74.     qs.total_elapsed_time/qs.execution_count AS ‘AvgElapsedTime’,  
  75.     qs.max_logical_reads, qs.max_logical_writes,    
  76.     DATEDIFF(Minute, qs.creation_time, GetDate()) AS ‘Age in Cache’, qt.dbid   
  77.     FROM sys.dm_exec_query_stats AS qs  
  78.     CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS qt  
  79.     WHERE qt.dbid = db_id() — 当前数据库  
  80.     ORDER BY qs.total_physical_reads DESC  
  81.   
  82. –语句6: 获取前20推行的 SP 命令实行计数  
  83.     SELECT TOP 20 qt.text AS ‘SP Name’, qs.execution_count AS ‘Execution Count’,    
  84.     qs.execution_count/DATEDIFF(Second, qs.creation_time, GetDate()) AS ‘Calls/Second’,  
  85.     qs.total_worker_time/qs.execution_count AS ‘AvgWorkerTime’,  
  86.     qs.total_worker_time AS ‘TotalWorkerTime’,  
  87.     qs.total_elapsed_time/qs.execution_count AS ‘AvgElapsedTime’,  
  88.     qs.max_logical_reads, qs.max_logical_writes, qs.total_physical_reads,   
  89.     DATEDIFF(Minute, qs.creation_time, GetDate()) AS ‘Age in Cache’  
  90.     FROM sys.dm_exec_query_stats AS qs  
  91.     CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS qt  
  92.     WHERE qt.dbid = db_id() — Filter by current database  
  93.     ORDER BY qs.execution_count DESC  

 

查看10秒时间内积存进程进行次数和CPU时间

[sql] view
plaincopy402com永利平台登录 6402com永利平台登录 7

  1. SELECT DB_NAME(st.dbid) DBName  
  2.       ,OBJECT_SCHEMA_NAME(st.objectid,dbid) SchemaName  
  3.       ,OBJECT_NAME(st.objectid,dbid) StoredProcedure  
  4.       ,max(cp.usecounts) Execution_count  
  5.       ,sum(qs.total_worker_time) total_cpu_time  
  6.       ,sum(qs.total_worker_time) / (max(cp.usecounts) * 1.0)  avg_cpu_time  
  7.  into #temp  
  8.  FROM sys.dm_exec_cached_plans cp join sys.dm_exec_query_stats qs on cp.plan_handle = qs.plan_handle  
  9.       CROSS APPLY sys.dm_exec_sql_text(cp.plan_handle) st  
  10.  where DB_NAME(st.dbid) is not null and cp.objtype = ‘proc’  
  11.  group by DB_NAME(st.dbid),OBJECT_SCHEMA_NAME(objectid,st.dbid), OBJECT_NAME(objectid,st.dbid)   
  12.  order by sum(qs.total_worker_time) desc  
  13.   
  14. WAITFOR DELAY ’00:00:10′   
  15.   
  16. SELECT DB_NAME(st.dbid) DBName  
  17.       ,OBJECT_SCHEMA_NAME(st.objectid,dbid) SchemaName  
  18.       ,OBJECT_NAME(st.objectid,dbid) StoredProcedure  
  19.       ,max(cp.usecounts) Execution_count  
  20.       ,sum(qs.total_worker_time) total_cpu_time  
  21.       ,sum(qs.total_worker_time) / (max(cp.usecounts) * 1.0)  avg_cpu_time  
  22.  into #temp2  
  23.  FROM sys.dm_exec_cached_plans cp join sys.dm_exec_query_stats qs on cp.plan_handle = qs.plan_handle  
  24.       CROSS APPLY sys.dm_exec_sql_text(cp.plan_handle) st  
  25.  where DB_NAME(st.dbid) is not null and cp.objtype = ‘proc’  
  26.  group by DB_NAME(st.dbid),OBJECT_SCHEMA_NAME(objectid,st.dbid), OBJECT_NAME(objectid,st.dbid)   
  27.  order by sum(qs.total_worker_time) desc  
  28.   
  29. SELECT a.DBNAMe, a.SchemaName, a.StoredProcedure,  
  30. b.Execution_count – a.Execution_count as ExecCnt,  
  31. b.total_cpu_time – a.total_cpu_time as CPU   
  32. FROM #temp a inner join #temp2 b on a.DBName = b.DBname and a.SchemaName = b.SchemaName and a.StoredProcedure = b.StoredProcedure  
  33. ORDER BY 5 desc  
  34.   
  35. drop table #temp  
  36. drop table #temp2   

正规服务器动态管理对象满含:

 

  • dm_db_*:数据库和数据库对象

  • dm_exec_*:推行顾客代码和关联的连日

  • dm_os_*:内部存款和储蓄器、锁定和岁月安顿

  • dm_tran_*:事务和隔绝

  • dm_io_*:互联网和磁盘的输入/输出

 

此部分介绍为监察 SQL Server
运营景况而针对性这一个动态管理视图和函数运维的部分常用查询。