[关闭]
@windwolf 2019-12-09T08:33:25.000000Z 字数 2678 阅读 411

SQL SERVER处理死锁的一揽子解决方案

编程->数据库


检测死锁

如果发生死锁了,我们怎么去检测具体发生死锁的是哪条SQL语句或存储过程?
这时我们可以使用以下存储过程来检测,就可以查出引起死锁的进程和SQL语句。SQL Server自带的系统存储过程sp_who和sp_lock也可以用来查找阻塞和死锁, 但没有这里介绍的方法好用。

  1. use master
  2. go
  3. create procedure sp_who_lock
  4. as
  5. begin
  6. declare @spid int,@bl int,
  7. @intTransactionCountOnEntry int,
  8. @intRowcount int,
  9. @intCountProperties int,
  10. @intCounter int
  11. create table #tmp_lock_who (
  12. id int identity(1,1),
  13. spid smallint,
  14. bl smallint)
  15. IF @@ERROR<>0 RETURN @@ERROR
  16. insert into #tmp_lock_who(spid,bl) select 0 ,blocked
  17. from (select * from sysprocesses where blocked>0 ) a
  18. where not exists(select * from (select * from sysprocesses where blocked>0 ) b where a.blocked=spid)
  19. union select spid,blocked from sysprocesses where blocked>0
  20. IF @@ERROR<>0 RETURN @@ERROR
  21. -- 找到临时表的记录数
  22. select @intCountProperties = Count(*),@intCounter = 1
  23. from #tmp_lock_who
  24. IF @@ERROR<>0 RETURN @@ERROR
  25. if @intCountProperties=0
  26. select '现在没有阻塞和死锁信息' as message
  27. -- 循环开始
  28. while @intCounter <= @intCountProperties
  29. begin
  30. -- 取第一条记录
  31. select @spid = spid,@bl = bl
  32. from #tmp_lock_who where Id = @intCounter
  33. begin
  34. if @spid =0
  35. select '引起数据库死锁的是: '+ CAST(@bl AS VARCHAR(10)) + '进程号,其执行的SQL语法如下'
  36. else
  37. select '进程号SPID:'+ CAST(@spid AS VARCHAR(10))+ '被' + '进程号SPID:'+ CAST(@bl AS VARCHAR(10)) +'阻塞,其当前进程执行的SQL语法如下'
  38. DBCC INPUTBUFFER (@bl )
  39. end
  40. -- 循环指针下移
  41. set @intCounter = @intCounter + 1
  42. end
  43. drop table #tmp_lock_who
  44. return 0
  45. end

杀死锁和进程

如何去手动的杀死进程和锁?最简单的办法,重新启动服务。但是这里要介绍一个存储过程,通过显式的调用,可以杀死进程和锁。

  1. use master
  2. go
  3. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_killspid]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
  4. drop procedure [dbo].[p_killspid]
  5. GO
  6. create proc p_killspid
  7. @dbname varchar(200) --要关闭进程的数据库名
  8. as
  9. declare @sql nvarchar(500)
  10. declare @spid nvarchar(20)
  11. declare #tb cursor for
  12. select spid=cast(spid as varchar(20)) from master..sysprocesses where dbid=db_id(@dbname)
  13. open #tb
  14. fetch next from #tb into @spid
  15. while @@fetch_status=0
  16. begin
  17. exec('kill '+@spid)
  18. fetch next from #tb into @spid
  19. end
  20. close #tb
  21. deallocate #tb
  22. go

用法

  1. exec p_killspid 'newdbpy'

查看锁信息

如何查看系统中所有锁的详细信息?在企业管理管理器中,我们可以看到一些进程和锁的信息,这里介绍另外一种方法。

  1. --查看锁信息
  2. create table #t(req_spid int,obj_name sysname)
  3. declare @s nvarchar(4000)
  4. ,@rid int,@dbname sysname,@id int,@objname sysname
  5. declare tb cursor for
  6. select distinct req_spid,dbname=db_name(rsc_dbid),rsc_objid
  7. from master..syslockinfo where rsc_type in(4,5)
  8. open tb
  9. fetch next from tb into @rid,@dbname,@id
  10. while @@fetch_status=0
  11. begin
  12. set @s='select @objname=name from ['+@dbname+']..sysobjects where id=@id'
  13. exec sp_executesql @s,N'@objname sysname out,@id int',@objname out,@id
  14. insert into #t values(@rid,@objname)
  15. fetch next from tb into @rid,@dbname,@id
  16. end
  17. close tb
  18. deallocate tb
  19. select 进程id=a.req_spid
  20. ,数据库=db_name(rsc_dbid)
  21. ,类型=case rsc_type when 1 then 'NULL 资源(未使用)'
  22. when 2 then '数据库'
  23. when 3 then '文件'
  24. when 4 then '索引'
  25. when 5 then '表'
  26. when 6 then '页'
  27. when 7 then '键'
  28. when 8 then '扩展盘区'
  29. when 9 then 'RID(行 ID)'
  30. when 10 then '应用程序'
  31. end
  32. ,对象id=rsc_objid
  33. ,对象名=b.obj_name
  34. ,rsc_indid
  35. from master..syslockinfo a left join #t b on a.req_spid=b.req_spid
  36. go
  37. drop table #t
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注