SQL query to Identify the most resource intensive queries in sql server database


You can use the “sys.dm_exec_query_stats” dynamic management view in SQL Server to identify the most resource-intensive queries. This view contains performance statistics for cached query plans, such as CPU time, execution count, and logical reads. Here’s an example SQL query that uses this view to identify the top 10 most resource-intensive queries:

SELECT TOP 10 
    SUBSTRING(qt.TEXT, (qs.statement_start_offset/2)+1, 
    ((CASE qs.statement_end_offset
        WHEN -1 THEN DATALENGTH(qt.TEXT)
        ELSE qs.statement_end_offset
    END - qs.statement_start_offset)/2)+1) AS [Query Text], 
    qs.total_worker_time AS [Total Worker Time],
    qs.execution_count AS [Execution Count], 
    qs.total_elapsed_time AS [Total Elapsed Time],
    qs.total_logical_reads AS [Total Logical Reads]
FROM 
    sys.dm_exec_query_stats qs
CROSS APPLY 
    sys.dm_exec_sql_text(qs.sql_handle) qt
ORDER BY 
    qs.total_worker_time DESC;

In this query, the “sys.dm_exec_query_stats” view is joined with the “sys.dm_exec_sql_text” view using a CROSS APPLY function to get the query text associated with each cached query plan. The columns returned by the query include the query text, total worker time, execution count, total elapsed time, and total logical reads. The results are sorted in descending order by total worker time, which is a measure of the CPU time used by each query. The TOP 10 clause limits the output to the top 10 most resource-intensive queries.

Author: Rajib Kundu

Rajib Kundu is a SQL Server Evangelist/Cloud Architect with over 16 years of Expertise.A husband, father of boy, entrepreneur, Rajib has learned throughout his journey the value of hard work and giving back. Rajib is a hard working & high energy individual fueled by his passion to help people and solve problems on SQL Server and Robotic Process Automation. Rajib Kundu is a technology enthusiast with a track record of delivering breakthrough innovations at leading Indian pure play & multi national IT companies. Equally comfortable with executives, creative directors and engineers, Rajib has differentiated himself through his ability to balance business and technology needs to deliver powerful solutions. Rajib Kundu Innovative with demonstrated success in increasing revenues, market share, and earnings, achieving cost reduction, and improving client satisfaction in customer-facing operations and large, diverse organisation. Expertise in management and implementation of complex corporate acquisition initiatives and successful organisation Integrations. Talent for analysing competitive landscape, conducting research, and aligning product offering with customer requirements which translates into lower product development and Implementation costs. Reputation as a change agent With the ability to analyse issues, devise continuous process Improvements, and incorporate business process outsourcing Initiatives to Increase efficiency ,streamline operations, and decrease aggregate expenses With limited resources. You can contact me here: India:-+91-9731155800 | USA & Canada: +1- 647-694-1826 | Skype: rk_india1@rediffmail.com

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.