11
22-- SQL Server 2012 Diagnostic Information Queries
33-- Glenn Berry
4- -- Last Modified: June 5, 2018
4+ -- Last Modified: July 5, 2018
55-- https://www.sqlskills.com/blogs/glenn/
66-- http://sqlserverperformance.wordpress.com/
77-- Twitter: GlennAlanBerry
@@ -1047,7 +1047,9 @@ qs.max_elapsed_time AS [Max Elapsed Time],
10471047qs .min_logical_reads AS [Min Logical Reads],
10481048qs .total_logical_reads / qs .execution_count AS [Avg Logical Reads],
10491049qs .max_logical_reads AS [Max Logical Reads],
1050- qs .execution_count AS [Execution Count], qs .creation_time AS [Creation Time]
1050+ qs .execution_count AS [Execution Count],
1051+ CASE WHEN CONVERT (nvarchar (max ), qp .query_plan ) LIKE N ' %<MissingIndexes>%' THEN 1 ELSE 0 END AS [Has Missing Index],
1052+ qs .creation_time AS [Creation Time]
10511053-- ,t.[text] AS [Query Text], qp.query_plan AS [Query Plan] -- uncomment out these columns if not copying results to Excel
10521054FROM sys .dm_exec_query_stats AS qs WITH (NOLOCK )
10531055CROSS APPLY sys .dm_exec_sql_text (plan_handle) AS t
@@ -1141,7 +1143,9 @@ qs.max_worker_time AS [Max Worker Time],
11411143qs .min_elapsed_time AS [Min Elapsed Time],
11421144qs .total_elapsed_time / qs .execution_count AS [Avg Elapsed Time],
11431145qs .max_elapsed_time AS [Max Elapsed Time],
1144- qs .execution_count AS [Execution Count], qs .creation_time AS [Creation Time]
1146+ qs .execution_count AS [Execution Count],
1147+ CASE WHEN CONVERT (nvarchar (max ), qp .query_plan ) LIKE N ' %<MissingIndexes>%' THEN 1 ELSE 0 END AS [Has Missing Index],
1148+ qs .creation_time AS [Creation Time]
11451149-- ,t.[text] AS [Complete Query Text], qp.query_plan AS [Query Plan] -- uncomment out these columns if not copying results to Excel
11461150FROM sys .dm_exec_query_stats AS qs WITH (NOLOCK )
11471151CROSS APPLY sys .dm_exec_sql_text (plan_handle) AS t
@@ -1163,8 +1167,9 @@ qs.execution_count AS [Execution Count],
11631167qs .total_logical_reads / qs .execution_count AS [Avg Logical Reads],
11641168qs .total_physical_reads / qs .execution_count AS [Avg Physical Reads],
11651169qs .total_worker_time / qs .execution_count AS [Avg Worker Time],
1170+ CASE WHEN CONVERT (nvarchar (max ), qp .query_plan ) LIKE N ' %<MissingIndexes>%' THEN 1 ELSE 0 END AS [Has Missing Index],
11661171qs .creation_time AS [Creation Time]
1167- , qp .query_plan AS [Query Plan] -- comment out this column if copying results to Excel
1172+ -- ,t.[text] AS [Complete Query Text], qp.query_plan AS [Query Plan] -- uncomment out these columns if not copying results to Excel
11681173FROM sys .dm_exec_query_stats AS qs WITH (NOLOCK )
11691174CROSS APPLY sys .dm_exec_sql_text (plan_handle) AS t
11701175CROSS APPLY sys .dm_exec_query_plan (plan_handle) AS qp
@@ -1229,7 +1234,8 @@ qs.total_logical_reads/qs.execution_count AS [Avg Logical Reads],
12291234qs .total_worker_time AS [Total Worker Time],
12301235qs .total_worker_time / qs .execution_count AS [Avg Worker Time],
12311236qs .total_elapsed_time AS [Total Elapsed Time],
1232- qs .total_elapsed_time / qs .execution_count AS [Avg Elapsed Time],
1237+ qs .total_elapsed_time / qs .execution_count AS [Avg Elapsed Time],
1238+ CASE WHEN CONVERT (nvarchar (max ), qp .query_plan ) LIKE N ' %<MissingIndexes>%' THEN 1 ELSE 0 END AS [Has Missing Index],
12331239qs .creation_time AS [Creation Time]
12341240-- ,t.[text] AS [Complete Query Text], qp.query_plan AS [Query Plan] -- uncomment out these columns if not copying results to Excel
12351241FROM sys .dm_exec_query_stats AS qs WITH (NOLOCK )
@@ -1240,16 +1246,21 @@ ORDER BY qs.execution_count DESC OPTION (RECOMPILE);
12401246-- ----
12411247
12421248
1243- -- Queries 47 through 52 are the "Bad Man List" for stored procedures
1249+ -- Queries 50 through 55 are the "Bad Man List" for stored procedures
12441250-- Top Cached SPs By Execution Count (Query 50) (SP Execution Counts)
1245- SELECT TOP (100 ) p .name AS [SP Name], qs .execution_count ,
1251+ SELECT TOP (100 ) p .name AS [SP Name], qs .execution_count AS [Execution Count] ,
12461252ISNULL (qs .execution_count / DATEDIFF (Minute, qs .cached_time , GETDATE ()), 0 ) AS [Calls/Minute],
1247- qs .total_worker_time / qs .execution_count AS [AvgWorkerTime], qs .total_worker_time AS [TotalWorkerTime],
1248- qs .total_elapsed_time , qs .total_elapsed_time / qs .execution_count AS [avg_elapsed_time],
1249- qs .cached_time
1253+ qs .total_elapsed_time / qs .execution_count AS [Avg Elapsed Time],
1254+ qs .total_worker_time / qs .execution_count AS [Avg Worker Time],
1255+ qs .total_logical_reads / qs .execution_count AS [Avg Logical Reads],
1256+ CASE WHEN CONVERT (nvarchar (max ), qp .query_plan ) LIKE N ' %<MissingIndexes>%' THEN 1 ELSE 0 END AS [Has Missing Index],
1257+ FORMAT (qs .last_execution_time , ' yyyy-MM-dd HH:mm:ss' , ' en-US' ) AS [Last Execution Time],
1258+ FORMAT (qs .cached_time , ' yyyy-MM-dd HH:mm:ss' , ' en-US' ) AS [Plan Cached Time]
1259+ -- ,qp.query_plan AS [Query Plan] -- Uncomment if you want the Query Plan
12501260FROM sys .procedures AS p WITH (NOLOCK )
12511261INNER JOIN sys .dm_exec_procedure_stats AS qs WITH (NOLOCK )
12521262ON p.[object_id] = qs.[object_id]
1263+ CROSS APPLY sys .dm_exec_query_plan (qs .plan_handle ) AS qp
12531264WHERE qs .database_id = DB_ID ()
12541265AND DATEDIFF (Minute, qs .cached_time , GETDATE ()) > 0
12551266ORDER BY qs .execution_count DESC OPTION (RECOMPILE );
@@ -1264,10 +1275,14 @@ SELECT TOP(25) p.name AS [SP Name], qs.min_elapsed_time, qs.total_elapsed_time/q
12641275qs .max_elapsed_time , qs .last_elapsed_time , qs .total_elapsed_time , qs .execution_count ,
12651276ISNULL (qs .execution_count / DATEDIFF (Minute, qs .cached_time , GETDATE ()), 0 ) AS [Calls/Minute],
12661277qs .total_worker_time / qs .execution_count AS [AvgWorkerTime],
1267- qs .total_worker_time AS [TotalWorkerTime], qs .cached_time
1278+ qs .total_worker_time AS [TotalWorkerTime],
1279+ FORMAT (qs .last_execution_time , ' yyyy-MM-dd HH:mm:ss' , ' en-US' ) AS [Last Execution Time],
1280+ FORMAT (qs .cached_time , ' yyyy-MM-dd HH:mm:ss' , ' en-US' ) AS [Plan Cached Time]
1281+ -- ,qp.query_plan AS [Query Plan] -- Uncomment if you want the Query Plan
12681282FROM sys .procedures AS p WITH (NOLOCK )
12691283INNER JOIN sys .dm_exec_procedure_stats AS qs WITH (NOLOCK )
12701284ON p.[object_id] = qs.[object_id]
1285+ CROSS APPLY sys .dm_exec_query_plan (qs .plan_handle ) AS qp
12711286WHERE qs .database_id = DB_ID ()
12721287AND DATEDIFF (Minute, qs .cached_time , GETDATE ()) > 0
12731288ORDER BY avg_elapsed_time DESC OPTION (RECOMPILE );
@@ -1282,11 +1297,15 @@ ORDER BY avg_elapsed_time DESC OPTION (RECOMPILE);
12821297SELECT TOP (25 ) p .name AS [SP Name], qs .total_worker_time AS [TotalWorkerTime],
12831298qs .total_worker_time / qs .execution_count AS [AvgWorkerTime], qs .execution_count ,
12841299ISNULL (qs .execution_count / DATEDIFF (Minute, qs .cached_time , GETDATE ()), 0 ) AS [Calls/Minute],
1285- qs .total_elapsed_time , qs .total_elapsed_time / qs .execution_count
1286- AS [avg_elapsed_time], qs .cached_time
1300+ qs .total_elapsed_time , qs .total_elapsed_time / qs .execution_count AS [avg_elapsed_time],
1301+ CASE WHEN CONVERT (nvarchar (max ), qp .query_plan ) LIKE N ' %<MissingIndexes>%' THEN 1 ELSE 0 END AS [Has Missing Index],
1302+ FORMAT (qs .last_execution_time , ' yyyy-MM-dd HH:mm:ss' , ' en-US' ) AS [Last Execution Time],
1303+ FORMAT (qs .cached_time , ' yyyy-MM-dd HH:mm:ss' , ' en-US' ) AS [Plan Cached Time]
1304+ -- ,qp.query_plan AS [Query Plan] -- Uncomment if you want the Query Plan
12871305FROM sys .procedures AS p WITH (NOLOCK )
12881306INNER JOIN sys .dm_exec_procedure_stats AS qs WITH (NOLOCK )
12891307ON p.[object_id] = qs.[object_id]
1308+ CROSS APPLY sys .dm_exec_query_plan (qs .plan_handle ) AS qp
12901309WHERE qs .database_id = DB_ID ()
12911310AND DATEDIFF (Minute, qs .cached_time , GETDATE ()) > 0
12921311ORDER BY qs .total_worker_time DESC OPTION (RECOMPILE );
@@ -1300,11 +1319,15 @@ ORDER BY qs.total_worker_time DESC OPTION (RECOMPILE);
13001319SELECT TOP (25 ) p .name AS [SP Name], qs .total_logical_reads AS [TotalLogicalReads],
13011320qs .total_logical_reads / qs .execution_count AS [AvgLogicalReads],qs .execution_count ,
13021321ISNULL (qs .execution_count / DATEDIFF (Minute, qs .cached_time , GETDATE ()), 0 ) AS [Calls/Minute],
1303- qs .total_elapsed_time , qs .total_elapsed_time / qs .execution_count
1304- AS [avg_elapsed_time], qs .cached_time
1322+ qs .total_elapsed_time , qs .total_elapsed_time / qs .execution_count AS [avg_elapsed_time],
1323+ CASE WHEN CONVERT (nvarchar (max ), qp .query_plan ) LIKE N ' %<MissingIndexes>%' THEN 1 ELSE 0 END AS [Has Missing Index],
1324+ FORMAT (qs .last_execution_time , ' yyyy-MM-dd HH:mm:ss' , ' en-US' ) AS [Last Execution Time],
1325+ FORMAT (qs .cached_time , ' yyyy-MM-dd HH:mm:ss' , ' en-US' ) AS [Plan Cached Time]
1326+ -- ,qp.query_plan AS [Query Plan] -- Uncomment if you want the Query Plan
13051327FROM sys .procedures AS p WITH (NOLOCK )
13061328INNER JOIN sys .dm_exec_procedure_stats AS qs WITH (NOLOCK )
13071329ON p.[object_id] = qs.[object_id]
1330+ CROSS APPLY sys .dm_exec_query_plan (qs .plan_handle ) AS qp
13081331WHERE qs .database_id = DB_ID ()
13091332AND DATEDIFF (Minute, qs .cached_time , GETDATE ()) > 0
13101333ORDER BY qs .total_logical_reads DESC OPTION (RECOMPILE );
@@ -1317,11 +1340,15 @@ ORDER BY qs.total_logical_reads DESC OPTION (RECOMPILE);
13171340-- Top Cached SPs By Total Physical Reads. Physical reads relate to disk read I/O pressure (Query 54) (SP Physical Reads)
13181341SELECT TOP (25 ) p .name AS [SP Name],qs .total_physical_reads AS [TotalPhysicalReads],
13191342qs .total_physical_reads / qs .execution_count AS [AvgPhysicalReads], qs .execution_count ,
1320- qs .total_logical_reads ,qs .total_elapsed_time , qs .total_elapsed_time / qs .execution_count
1321- AS [avg_elapsed_time], qs .cached_time
1343+ qs .total_logical_reads ,qs .total_elapsed_time , qs .total_elapsed_time / qs .execution_count AS [avg_elapsed_time],
1344+ CASE WHEN CONVERT (nvarchar (max ), qp .query_plan ) LIKE N ' %<MissingIndexes>%' THEN 1 ELSE 0 END AS [Has Missing Index],
1345+ FORMAT (qs .last_execution_time , ' yyyy-MM-dd HH:mm:ss' , ' en-US' ) AS [Last Execution Time],
1346+ FORMAT (qs .cached_time , ' yyyy-MM-dd HH:mm:ss' , ' en-US' ) AS [Plan Cached Time]
1347+ -- ,qp.query_plan AS [Query Plan] -- Uncomment if you want the Query Plan
13221348FROM sys .procedures AS p WITH (NOLOCK )
13231349INNER JOIN sys .dm_exec_procedure_stats AS qs WITH (NOLOCK )
13241350ON p.[object_id] = qs.[object_id]
1351+ CROSS APPLY sys .dm_exec_query_plan (qs .plan_handle ) AS qp
13251352WHERE qs .database_id = DB_ID ()
13261353AND qs .total_physical_reads > 0
13271354ORDER BY qs .total_physical_reads DESC , qs .total_logical_reads DESC OPTION (RECOMPILE );
@@ -1337,11 +1364,15 @@ ORDER BY qs.total_physical_reads DESC, qs.total_logical_reads DESC OPTION (RECOM
13371364SELECT TOP (25 ) p .name AS [SP Name], qs .total_logical_writes AS [TotalLogicalWrites],
13381365qs .total_logical_writes / qs .execution_count AS [AvgLogicalWrites], qs .execution_count ,
13391366ISNULL (qs .execution_count / DATEDIFF (Minute, qs .cached_time , GETDATE ()), 0 ) AS [Calls/Minute],
1340- qs .total_elapsed_time , qs .total_elapsed_time / qs .execution_count AS [avg_elapsed_time],
1341- qs .cached_time
1367+ qs .total_elapsed_time , qs .total_elapsed_time / qs .execution_count AS [avg_elapsed_time],
1368+ CASE WHEN CONVERT (nvarchar (max ), qp .query_plan ) LIKE N ' %<MissingIndexes>%' THEN 1 ELSE 0 END AS [Has Missing Index],
1369+ FORMAT (qs .last_execution_time , ' yyyy-MM-dd HH:mm:ss' , ' en-US' ) AS [Last Execution Time],
1370+ FORMAT (qs .cached_time , ' yyyy-MM-dd HH:mm:ss' , ' en-US' ) AS [Plan Cached Time]
1371+ -- ,qp.query_plan AS [Query Plan] -- Uncomment if you want the Query Plan
13421372FROM sys .procedures AS p WITH (NOLOCK )
13431373INNER JOIN sys .dm_exec_procedure_stats AS qs WITH (NOLOCK )
13441374ON p.[object_id] = qs.[object_id]
1375+ CROSS APPLY sys .dm_exec_query_plan (qs .plan_handle ) AS qp
13451376WHERE qs .database_id = DB_ID ()
13461377AND qs .total_logical_writes > 0
13471378AND DATEDIFF (Minute, qs .cached_time , GETDATE ()) > 0
0 commit comments