SQL Server程序阻塞的檢查和解決辦法

2021-08-23 15:33:43 字數 4229 閱讀 5843

先宣告,這篇文章是**(文心殘)的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

endend

-- 迴圈指標下移

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

endclose 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

godrop table #t

檢視阻塞的程序和被阻塞的程序

檢視阻塞的程序和被阻塞的程序 select from master.sysprocesses where db name dbid golddb and spid spid and dbid 0 and blocked 0 select request session id spid,object ...

sqlserver死鎖阻塞

create proc p lockinfo kill lock spid bit 1,是否殺掉死鎖的程序,1 殺掉,0 僅顯示 show spid if nolock bit 1 如果沒有死鎖的程序,是否顯示正常程序資訊,1 顯示,0 不顯示 as declare count int,s nvar...

sql server 阻塞查詢

原文 sql server 阻塞查詢 在生產環境下,有時公司客服反映網頁半天打不到,除了在瀏覽器按f12的network響應來排查,確定web伺服器無故障後。就需要檢查資料庫是否有出現阻塞 當時資料庫的生產環境中主表資料量超過2000w,子表資料量超過1億,且更新和新增頻繁。再加上做了同步映象,很消...