How to know how long the TSQL statement has been running
Declare @ms_per_tick decimal(Ten,6)--millisecond per tickSELECT @ms_per_tick=1.0*DateDiff(Millisecond,sqlserver_start_time,getdate())/(ms_ticks-sqlserver_start_time_ms_ticks) fromSys.[Dm_os_sys_info];--SELECT @ms_per_tickSelectReq.session_id,req.start_time Request_start_time, ((SelectMs_ticks fromsys.dm_os_sys_info)-Workers.task_bound_ms_ticks)*@ms_per_tick 'Ms_since_task_bound',DATEDIFF(Ms,req.start_time,getdate())'Ms_since_request_start', Tasks.task_state,workers.state worker_state,req.status request_state,st.text, SUBSTRING(St.text, (Req.statement_start_offset/2)+1, (( CaseReq.statement_end_offset when -1 Then datalength(St.text)ELSEReq.statement_end_offsetEND -Req.statement_start_offset)/2)+ 1) asstmt, Qp.query_plan,req.* fromsys.dm_exec_requests req Left Joinsys.dm_os_tasks Tasks onTasks.task_address=req.task_address Left Joinsys.dm_os_workers Workers onTasks.task_address=workers.task_address CrossAPPLY sys.dm_exec_sql_text (req.sql_handle) St CrossAPPLY sys.dm_exec_query_plan (req.plan_handle) QPwhere(req.session_id> - orreq.session_id is NULL)Go
--Transfer from https://blogs.msdn.microsoft.com/apgcdsd/2011/05/05/tsql/
How to know how long the TSQL statement has been running