记录两种排查死锁的方法:
方法一:这个存储过程可以很清楚的看到当前死锁跟阻塞的sql;并释放进程
1,创建表来存储释放掉的sql,方便排查问题
GO
/****** Object: Table [dbo].[killSpId] Script Date: 2020/6/16 14:12:40 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[killSpId](
[id] [int] IDENTITY(1,1) NOT NULL,
[spId] [int] NULL,
[createTime] [datetime] NULL,
[type] [nvarchar](10) NULL,
[sql] [nvarchar](4000) NULL,
CONSTRAINT [PK_killSpId] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
2,创建存储过程。
GO
/****** Object: StoredProcedure [dbo].[sp_whoIsLockAndKillSpId] Script Date: 2020/6/16 14:10:13 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE procedure [dbo].[sp_whoIsLockAndKillSpId]
as
begin
declare @spid int
declare @blk int
declare @count int
declare @index int
declare @lock tinyint
declare @tmpSpId int
DECLARE @TSQL nvarchar(max)
DECLARE @IPB TABLE
(EVENTTYPE nvarchar(30),
PARAMETERS int,
EVENTINFO nvarchar(4000))
set @lock=0
create table #temp_who_lock
(
id int identity(1,1),
spid int,
blk int
)
--insert into killSpId(sql,createTime)values('haha',GETDATE())
if @@error<>0 return @@error
insert into #temp_who_lock(spid,blk)
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
if @@error<>0 return @@error
select @count=count(*),@index=1 from #temp_who_lock
if @@error<>0 return @@error
if @count=0
begin
select '没有阻塞和死锁信息'
return 0
end
while @index<=@count
begin
if exists(select 1 from #temp_who_lock a where id>@index and exists(select 1 from #temp_who_lock where id<=@index and a.blk=spid))
begin
set @lock=1
select @spid=spid,@blk=blk from #temp_who_lock where id=@index
select '引起数据库死锁的是: '+ CAST(@spid AS VARCHAR(10)) + '进程号,其执行的SQL语法如下'
select @spid, @blk
if(@spid <> 0)
begin
dbcc inputbuffer(@spid)
dbcc inputbuffer(@blk)
end
INSERT INTO @IPB
EXEC('DBCC INPUTBUFFER('+@spid+')')
insert into killSpId(spId,createTime,type,sql)
SELECT @spid,GETDATE(),'lock',EVENTINFO from @IPB
exec('kill '+ @spid)
end
set @index=@index+1
end
if @lock=0
begin
set @index=1
while @index<=@count
begin
select @spid=spid,@blk=blk from #temp_who_lock where id=@index
if @spid=0
begin
select '引起阻塞的是:'+cast(@blk as varchar(10))+ '进程号,其执行的SQL语法如下'
INSERT INTO @IPB
EXEC('DBCC INPUTBUFFER('+@spid+')')
insert into killSpId(spId,createTime,type,sql)
SELECT @spid,GETDATE(),'blocked',EVENTINFO from @IPB
end
else
begin
select '进程号SPID:'+ CAST(@spid AS VARCHAR(10))+ '被' + '进程号SPID:'+ CAST(@blk AS VARCHAR(10)) +'阻塞,其当前进程执行的SQL语法如下'
-- insert into killSpId(spId,createTime,[type],sql) values(@spid,GETDATE(),'blocked','')
if(CAST(@spid AS VARCHAR(10))<>CAST(@blk AS VARCHAR(10)))
begin
exec('kill '+ @blk)
INSERT INTO @IPB
EXEC('DBCC INPUTBUFFER('+@spid+')')
insert into killSpId(spId,createTime,type,sql)
SELECT @spid,GETDATE(),'blocked',EVENTINFO from @IPB
end
end
if(@spid <> 0)
begin
dbcc inputbuffer(@spid)
dbcc inputbuffer(@blk)
end
-- insert into killSpId(spId,createTime,sql) values(@spid,GETDATE(),'blocked')
-- exec('kill '+ @spid)
set @index=@index+1
end
end
drop table #temp_who_lock
return 0
end
GO
方法二:当【方法一】查不到的时候可以试试【方法二】,多执行几次看看是否存在长时间占用表不释放的进程。
select request_session_id spid,OBJECT_NAME(resource_associated_entity_id) tableName
into #tmpspId
from sys.dm_tran_locks where resource_type='OBJECT'
select *from #tmpspId --where tableName='AP_VC_VaccineStock'
drop table #tmpspId