前些天一个棘手的问题,早上上班刚到公司不久,就发现系统里面出现了大量的查询超时。
一开始以为是某个语句锁住了,感觉查查,看看系统里面是否有语句死锁了,,
select 0 , blocked
from ( select * from master..sysprocesses where blocked>0 )a
where not exists( select * from master..sysprocesses where a.blocked =spid and blocked >0)
union select spid ,blocked from master ..sysprocesses where blocked >0
在数据库执行这个语句,发现没有死锁的问题,
系统还一直在超时的错误,突然想起来,是不是某个语句阻塞了,导致数据库其他查询都被阻塞了。于是赶紧问牛逼的 文佳哥。
给了我一段查询系统阻塞的sql 语句,
--查看阻塞
SELECT
SPID = er.session_id
,STATUS = ses.STATUS
,[LOGIN] = ses.login_name
,HOST = ses.host_name
,BlkBy = er.blocking_session_id
,DBName = DB_NAME(er.database_id)
,CommandType = er.command
,SQLStatement = st.text
,BlockingText = bst.text
,ObjectName = OBJECT_NAME(st.objectid)
,ElapsedMS = er.total_elapsed_time
,CPUTime = er.cpu_time
,IOReads = er.logical_reads + er.reads
,IOWrites = er.writes
,LastWaitType = er.last_wait_type
,StartTime = er.start_time
,Protocol = con.net_transport
,ConnectionWrites = con.num_writes
,ConnectionReads = con.num_reads
,ClientAddress = con.client_net_address
,Authentication = con.auth_scheme
FROM sys.dm_exec_requests er
OUTER APPLY sys.dm_exec_sql_text(er.sql_handle) st
LEFT JOIN sys.dm_exec_sessions ses
ON ses.session_id = er.session_id
LEFT JOIN sys.dm_exec_connections con
ON con.session_id = ses.session_id
LEFT JOIN sys.dm_exec_requests ber
ON er.blocking_session_id=ber.session_id
OUTER APPLY sys.dm_exec_sql_text(ber.sql_handle) bst
WHERE er.session_id > 50
ORDER BY er.blocking_session_id DESC,er.session_id
SELECT
SPID = er.session_id
,STATUS = ses.STATUS
,[LOGIN] = ses.login_name
,HOST = ses.host_name
,BlkBy = er.blocking_session_id
,DBName = DB_NAME(er.database_id)
,CommandType = er.command
,SQLStatement = st.text
,BlockingText = bst.text
,ObjectName = OBJECT_NAME(st.objectid)
,ElapsedMS = er.total_elapsed_time
,CPUTime = er.cpu_time
,IOReads = er.logical_reads + er.reads
,IOWrites = er.writes
,LastWaitType = er.last_wait_type
,StartTime = er.start_time
,Protocol = con.net_transport
,ConnectionWrites = con.num_writes
,ConnectionReads = con.num_reads
,ClientAddress = con.client_net_address
,Authentication = con.auth_scheme
FROM sys.dm_exec_requests er
OUTER APPLY sys.dm_exec_sql_text(er.sql_handle) st
LEFT JOIN sys.dm_exec_sessions ses
ON ses.session_id = er.session_id
LEFT JOIN sys.dm_exec_connections con
ON con.session_id = ses.session_id
LEFT JOIN sys.dm_exec_requests ber
ON er.blocking_session_id=ber.session_id
OUTER APPLY sys.dm_exec_sql_text(ber.sql_handle) bst
WHERE er.session_id > 50
ORDER BY er.blocking_session_id DESC,er.session_id
通过这个语句一查,还真有几条语句处于阻塞的状态,一直在等待,但是等待的语句太多,也不知道是因为那个阻塞的,通过阻塞时间,和其他的参数也看不出来,也没办只能把所有阻塞的语句都kill 掉。kill SPID
经过一阵折腾,系统总算是恢复正常了。但是还是没有找到罪魁祸首,没办法有加了一些日子,果然,没过多次时间,又被阻塞了,于是分析相关的日志,终于找到相关的语句了。