This article is half-done without your Comment! *** Please share your thoughts via Comment ***
In this post, I am sharing one SQL Script to find TOP 20 most used or executed query in SQL Server.
Database Administrator always prefers to find most long running queries, but It is also required to check most executed queries.
Because most executed queries might take average 1 second or 2 second for execution, but total number execution count 10 times than few long running queries.
If we find the total number of execution count more than our expectation, we can take the necessary decision like: remove redundant queries from the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
SELECT TOP 20 deqs.execution_count AS TotalExecutionTime ,OBJECT_NAME(objectid) AS ObjectName ,QueryText = SUBSTRING( dest.text, deqs.statement_start_offset/2, (CASE WHEN deqs.statement_end_offset = -1 THEN len(CONVERT(nvarchar(MAX), dest.text)) * 2 ELSE deqs.statement_end_offset END - deqs.statement_start_offset)/2) ,DatabaseName = db_name(dest.dbid) FROM sys.dm_exec_query_stats AS deqs CROSS APPLY sys.dm_exec_sql_text(deqs.sql_handle) AS dest ORDER BY deqs.execution_count DESC |
You can also access few related articles:
SQL Server: Script to find top 20 running query, which are utilizing more CPU
SQL Server: Script to find top 20 Stored Procedure, which are utilizing more CPU