Synchronize Queries Retrieving Sessions And Locks From DMVs
-
31-10-2019 - |
Question
I have a SQL script that I run when I want to see what is happening in the database. The script has many queries that return information from DMVs, but the two I use most are "Requests" (sys.dm_exec_requests, sys.dm_exec_sessions, etc.) and "Locks" (sys.dm_tran_locks). The output is similar to SQL Server Activity Monitor, but it displays more information.
Sometimes, a request appears in the Requests query, but it completes before the Locks query runs. For example, the Requests query may show SPID 51 is waiting on a lock resource, but the Locks query does not include any lock information for SPID 51. (I know about the wait_type
and wait_resource
columns from sys.dm_exec_requests
.)
Is there a way to ensure that these two separate queries display a coherent snapshot of database activity?
I expect commercial database monitoring applications must encounter the same problem.
I have experimented with running these queries in SERIALIZABLE concurrency and added locking hints to the DMV joins, but the queries acquire no locks. I would not want to do this in production anyway.
The best ideas I have so far are:
Run these queries simultaneously from different sessions.
Join requests and locks together in one query. Considering that I have seen over 100,000 locks at once, this join would return a lot of duplicate request and session data, but it might work.
I am not familiar enough with extended events to know if they would work better-- event pairing, maybe?
No correct solution