26 November,2012 by Jack Vamvas
Do you know what statements are currently running on the SQL Server tempdb database? Can you tell which statements are creating insufficient disk space in the tempdb?
Use this statement to list the current tempdb statements. It is very useful for troubleshooting tempdb disk usage and which queries are creating resource contention on SQL Server tempdb.
Maintaining SQL Server tempdb performance is critical for overall performance. Tempdb has many uses – such as Version Stores, Internal objects (join work files) and User objects (tables,indices). Read more on TempDB performance and strategy checklist
The statement uses the DMV:
sys.dm_db_session_space_usage - Returns the number of pages allocated and deallocated by each session for the database
sys.dm_exec_requests - Returns information about each request that is executing within SQL Server.SQL Server – Current queries executing on SQL Server
sys.dm_exec_sql_text - Returns the text of the SQL batch that is identified by the specified sql_handle
sys.dm_db_task_space_usage - Returns page allocation and deallocation activity by task for the database
SELECT ssu.session_id, (ssu.internal_objects_alloc_page_count + sess_alloc) as allocated, (ssu.internal_objects_dealloc_page_count + sess_dealloc) as deallocated , stm.TEXT from sys.dm_db_session_space_usage as ssu, sys.dm_exec_requests req CROSS APPLY sys.dm_exec_sql_text(sql_handle) AS stm , (select session_id, sum(internal_objects_alloc_page_count) as sess_alloc, sum (internal_objects_dealloc_page_count) as sess_dealloc from sys.dm_db_task_space_usage group by session_id) as tsk where ssu.session_id = tsk.session_id and ssu.session_id >50 and ssu.session_id = req.session_id and ssu.database_id = 2 order by allocated DESC