Chủ Nhật, 24 tháng 12, 2023

[SQL Server] Find Currently Running Queries

SELECT 
	DB_NAME(req.database_id) as db_name
	,sqltext.text
    ,req.session_id
    ,req.status
    ,req.command
	,req.start_time
    ,req.cpu_time
    ,req.total_elapsed_time
	,queryplan.query_plan
	,c.client_net_address
	,s.login_name
	,s.login_time
	,s.program_name
	,s.host_name
	,s.host_process_id
	,s.client_interface_name
	,s.client_version
	--,req.*
	--,c.*
	--,s.*
FROM sys.dm_exec_requests req
JOIN sys.dm_exec_connections c on req.connection_id = c.connection_id
JOIN sys.dm_exec_sessions s on req.session_id = s.session_id
CROSS APPLY sys.dm_exec_sql_text(sql_handle) AS sqltext
CROSS APPLY sys.dm_exec_query_plan(req.plan_handle) as queryplan