当前位置: 首页 > news >正文

SQL Server进程阻塞的检查和解决办法

先声明,这篇文章是转自(文心残)的Blog

create procedure sp_who_lock

as

begin

declare @spid int,@bl int,

@intTransactionCountOnEntry int,

@intRowcount int,

@intCountProperties int,

@intCounter int

create table #tmp_lock_who (

id int identity(1,1),

spid smallint,

bl smallint)

IF @@ERROR<>0 RETURN @@ERROR

insert into #tmp_lock_who(spid,bl) select 0 ,blocked

from (select * from sysprocesses where blocked>0 ) a

where not exists(select * from (select * from sysprocesses where blocked>0 ) b

where a.blocked=spid)

union select spid,blocked from sysprocesses where blocked>0

IF @@ERROR<>0 RETURN @@ERROR

-- 找到临时表的记录数

select @intCountProperties = Count(*),@intCounter = 1

from #tmp_lock_who

IF @@ERROR<>0 RETURN @@ERROR

if @intCountProperties=0

select '现在没有阻塞信息' as message

-- 循环开始

while @intCounter <= @intCountProperties

begin

-- 取第一条记录

select @spid = spid,@bl = bl

from #tmp_lock_who where Id = @intCounter

begin

if @spid =0

select '引起数据库阻塞的是: '+ CAST(@bl AS VARCHAR(10)) + '进程号,其执行的SQL语法如下'

else

select '进程号SPID'+ CAST(@spid AS VARCHAR(10))+ '' + '进程号SPID'+ CAST(@bl AS VARCHAR(10)) +'阻塞,其当前进程执行的SQL语法如下'

DBCC INPUTBUFFER (@bl )

end

-- 循环指针下移

set @intCounter = @intCounter + 1

end

drop table #tmp_lock_who

return 0

end

GO

--结束SQL阻塞的进程%%%%%%%%%%%%%%%%%%%%%%

create procedure sp_Kill_lockProcess

as

begin

SET NOCOUNT ON

declare @spid int,@bl int,

@intTransactionCountOnEntry int,

@intRowcount int,

@intCountProperties int,

@intCounter int,

@sSql nvarchar (200)

create table #tmp_lock_who (

id int identity(1,1),

spid smallint,

bl smallint)

IF @@ERROR<>0 RETURN @@ERROR

insert into #tmp_lock_who(spid,bl)

select 0 ,blocked

from

(select * from sysprocesses where blocked>0 ) a

where not exists

(

select * from (select * from sysprocesses where blocked>0 ) b

where a.blocked=spid

)

union select spid,blocked from sysprocesses where blocked>0

IF @@ERROR<>0 RETURN @@ERROR

-- 找到临时表的记录数

select @intCountProperties = Count(*),@intCounter = 1

from #tmp_lock_who

IF @@ERROR<>0 RETURN @@ERROR

while @intCounter <= @intCountProperties

begin

-- 取第一条记录

select @spid = spid,@bl = bl

from #tmp_lock_who where Id = @intCounter

begin

if @spid =0

begin

set @sSql='kill ' + CAST(@bl AS VARCHAR(10))

exec sp_executesql @sSql

end

end

-- 循环指针下移

set @intCounter = @intCounter + 1

end

drop table #tmp_lock_who

SET NOCOUNT OFF

return 0

end

GO

查看锁信息

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


--查看锁信息
create table #t(req_spid int,obj_name sysname)

declare @s nvarchar(4000)
,@rid int,@dbname sysname,@id int,@objname sysname

declare tb cursor for
select distinct req_spid,dbname=db_name(rsc_dbid),rsc_objid
from master..syslockinfo where rsc_type in(4,5)
open tb
fetch next from tb into @rid,@dbname,@id
while @@fetch_status=0
begin
set @s='select @objname=name from ['+@dbname+']..sysobjects where id=@id'
exec sp_executesql @s,N'@objname sysname out,@id int',@objname out,@id
insert into #t values(@rid,@objname)
fetch next from tb into @rid,@dbname,@id
end
close tb
deallocate tb

select 进程id=a.req_spid
,数据库=db_name(rsc_dbid)
,类型=case rsc_type when 1 then 'NULL 资源(未使用)'
when 2 then '数据库'
when 3 then '文件'
when 4 then '索引'
when 5 then '表'
when 6 then '页'
when 7 then '键'
when 8 then '扩展盘区'
when 9 then 'RID(行 ID)'
when 10 then '应用程序'
end
,对象id=rsc_objid
,对象名=b.obj_name
,rsc_indid
from master..syslockinfo a left join #t b on a.req_spid=b.req_spid

go
drop table #t

相关文章:

  • Azure 软件架构选择
  • Flex与.NET互操作(十二):FluorineFx.Net的及时通信应用(Remote Shared Objects)(三)
  • 简单说说什么是Restful
  • Flex与.NET互操作(十三):FluorineFx.Net实现视频录制与视频回放
  • Swift中的本地化实现
  • Azure .Net应用架构原型
  • Nebula3中的委托(Delegate)
  • 使用微软T4 template进行代码生成
  • ora-12537教训
  • Asp.Net MVC webAPI Token based authentication
  • Asp.Net 2.0 TreeView的Checkbox级联操作
  • azure最佳实践系列1-自我修复的设计
  • Nebula3 RTTI 小技巧
  • azure 最佳实践 -- 保持冗余
  • Swift 3 打印功能
  • Angular js 常用指令ng-if、ng-class、ng-option、ng-value、ng-click是如何使用的?
  • classpath对获取配置文件的影响
  • JavaScript DOM 10 - 滚动
  • Java反射-动态类加载和重新加载
  • Java新版本的开发已正式进入轨道,版本号18.3
  • Laravel Mix运行时关于es2015报错解决方案
  • Less 日常用法
  • maven工程打包jar以及java jar命令的classpath使用
  • node-glob通配符
  • open-falcon 开发笔记(一):从零开始搭建虚拟服务器和监测环境
  • React 快速上手 - 07 前端路由 react-router
  • Spring-boot 启动时碰到的错误
  • Webpack 4x 之路 ( 四 )
  • XML已死 ?
  • 复杂数据处理
  • 个人博客开发系列:评论功能之GitHub账号OAuth授权
  • 给Prometheus造假数据的方法
  • 入门到放弃node系列之Hello Word篇
  • 通过几道题目学习二叉搜索树
  • 怎样选择前端框架
  • UI设计初学者应该如何入门?
  • 第二十章:异步和文件I/O.(二十三)
  • 资深实践篇 | 基于Kubernetes 1.61的Kubernetes Scheduler 调度详解 ...
  • #pragma 指令
  • #微信小程序:微信小程序常见的配置传值
  • (10)ATF MMU转换表
  • (C#)Windows Shell 外壳编程系列4 - 上下文菜单(iContextMenu)(二)嵌入菜单和执行命令...
  • (附源码)springboot宠物管理系统 毕业设计 121654
  • (原)Matlab的svmtrain和svmclassify
  • (转) Android中ViewStub组件使用
  • * 论文笔记 【Wide Deep Learning for Recommender Systems】
  • .[hudsonL@cock.li].mkp勒索病毒数据怎么处理|数据解密恢复
  • .aanva
  • .NET CF命令行调试器MDbg入门(三) 进程控制
  • .net 无限分类
  • .NET是什么
  • [ JavaScript ] JSON方法
  • [ 蓝桥杯Web真题 ]-Markdown 文档解析
  • [Android] 修改设备访问权限
  • [c++] 什么是平凡类型,标准布局类型,POD类型,聚合体