This is helpful
-- https://www.mssqltips.com/sqlservertip/7696/sql-server-oracle-cached-query-statistics-execution-plans/?utm_content=head
SELECT
DB_NAME(ISNULL([t].[dbid],
(SELECT CAST([value] AS SMALLINT) FROM [sys].[dm_exec_plan_attributes]([st].[plan_handle]) WHERE [attribute] = 'dbid'))) [DatabaseName],
ISNULL(OBJECT_NAME([t].[objectid], [t].[dbid]),'{AdHocQuery}') [Proc/Func],
MIN(SUBSTRING([t].[text], ([st].[statement_start_offset]/2)+1, ((CASE [st].[statement_end_offset] WHEN -1 THEN DATALENGTH([t].[text]) ELSE [st].[statement_end_offset] END - [st].[statement_start_offset])/2)+1)) [Text],
MAX([st].[max_rows]) [Rows],
SUM([st].[execution_count]) [Runs],
SUM([st].[execution_count])/(SELECT MAX(v) FROM (VALUES (DATEDIFF(ss,MIN([st].[creation_time]),GETDATE())), (1)) AS VALUE(v)) [Calls/Sec],
SUM([st].[max_elapsed_time])/1000000 [TimeSec],
SUM([st].[max_worker_time])/1000000 [CpuTimeSec],
SUM([st].[max_logical_reads]+[st].[max_logical_writes])*8/1024/1024 [IOinGB],
MAX([st].[max_dop]) [DOP],
SUM([st].[max_reserved_threads])-SUM([st].[max_used_threads]) [ThreadsExceeded],
(SUM([st].[max_grant_kb])-SUM([st].[max_used_grant_kb]))/1024 [MemoryExceededMb],
'SELECT [query_plan] FROM [sys].[dm_exec_query_plan](0x'+CONVERT(VARCHAR(MAX),[st].[plan_handle],2)+')' [ViewPlan]
FROM [sys].[dm_exec_query_stats] [st]
CROSS APPLY [sys].[dm_exec_sql_text]([st].[sql_handle]) [t]
GROUP BY [st].[query_hash], [st].[plan_handle], [t].[dbid], [t].[objectid]
ORDER BY (SELECT MAX(v) FROM (VALUES
(SUM([st].[execution_count])/(SELECT MAX(v) FROM (VALUES (DATEDIFF(ss,MIN([st].[creation_time]),GETDATE())), (1)) AS VALUE(v))),
(SUM([st].[max_elapsed_time])/1000000),
(SUM([st].[max_worker_time])/1000000),
(SUM([st].[max_logical_reads]+[st].[max_logical_writes])*8/1024/1024),
(MAX([st].[max_dop])),
(SUM([st].[max_reserved_threads])-SUM([st].[max_used_threads])),
((SUM([st].[max_grant_kb])-SUM([st].[max_used_grant_kb]))/1024)) AS VALUE(v)) DESC;
Comments
Post a Comment