您的位置:首页 > 数据库

Sqlserver 2005+:【当前】【正在运行】的【进程会话的请求信息】

2012-11-20 16:41 330 查看
set nocount on
set transaction isolation level read uncommitted

;with cte as
(
select t.session_id as spid,t.status,t.blocking_session_id as blocked
,t.program_name
,str(1.0* t.total_elapsed_time/1000,16,0) as duration_s
,str(1.0*(t.cpu_time+t.wait_time)/1000,16,0) as inter_duration_s
,str(1.0*(t.cpu_time)/1000,16,0) as cpu_time_s
,str(1.0*(t.wait_time)/1000,16,0) as wait_time_s
,t.granted_query_memory*8/1024 as mb,t.logical_reads,t.reads,t.writes
,t.start_time,t.login_time
,t.command,t.wait_type,t.wait_resource,s.text
from
(
select b.session_id,b.status,a.blocking_session_id,b.program_name
,a.command,a.sql_handle
,a.cpu_time,a.wait_time,a.total_elapsed_time,a.granted_query_memory
,a.start_time,b.login_time
,a.logical_reads,a.reads,a.writes
,a.wait_type,a.wait_resource
from sys.dm_exec_requests a inner join sys.dm_exec_sessions b on b.session_id=a.session_id
where b.program_name is not null
and b.session_id <> @@spid
) t cross apply sys.dm_exec_sql_text(t.sql_handle) s
)
select
(case when exists(select * from cte b where b.spid=a.spid and b.blocked=0 and (a.spid in (select blocked from cte c))) then 'yes' else '' end) as is_blocker
,*
from cte a
where
(blocked>0) or
(spid in (select blocked from cte)) or
duration_s>5
order by blocked,program_name,spid

/*

select spid,program_name,blocked,open_tran,a.status,str((waittime + cpu)/1000.0,16,0) as duration,waittime,cpu,physical_io,last_batch,a.login_time--,b.text
,a.lastwaittype,a.waitresource
from master..sysprocesses a cross apply sys.dm_exec_sql_text (a.sql_handle) b
where spid<>@@SPID and spid>50
order by program_name
and program_name like '.Net%'

*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: