Am trying to work on script for slow-running queries. I understand that we can get the list of slow running queries using sys.dm_exec_requests
. Say, I have a query Q1 which is running very slow, say, 1 hour. Till Q1 is running i.e. executing, I can track it using sys.dm_exec_requests
DMV. If I understood it right, once Q1 is completed, its statistics will be no more available in this DMV. Instead, we need to look at sys.dm_exec_query_stats
. But how do I get the statistics of that specific query Q1 which had been slow-running ? Meaning how do I relate the data I earlier got in sys.dm_exec_requests and sys.dm_exec_query_stats?
Sql-server – SQL Server : Tracking a slow running query on completion
sql serversql-server-2008sql-server-2012
Related Question
- Sql-server – SQL Server 2012 CPU spike due to LinkedServer – how to find the offending query
- Sql-server – SQL Server : If a query is cancelled, where to get data on it
- SQL Server 2012 Performance – Super Slow with No Queries
- SQL Server – Does Microsoft Release Its SQL Query Hash Algorithm?
- Practical Impacts of sys.dm_exec_query_stats Warning in SQL Server
- SQL Server Performance – Very High Disk Activity After Running Query
Best Answer
A quick-and-dirty way to get this information is to run something like this query while the query you wish to observe is still running.
Note: this will only capture queries that are running at that moment. Additionally, the query stats DMV does not persist across reboots and may also be flushed when under pressure. Joining on
sql_handle
gets you query stats for that exact statement. You can find query stats for "similar" queries (similar logic with difference occurring only in literal values) by joining onquery_hash
.