--根据作业名 查找session id
select a.spid,a.blocked,b.name,substring(replace(a.PROGRAM_NAME,'SQLAgent - TSQL JobStep (Job ',''),1,34) JobStep ,a.last_batch
from master.dbo.sysprocesses a inner join msdb.dbo.sysjobs b
on master.dbo.fn_varbintohexstr(CONVERT(varbinary(16), job_id)) = substring(replace(PROGRAM_NAME,'SQLAgent - TSQL JobStep (Job ',''),1,34)
--根据session id 执行计划
select a1.session_id,b1.text,a1.query_plan,a1.cpu_time,a1.logical_reads,a1.sql_handle,a1.plan_handle from
(
select * from sys.dm_exec_requests a cross apply sys.dm_exec_query_plan (a.plan_handle) b
where a.session_id<>@@spid
)a1 cross apply sys.dm_exec_sql_text (a1.sql_handle)b1
order by a1.cpu_time desc
--查看执行计划
set statistics io on
set statistics time on
set statistics profile on
--单条语句执行很快,但session长时间未结束
确定语句是否循环执行(查看sp内容),多次执行同一条语句,
导致表面上是简单sql造成的,但是重复执行相同语句,以为是一条语句运行慢导致session执行很长时间 ,其实是n×单次时间