--查找堵塞源 SELECT spid 被锁进程ID, blocked 锁进程ID,status 被锁状态, login_time 登录时间, SUBSTRING(SUSER_SNAME(sid),1,30) 被锁进程登陆帐号, SUBSTRING(hostname,1,12) 被锁进程用户机器名称, SUBSTRING(DB_NAME(dbid),1,13) 被锁进程数据名称, cmd 被锁进程命令, waittype 被锁进程等待类型 FROM master.sys.sysprocesses WHERE SPID>50 and blocked!=0 AND spid<>blocked order by waittime desc --堵塞源的堵塞类型 SELECT CASE WHEN waittime!=0X0000 AND open_tran>=0 AND status='runnable' THEN N'运行时间太长' WHEN waittime=0X0000 AND open_tran>0 AND status='sleeping' THEN N'sleeping进程事物未提交' WHEN waittime in(0x0800,0x0063) AND open_tran>=0 AND status='runnable' THEN N'没有及时取走结果集' WHEN waittime=0X0000 AND open_tran>0 AND status='rollback' THEN N'超时或主动回滚' WHEN waittime>=0X0000 AND open_tran>=0 AND status='runnable' THEN N'死锁以堵塞体现' END AS Reason, b.text SQL,DB_NAME(a.dbid) as DB,program_name,hostname,loginame FROM master.sys.sysprocesses a CROSS APPLY sys.dm_exec_sql_text(a.sql_handle) b WHERE SPID=346
--查询会话的详细信息 select s.session_id,s.status,s.login_time,s.host_name, s.program_name,s.host_process_id,s.client_version, s.client_interface_name,s.login_name,s.last_request_start_time, s.last_request_end_time,c.connect_time,c.net_transport, c.net_packet_size,c.client_net_address,r.request_id,r.start_time, r.status,r.command,r.database_id,r.user_id,r.blocking_session_id, r.wait_type,r.wait_time,r.last_wait_type,r.wait_resource, r.open_transaction_count,r.transaction_id,r.percent_complete, r.cpu_time,r.reads,r.writes,r.granted_query_memory from sys.dm_exec_requests r right outer join sys.dm_exec_sessions s on r.session_id=s.session_id right outer join sys.dm_exec_connections c on s.session_id=c.session_id where s.is_user_process=1