Sys.dm_exec_sql_text(sql_handle) is a function that returns the executed command from sql_handle.
Putting them together with CROSS APPLY (APPLY lets you join the output of a function), you can see what is being run and how often.
SELECT t.text , s.* FROM sys.dm_exec_query_stats s CROSS APPLY sys.dm_exec_sql_text(sql_handle) t WHERE t.text NOT like 'SELECT * FROM(SELECT coalesce(object_name(s2.objectid)%' ORDER BY execution_count DESC
This provides you 35 columns summarising query activity and includes counts, times, reads, writes statistics.
SQLDenis has provided a great query, which i've slightly adapted to order by the the most commonly executed queries.
It tells you where the sql is called from (ProcedureName) or replaces it with 'Ad-hoc' if not called from a procedure.
SELECT * FROM(SELECT COALESCE(OBJECT_NAME(s2.objectid),'Ad-Hoc') AS ProcedureName,execution_count, (SELECT TOP 1 SUBSTRING(s2.TEXT,statement_start_offset / 2+1 , ( (CASE WHEN statement_end_offset = -1 THEN (LEN(CONVERT(NVARCHAR(MAX),s2.TEXT)) * 2) ELSE statement_end_offset END) - statement_start_offset) / 2+1)) AS sql_statement, last_execution_time FROM sys.dm_exec_query_stats AS s1 CROSS APPLY sys.dm_exec_sql_text(sql_handle) AS s2 ) x WHERE sql_statement NOT like 'SELECT * FROM(SELECT coalesce(object_name(s2.objectid)%' ORDER BY execution_count DESC
SQLDenis's original post >http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/finding-out-how-many-times-a-table-is-be-2008
MSDN Reference : http://msdn.microsoft.com/en-us/library/ms189741.aspx
No comments:
Post a Comment