Query does nothing except context switches I run Adam Mechanic's WhoIsActive every so often on my server and log results to a table.  I noticed a peculiar case while looking at some of the results and would like help interpreting what is happening.

There was a query that had been executing for about 30 seconds, but it seems it was not doing any work.  There were no wait stats, no reads, no writes, no CPU usage, but there were several thousand context switches.

   wait_info  CPU    tempdb_allocations tempdb_current  blocking_session_id  reads  writes context_switches physical_io physical_reads  query_plan  used_memory
   NULL       0      0                  0               NULL                 0      0      8,404            0           0               NULL        4


The query responsible is a pretty simple Select ... Into a temp table. And unfortunately I cannot reproduce the scenario, because if I run the query now, it finishes in under a second.  But but my curiosity is killing me and I'd like to understand what could have been happening during the 30 seconds when it seems to have been doing nothing.
I'd like to understand what could have been happening during the 30 seconds when it seems to have been doing nothing.


If a query isn't using CPU, it's in a wait, which are tracked as the system-level in sys.dm_os_wait_stats.

SQL Server 2016 introduces a DMV for session-level wait stats with sys.dm_exec_session_wait_stats.

SQL Server 2017 introduces wait tracking at the query level in Query Store with sys.query_store_wait_stats.

Earlier versions can use Extended Events to see the waits for a particular session.  See eg: https://blogs.msdn.microsoft.com/sqlsakthi/2011/02/20/troubleshooting-slow-running-query-using-extended-events-wait-info-event/