Get the most frequently executed queries for this database

SELECT TOP (50) LEFT(t.[text], 50) AS [Short Query Text]
	,qs.execution_count AS [Execution Count]
	,qs.total_logical_reads AS [Total Logical Reads]
	,qs.total_logical_reads / qs.execution_count AS [Avg Logical Reads]
	,qs.total_worker_time AS [Total Worker Time]
	,qs.total_worker_time / qs.execution_count AS [Avg Worker Time]
	,qs.total_elapsed_time AS [Total Elapsed Time]
	,qs.total_elapsed_time / qs.execution_count AS [Avg Elapsed Time]
	,CASE WHEN CONVERT(NVARCHAR(max), qp.query_plan) COLLATE Latin1_General_BIN2 LIKE N'%<MissingIndexes>%' THEN 1 ELSE 0 END AS [Has Missing Index]
	,qs.creation_time AS [Creation Time]
--,t.[text] AS [Complete Query Text], qp.query_plan AS [Query Plan] -- uncomment out these columns if not copying results to Excel
FROM sys.dm_exec_query_stats AS qs WITH (NOLOCK)
CROSS APPLY sys.dm_exec_sql_text(plan_handle) AS t
CROSS APPLY sys.dm_exec_query_plan(plan_handle) AS qp
WHERE t.dbid = DB_ID()
ORDER BY qs.execution_count DESC
OPTION (RECOMPILE);

dm_exec_query_stats, dm_exec_query_plan

Spread the love