我有两个非常简单的表,让我们称它们为[UserData1]和[UserData2]。它们都有[UserId]列作为主键。我正在针对这两个表运行两种类型的查询。一个是SELECT语句,它返回特定用户的组合数据:
SELECT <a subset of columns from both tables>
FROM [UserData1] ud1
FULL OUTER JOIN [UserData2] ud2 ON ud1.[UserId] = ud2.[UserId]
WHERE
ud1.[UserId] = @UserId OR ud2.[UserId] = @UserId
另一个是为特定用户更新两个表中的用户数据的事务:
BEGIN TRANSACTION
UPDATE [UserData1]
SET <new values>
WHERE [UserId] = @UserId
UPDATE [UserData2]
SET <new values>
WHERE [UserId] = @UserId
COMMIT TRANSACTION
这里的问题是SELECT语句中获取共享表锁的顺序是不确定的,如果SQL Server决定在[UserData1]之前锁定[UserData2],这可能(实际上)会导致经典的死锁情况。在这种情况下,避免死锁的最佳方法是什么?
将这些表合并到一个表中,对吗?我希望这很容易。假设有理由将它们分开。
READ UNCOMMITTED / NOLOCK提示?假设不能容忍脏读。
SNAPSHOT隔离级别?这样可以解决问题,但我不确定所涉及的开销。
所以问题归结为:有没有办法保证获取连接表的锁定顺序?
起初我认为这可以通过FORCE ORDER
查询提示来实现,但后来我通过实验发现它不一定强制执行表被锁定的顺序。在这种特殊情况下的另一个解决方案是为每个表发出单独的SELECT查询,然后在应用程序层中组合两个单行记录集,但是如果我需要为多个用户进行查询,我仍然希望得到所有导致一个记录集。
更新:
这是死锁追踪的摘录:
Deadlock encountered .... Printing deadlock information
Wait-for graph
Node:1
KEY: 17:72057594039173120 (e21762ccf3dc) CleanCnt:3 Mode:X Flags: 0x1
Grant List 1:
Owner:0x00000020F75B0480 Mode: X Flg:0x40 Ref:0 Life:02000000 SPID:72 ECID:0 XactLockInfo: 0x00000020EB13ED68
SPID: 72 ECID: 0 Statement Type: UPDATE Line #: 1
Input Buf: Language Event: (@UserId bigint,@DataColumn2 int)update
Requested by:
ResType:LockOwner Stype:'OR'Xdes:0x00000020FC98DA40 Mode: S SPID:75 BatchID:0 ECID:0 TaskProxy:(0x00000020DAB38608) Value:0xf75abbc0 Cost:(0/0)
Node:2
KEY: 17:72057594039107584 (e21762ccf3dc) CleanCnt:9 Mode:S Flags: 0x1
Grant List 1:
Owner:0x00000020EEBFE580 Mode: S Flg:0x40 Ref:1 Life:00000000 SPID:75 ECID:0 XactLockInfo: 0x00000020FC98DA80
SPID: 75 ECID: 0 Statement Type: SELECT Line #: 1
Input Buf: Language Event: (@UserId bigint)select [t].[UserId], t.[DataColumn2], t1.[DataColumn1]
Requested by:
ResType:LockOwner Stype:'OR'Xdes:0x00000020EB13ED28 Mode: X SPID:72 BatchID:0 ECID:0 TaskProxy:(0x0000001F671C6608) Value:0xf75b5400 Cost:(0/456)
Victim Resource Owner:
ResType:LockOwner Stype:'OR'Xdes:0x00000020FC98DA40 Mode: S SPID:75 BatchID:0 ECID:0 TaskProxy:(0x00000020DAB38608) Value:0xf75abbc0 Cost:(0/0)
deadlock-list
deadlock victim=process20fda2ccf8
process-list
process id=process20fda2ccf8 taskpriority=0 logused=0 waitresource=KEY: 17:72057594039173120 (e21762ccf3dc) waittime=4526 ownerId=3416711 transactionname=SELECT lasttranstarted=2013-07-11T18:42:20.943 XDES=0x20fc98da40 lockMode=S schedulerid=20 kpid=2800 status=suspended spid=75 sbid=0 ecid=0 priority=0 trancount=0 lastbatchstarted=2013-07-11T18:42:20.950 lastbatchcompleted=2013-07-11T18:42:20.950 lastattention=1900-01-01T00:00:00.950 clientapp=.Net SqlClient Data Provider hostname=hostname hostpid=27716 loginname=loginname isolationlevel=read committed (2) xactid=3416711 currentdb=17 lockTimeout=4294967295 clientoption1=671088672 clientoption2=128056
executionStack
frame procname=adhoc line=1 stmtstart=36 sqlhandle=0x020000001fcbbe1423a0c65cc8411344c6040e879195af3a0000000000000000000000000000000000000000
select [t].[UserId], t.[DataColumn2], t1.[DataColumn1] from [UserData1] t1 full outer join [UserData2] t on t1.[UserId]=t.[UserId] where t.[UserId]=@UserId or t1.[UserId]=@UserId option (force order)
frame procname=unknown line=1 sqlhandle=0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
unknown
inputbuf
(@UserId bigint)select [t].[UserId], t.[DataColumn2], t1.[DataColumn1] from [UserData1] t1 full outer join [UserData2] t on t1.[UserId]=t.[UserId] where t.[UserId]=@UserId or t1.[UserId]=@UserId option (force order)
process id=process20fd055498 taskpriority=0 logused=456 waitresource=KEY: 17:72057594039107584 (e21762ccf3dc) waittime=4525 ownerId=3416764 transactionname=user_transaction lasttranstarted=2013-07-11T18:42:20.960 XDES=0x20eb13ed28 lockMode=X schedulerid=9 kpid=6024 status=suspended spid=72 sbid=0 ecid=0 priority=0 trancount=2 lastbatchstarted=2013-07-11T18:42:20.970 lastbatchcompleted=2013-07-11T18:42:20.970 lastattention=1900-01-01T00:00:00.970 clientapp=.Net SqlClient Data Provider hostname=hostname hostpid=27716 loginname=loginname isolationlevel=read committed (2) xactid=3416764 currentdb=17 lockTimeout=4294967295 clientoption1=671088672 clientoption2=128056
executionStack
frame procname=adhoc line=1 stmtstart=508 sqlhandle=0x02000000c0d74a32597ec460559a2d5dbdc92f7746cdce270000000000000000000000000000000000000000
update UserData2 set [LastModified]=getutcdate(), [DataColumn2]=[DataColumn2]+@DataColumn2Increment where [UserId]=@UserId
frame procname=unknown line=1 sqlhandle=0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
unknown
inputbuf
(@UserId bigint,@DataColumn2Increment int)update UserData2 set [LastModified]=getutcdate(), [DataColumn2]=[DataColumn2]+@DataColumn2Increment where [UserId]=@UserId
resource-list
keylock hobtid=72057594039173120 dbid=17 objectname=database_name.dbo.UserData1 indexname=1 id=lock20ec75b380 mode=X associatedObjectId=72057594039173120
owner-list
owner id=process20fd055498 mode=X
waiter-list
waiter id=process20fda2ccf8 mode=S requestType=wait
keylock hobtid=72057594039107584 dbid=17 objectname=database_name.dbo.UserData2 indexname=1 id=lock20ec07f600 mode=S associatedObjectId=72057594039107584
owner-list
owner id=process20fda2ccf8 mode=S
waiter-list
waiter id=process20fd055498 mode=X requestType=wait
显然,在[UserData1]之前,运行SELECT语句的进程在[UserData2]表上获取了一个锁,尽管有FORCE ORDER提示。
答案 0 :(得分:1)
使用READ COMMITTED
,select不应该参与死锁,因为它一次只能获得一个锁。读取锁定的行后,可以立即释放锁定。
我非常建议你打开快照隔离。它将解决这个问题。熟悉所涉及的3个开销:增加行大小,tempdb写入和微小的读取开销。大多数时候他们没有意义。
答案 1 :(得分:0)
首先(我认为)是第一个查询中的where子句是多余的。在Join中你有相同的东西,它在连接中更好,因为你正在进行完全外连接。
在避免死锁方面,它可能与删除读锁定方面的第一个查询的处理方式有关。如果应用程序只是读取数据并且这不是用户事务的一部分,那么一旦读取完成,第二个查询就能完成,您将不会遇到死锁。
您是否在您的环境中陷入僵局,或者只是猜测您将陷入僵局。如果您发布了死锁图,那么我们可以看到实际的锁是什么。