查询耗费CPU最多的SQL语句

来源:互联网 发布:追信魔盒在线制作软件 编辑:程序博客网 时间:2024/05/16 07:10

查询耗费CPU最多的SQL语句


SELECT TOP 10
   total_worker_time/execution_count AS avg_cpu_cost, plan_handle,
  
   execution_count,
  
   (SELECT SUBSTRING(text, statement_start_offset/2 + 1,
  
      (CASE WHEN statement_end_offset = -1
  
         THEN LEN(CONVERT(nvarchar(max), text)) * 2
  
         ELSE statement_end_offset
  
      END - statement_start_offset)/2)
  
   FROM sys.dm_exec_sql_text(sql_handle)) AS query_text
  
FROM sys.dm_exec_query_stats
  
ORDER BY [avg_cpu_cost] DESC


执行最耗时的前N条T-SQL语句


--得到最耗时的前N条T-SQL语句  
  
--适用于SQL SERVER 2005及其以上版本  
  
--给N赋初值为30  
declare @n int set @n=30   
  
;with maco as   
(     
    select top (@n)  
        plan_handle,  
        sum(total_worker_time) as total_worker_time ,  
        sum(execution_count) as execution_count ,  
        count(1) as sql_count  
    from sys.dm_exec_query_stats group by plan_handle  
    order by sum(total_worker_time) desc  
)  
select  t.text ,  
        a.total_worker_time ,  
        a.execution_count ,  
        a.sql_count  
from    maco a  
        cross apply sys.dm_exec_sql_text(plan_handle) t 

0 0
原创粉丝点击