Identifying Executing and Longest Running Queries in SQL Server

In today's blog, we will explore how to identify the currently executing queries on a SQL Server database and how to identify the longest running queries. This can be useful for optimizing query performance and troubleshooting slow-running queries.



Using the DM Execute Request View

To achieve this, we will be using a system view in SQL Server called DM execute request. This view provides a list of all the executing queries on the SQL Server database.

SELECT  *

FROM    sys.dm_exec_requests  

        CROSS APPLY sys.dm_exec_sql_text(sql_handle) 


By executing a SELECT statement from the DM execute request view, we can obtain a list of queries. In the output, you will notice that some rows have a null SQL handle and a command type of either task manager or some parallel task. These rows represent system processes that are running in the background.

However, our main interest lies in the queries with a non-null SQL handle and a command type of select. These are the queries that we want to identify.

Retrieving the Text of the Queries

Simply knowing that a select command is running is not sufficient for our purpose. We need to identify the actual text of the queries. To accomplish this, we can make use of another system function called DM execute SQL text.

Select cpu_time, text, * FROM

sys.dm_exec_requests req

CROSS APPLY sys.dm_exec_sql_text(sql_handle) txt

order by req.cpu_time DESC

By using a CROSS APPLY operation and passing the SQL handle as a parameter to the DM execute SQL text function, we can retrieve the text of the select queries. This allows us to determine which specific queries are running.

Example and Further Customizations

Let's consider an example where we execute some additional queries to demonstrate the functionality. We can also customize the output by adding columns such as CPU time and start time to get further insights into query execution.

By ordering the output in descending order based on the CPU time column, we can identify the query that is taking the longest time to run. This can be helpful in troubleshooting performance issues and identifying queries that are causing slowdowns.

Other Useful Functions

While the approach we discussed is straightforward and effective, it's important to note that SQL Server provides a variety of functions for identifying different aspects of query execution.

For example, there are functions available for identifying queries that are producing locks or for analyzing query statistics. These functions can provide additional insights into query performance and help with optimization efforts.

Conclusion

Identifying executing queries and the longest running queries in SQL Server is crucial for optimizing performance and troubleshooting slow-running queries. By using the DM execute request view and the DM execute SQL text function, we can easily obtain the necessary information to identify and analyze queries.


If you found this blog helpful, please consider liking, commenting, and sharing. Don't forget to subscribe to our YouTube channel for more informative videos. Thank you for reading!


Post a Comment

Previous Post Next Post

Contact Form