/****** Object: StoredProcedure [dbo].[SP_CPU] Script Date: 12/09/2018 19:01:24 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE Proc [dbo].[SP_CPU] as -- 单行平均耗时排行 SELECT TOP 10 total_worker_time / execution_count/1000 AS [Avg CPU Time], (SELECT Substring(text, statement_start_offset / 2, ( 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, last_execution_time,execution_count,total_worker_time,last_worker_time,min_worker_time,max_worker_time,total_elapsed_time,last_elapsed_time,min_elapsed_time,max_elapsed_time,last_logical_reads,last_logical_writes,last_physical_reads FROM sys.dm_exec_query_stats ORDER BY [Avg CPU Time] DESC GO
补充:
-- 汇总平均耗时排行
SELECT TOP 10 query_stats.query_hash AS "Query Hash", SUM(query_stats.total_worker_time) / SUM(query_stats.execution_count)/1000 AS "Avg CPU Time", MIN(query_stats.statement_text) AS "Statement Text" FROM (SELECT QS.*, 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) AS statement_text FROM sys.dm_exec_query_stats AS QS CROSS APPLY sys.dm_exec_sql_text(QS.sql_handle) as ST) as query_stats GROUP BY query_stats.query_hash ORDER BY 2 DESC; GO
-- 单行耗时排行 SELECT TOP 10 total_worker_time / execution_count/1000 AS [Avg CPU Time], 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) AS statement_text, QP.query_plan, DB_NAME(ST.dbid) AS db_name, obj.name AS obj_name, last_execution_time,execution_count,total_worker_time,last_worker_time,min_worker_time,max_worker_time,total_elapsed_time,last_elapsed_time,min_elapsed_time,max_elapsed_time,last_logical_reads,last_logical_writes,last_physical_reads FROM sys.dm_exec_query_stats AS QS CROSS APPLY sys.dm_exec_sql_text(QS.sql_handle) as ST CROSS APPLY sys.dm_exec_query_plan(QS.plan_handle) AS QP CROSS APPLY sysobjects obj WHERE ST.dbid = DB_ID() AND ST.objectid = obj.id --AND obj.name LIKE 'pr_get_%' ORDER BY [Avg CPU Time] DESC
-- 对象耗时排行 SELECT TOP 10 cc.db_name,cc.obj_name, SUM(total_worker_time) / SUM(execution_count)/1000 AS 'Avg CPU Time', MIN(statement_text) AS 'Statement Text' FROM ( SELECT 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) AS statement_text, QP.query_plan, DB_NAME(ST.dbid) AS db_name, obj.name AS obj_name, last_execution_time,execution_count,total_worker_time,last_worker_time,min_worker_time,max_worker_time,total_elapsed_time,last_elapsed_time,min_elapsed_time,max_elapsed_time,last_logical_reads,last_logical_writes,last_physical_reads FROM sys.dm_exec_query_stats AS QS CROSS APPLY sys.dm_exec_sql_text(QS.sql_handle) as ST CROSS APPLY sys.dm_exec_query_plan(QS.plan_handle) AS QP CROSS APPLY sysobjects obj WHERE ST.dbid = DB_ID() AND ST.objectid = obj.id --AND obj.name LIKE 't_bd_%' ) cc GROUP BY cc.db_name,cc.obj_name ORDER BY [Avg CPU Time] DESC
SELECT TOP 10 sql_handle,plan_handle,* FROM sys.dm_exec_query_stats -- 查SQL语句 SELECT dbid,objectid,encrypted,text,* FROM sys.dm_exec_sql_text (0x0200000096B5B034CDB8E45687480BB87E8A373F35AB3631) -- 查SQL语句对应的执行计划 SELECT dbid,objectid,encrypted,query_plan,* FROM sys.dm_exec_query_plan (0x0600140096B5B03440E140A2000000000000000000000000)