1.日常排查语句
--当前正在执行的语句
SELECT
der.[session_id],der.[blocking_session_id],
sp.lastwaittype,sp.hostname,sp.program_name,sp.loginame,
der.[start_time] AS '开始时间',
der.[status] AS '状态',
dest.[text] AS 'sql语句',
DB_NAME(der.[database_id]) AS '数据库名',
der.[wait_type] AS '等待资源类型',
der.[wait_time] AS '等待时间',
der.[wait_resource] AS '等待的资源',
der.[logical_reads] AS '逻辑读次数'
FROM sys.[dm_exec_requests] AS der
INNER JOIN master.dbo.sysprocesses AS sp ON der.session_id=sp.spid
CROSS APPLY sys.[dm_exec_sql_text](der.[sql_handle]) AS dest
WHERE [session_id]>50 AND session_id<>@@SPID
ORDER BY der.[session_id]
GO --是否堵塞
SELECT spid,blocked,waittime,waittype,waitresource,p.dbid,cpu,physical_io,memusage,open_tran
,status,login_time,last_batch,hostname,program_name,hostprocess,loginame,cmd,text
FROM master.dbo.sysprocesses p CROSS APPLY sys.dm_exec_sql_text(p.sql_handle) s
WHERE blocked > 0 OR spid IN(SELECT blocked FROM master.dbo.sysprocesses WHERE blocked > 0)
go --是否有未提交事物
select spid,blocked,waittime,waittype,waitresource,p.dbid,cpu,physical_io,memusage,open_tran
,status,login_time,last_batch,hostname,program_name,hostprocess,loginame,cmd,text
from master.dbo.sysprocesses p cross apply sys.dm_exec_sql_text(p.sql_handle) s
where open_tran <> 0
go --死锁 drop table #deadlock
CREATE TABLE #deadlock(LogDate DATETIME,ProcessInfo VARCHAR(20),Text VARCHAR(2000))
go
INSERT INTO #deadlock EXEC xp_readerrorlog 0,1,'deadlock victim',NULL,'2016-07-20 14:00:00','2016-07-30','DESC'
go
select * from #deadlock
发现较多进程正在等待某个表资源,从中查看是什么表。
waitresource/等待的资源 TAB: 7:1028198713:7
select OBJECT_NAME(1028198713)
同时看看有没有发生死锁,果然有死锁!(提前开启死锁跟踪: traceon(1222,-1))
死锁资源列表如下:
resource-list
objectlock lockPartition=27 objid=629577281 subresource=FULL dbid=7 objectname=MYDB.dbo.mytable id=lock1bdc37b280 mode=Sch-S associatedObjectId=629577281
owner-list
owner id=processe51d948 mode=Sch-S
owner id=processe4bf288 mode=Sch-S
waiter-list
waiter id=process2b95dcc508 mode=Sch-M requestType=wait
objectlock lockPartition=23 objid=629577281 subresource=FULL dbid=7 objectname=MYDB.dbo.mytable id=lock1bfb3d7680 mode=Sch-M associatedObjectId=629577281
owner-list
owner id=process2b95dcc508 mode=Sch-M
waiter-list
waiter id=process5c53dc8 mode=IS requestType=wait
objectlock lockPartition=19 objid=629577281 subresource=FULL dbid=7 objectname=MYDB.dbo.mytable id=lock1d20baba80 mode=Sch-M associatedObjectId=629577281
owner-list
waiter-list
waiter id=processe4bf288 mode=Sch-S requestType=wait
objectlock lockPartition=23 objid=629577281 subresource=FULL dbid=7 objectname=MYDB.dbo.mytable id=lock1bfb3d7680 mode=Sch-M associatedObjectId=629577281
owner-list
waiter-list
waiter id=processe51d948 mode=Sch-S requestType=wait
objectlock lockPartition=19 objid=629577281 subresource=FULL dbid=7 objectname=MYDB.dbo.mytable id=lock1d20baba80 mode=Sch-M associatedObjectId=629577281
owner-list
owner id=process2b95dcc508 mode=Sch-M
waiter-list
waiter id=processe480988 mode=IS requestType=wait
可以看到,出现了架构稳定锁(Sch-S)和架构修改锁(Sch-M),但是数据库没有人修改架构啊!查看审核信息也无人修改过表对象。什么原因??!
继续使用上面的排查语句进行多次查询,发现了一条特殊的语句!
insert bulk [dbo].[TABLES_Writes](此处省略字段)
果然是该语句了!大容量插入是要表级别的锁,并且也会出现架构锁。其他等待资源的进程,也是需要查询该表的,所以才出现了表级别的资源等待!而恰巧,要使用该表的是一个存储过程,也是比较复杂的,执行10秒以上,该存储过程使用的其实是一个视图,视图中包含了该表!暂时解决的话。在视图上加nolock。询问开发人员,说大容量导入操作随时发生的,因为有些数据是需要业务员操作导入的。这种方法以后还是得改,要么大容量先插入到一个中间表,再插入到当前表中。