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.
SELECT TOP 20
deqs.execution_count AS TotalExecutionTime
,OBJECT_NAME(objectid) AS ObjectName
,QueryText = SUBSTRING(
(CASE WHEN deqs.statement_end_offset = -1
THEN len(CONVERT(nvarchar(MAX), dest.text)) * 2
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:
Please share your ideas and opinions about this topic with me, your contribution will add true value to this topic.
If anyone has doubts on this topic then please do let me know by leaving comments or send me an email.
If you like this post, then please share it with others.
Please follow dbrnd.com, I will share my experience towards the success of Database Research and Development Activity.
I put up a post every day, please keep reading and learning.
Discover Yourself, Happy Blogging !
Anvesh M. Patel.