SQL Server死锁经验总结
2009-06-17 14:23
253 查看
SQL Server
死锁经验总结
作者:Terrylee将死锁减至最少
虽然不能完全避免死锁,但可以使死锁的数量减至最少。将死锁减至最少可以增加事务的吞吐量并减少系统开销,因为只有很少的事务:
回滚,而回滚会取消事务执行的所有工作。
由于死锁时回滚而由应用程序重新提交。
下列方法有助于最大限度地降低死锁:
按同一顺序访问对象。
避免事务中的用户交互。
保持事务简短并在一个批处理中。
使用低隔离级别。
使用绑定连接。
按同一顺序访问对象
如果所有并发事务按同一顺序访问对象,则发生死锁的可能性会降低。例如,如果两个并发事务获得
Supplier
表上的锁,然后获得
Part
表上的锁,则在其中一个事务完成之前,另一个事务被阻塞在 Supplier
表上。第一个事务提交或回滚后,第二个事务继续进行。不发生死锁。将存储过程用于所有的数据修改可以标准化访问对象的顺序。
![](http://terrylee.cnblogs.com/images/cnblogs_com/terrylee/PIC050.jpg)
避免事务中的用户交互
避免编写包含用户交互的事务,因为运行没有用户交互的批处理的速度要远远快于用户手动响应查询的速度,例如答复应用程序请求参数的提示。例如,如果事务正在等待用户输入,而用户去吃午餐了或者甚至回家过周末了,则用户将此事务挂起使之不能完成。这样将降低系统的吞吐量,因为事务持有的任何锁只有在事务提交或回滚时才会释放。即使不出现死锁的情况,访问同一资源的其它事务也会被阻塞,等待该事务完成。
保持事务简短并在一个批处理中
在同一数据库中并发执行多个需要长时间运行的事务时通常发生死锁。事务运行时间越长,其持有排它锁或更新锁的时间也就越长,从而堵塞了其它活动并可能导致死锁。
保持事务在一个批处理中,可以最小化事务的网络通信往返量,减少完成事务可能的延迟并释放锁。
使用低隔离级别
确定事务是否能在更低的隔离级别上运行。执行提交读允许事务读取另一个事务已读取(未修改)的数据,而不必等待第一个事务完成。使用较低的隔离级别(例如提交读)而不使用较高的隔离级别(例如可串行读)可以缩短持有共享锁的时间,从而降低了锁定争夺。
使用绑定连接
使用绑定连接使同一应用程序所打开的两个或多个连接可以相互合作。次级连接所获得的任何锁可以象由主连接获得的锁那样持有,反之亦然,因此不会相互阻塞
检测死锁
如果发生死锁了,我们怎么去检测具体发生死锁的是哪条SQL
语句或存储过程?
这时我们可以使用以下存储过程来检测,就可以查出引起死锁的进程和SQL
语句。SQL Server
自带的系统存储过程sp_who
和sp_lock
也可以用来查找阻塞和死锁,
但没有这里介绍的方法好用。
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
use
master
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
go
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
create
procedure
sp_who_lock
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
as
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
begin
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
declare
@spid
int
,
@bl
int
,
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
@intTransactionCountOnEntry
int
,
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
@intRowcount
int
,
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
@intCountProperties
int
,
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
@intCounter
int
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
create
table
#tmp_lock_who (
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
id
int
identity
(
1
,
1
),
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
spid
smallint
,
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
bl
smallint
)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
IF
@@ERROR
<>
0
RETURN
@@ERROR
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
insert
into
#tmp_lock_who(spid,bl)
select
0
,blocked
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
from
(
select
*
from
sysprocesses
where
blocked
>
0
) a
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
where
not
exists
(
select
*
from
(
select
*
from
sysprocesses
where
blocked
>
0
) b
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
where
a.blocked
=
spid)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
union
select
spid,blocked
from
sysprocesses
where
blocked
>
0
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
IF
@@ERROR
<>
0
RETURN
@@ERROR
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
--
找到临时表的记录数
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
select
@intCountProperties
=
Count
(
*
),
@intCounter
=
1
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
from
#tmp_lock_who
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
IF
@@ERROR
<>
0
RETURN
@@ERROR
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
if
@intCountProperties
=
0
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
select
'
现在没有阻塞和死锁信息
'
as
message
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
--
循环开始
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
while
@intCounter
<=
@intCountProperties
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
begin
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
--
取第一条记录
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
select
@spid
=
spid,
@bl
=
bl
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
from
#tmp_lock_who
where
Id
=
@intCounter
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
begin
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
if
@spid
=
0
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
select
'
引起数据库死锁的是:
'
+
CAST
(
@bl
AS
VARCHAR
(
10
))
+
'
进程号,其执行的SQL语法如下
'
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
else
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
select
'
进程号SPID:
'
+
CAST
(
@spid
AS
VARCHAR
(
10
))
+
'
被
'
+
'
进程号SPID:
'
+
CAST
(
@bl
AS
VARCHAR
(
10
))
+
'
阻塞,其当前进程执行的SQL语法如下
'
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
DBCC
INPUTBUFFER (
@bl
)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
end
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
--
循环指针下移
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
set
@intCounter
=
@intCounter
+
1
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
end
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
drop
table
#tmp_lock_who
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
return
0
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
end
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
杀死锁和进程
如何去手动的杀死进程和锁?最简单的办法,重新启动服务。但是这里要介绍一个存储过程,通过显式的调用,可以杀死进程和锁。
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
use
master
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
go
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
if
exists
(
select
*
from
dbo.sysobjects
where
id
=
object_id
(N
'
[dbo].[p_killspid]
'
)
and
OBJECTPROPERTY
(id, N
'
IsProcedure
'
)
=
1
)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
drop
procedure
[
dbo
]
.
[
p_killspid
]
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
GO
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
create
proc
p_killspid
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
@dbname
varchar
(
200
)
--
要关闭进程的数据库名
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
as
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
declare
@sql
nvarchar
(
500
)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
declare
@spid
nvarchar
(
20
)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
declare
#tb
cursor
for
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
select
spid
=
cast
(spid
as
varchar
(
20
))
from
master..sysprocesses
where
dbid
=
db_id
(
@dbname
)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
open
#tb
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
fetch
next
from
#tb
into
@spid
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
while
@@fetch_status
=
0
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
begin
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
exec
(
'
kill
'
+
@spid
)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
fetch
next
from
#tb
into
@spid
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
end
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
close
#tb
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
deallocate
#tb
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
go
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
--
用法
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
exec
p_killspid
'
newdbpy
'
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
查看锁信息
如何查看系统中所有锁的详细信息?在企业管理管理器中,我们可以看到一些进程和锁的信息,这里介绍另外一种方法。
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
--
查看锁信息
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
create
table
#t(req_spid
int
,obj_name sysname)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
declare
@s
nvarchar
(
4000
)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
,
@rid
int
,
@dbname
sysname,
@id
int
,
@objname
sysname
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
declare
tb
cursor
for
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
select
distinct
req_spid,dbname
=
db_name
(rsc_dbid),rsc_objid
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
from
master..syslockinfo
where
rsc_type
in
(
4
,
5
)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
open
tb
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
fetch
next
from
tb
into
@rid
,
@dbname
,
@id
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
while
@@fetch_status
=
0
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
begin
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
set
@s
=
'
select @objname=name from [
'
+
@dbname
+
'
]..sysobjects where id=@id
'
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
exec
sp_executesql
@s
,N
'
@objname sysname out,@id int
'
,
@objname
out,
@id
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
insert
into
#t
values
(
@rid
,
@objname
)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
fetch
next
from
tb
into
@rid
,
@dbname
,
@id
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
end
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
close
tb
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
deallocate
tb
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
select
进程id
=
a.req_spid
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
,数据库
=
db_name
(rsc_dbid)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
,类型
=
case
rsc_type
when
1
then
'
NULL 资源(未使用)
'
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
when
2
then
'
数据库
'
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
when
3
then
'
文件
'
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
when
4
then
'
索引
'
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
when
5
then
'
表
'
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
when
6
then
'
页
'
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
when
7
then
'
键
'
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
when
8
then
'
扩展盘区
'
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
when
9
then
'
RID(行 ID)
'
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
when
10
then
'
应用程序
'
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
end
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
,对象id
=
rsc_objid
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
,对象名
=
b.obj_name
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
,rsc_indid
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
from
master..syslockinfo a
left
join
#t b
on
a.req_spid
=
b.req_spid
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
go
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
drop
table
#t
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
总结
虽然不能完全避免死锁,但我们可以将死锁减至最少,并通过一定的方法来检测死锁。
相关文章推荐
- ----------------SQL Server2000中死锁经验总结 ---------------
- 维护SQL Server的交易日志经验总结
- SQL Server死锁总结
- [经验总结]调用WinSock的closesocket函数出现死锁的解决办法
- SQL Server 2005 一千万条以上记录分页数据库优化经验总结(转载)
- ----------------SQL Server2000中死锁经验总结 ---------------
- SQL Server2000中死锁经验总结【转】
- SQL Server2000中死锁经验总结 <转>
- SQL Server死锁总结
- SQL Server2000中死锁经验总结
- SQL Server死锁总结
- SQL Server死锁总结
- SQL Server 数据库设计经验总结
- SQL Server2000中死锁经验总结
- [导入]SQL Server2000中死锁经验总结
- SQL SERVER 数据库设计经验总结
- SQL Server 2005 一千万条以上记录分页数据库优化经验总结
- SQL Server死锁总结 [转]
- SQL Server 数据库优化经验总结
- SQL Server2000中死锁经验总结