4

在默认的“READ COMMITED”事务下,在一个简单的普通表上,一个愚蠢的简单 SQL UPDATE 查询导致了一些有趣的死锁。

UPDATE table SET column=@P1 WHERE PK=@P2

PKvarchar(11),上面有一个聚集索引。表上没有触发器或表关系..等。

我做了一些检查,发现死锁发生在“PAGE”级别,而不是 ROW/记录级别。然后,我发现对于每个更新查询,确实需要 100 个(甚至更多)页锁。(这对我来说没有意义,因为我一次更新一行)

有什么办法可以防止死锁的发生吗?或者,如何在不使用游标的情况下减少单行更新所需的锁数?

--

谢谢你的建议。

我曾尝试用高填充因子和低填充因子重建索引几次。我曾试图让进程更新不同的位置/切片。但没有任何改善或最坏的情况。

--

我尝试了 SQL Server Profiler。我捕获了一些“Lock:Deadlock Chain”和“Lock:Deadlock”,但没有捕获“Deadlock Graph”。双方都在读取提交、自动提交模式下进行简单的更新查询。

Lock:Deadlock Chain 17887475    1       0X01    4   myserver        2008-11-28 10:16:46.210 Parallel query worker thread was involved in a deadlock                                                                 0           971497  102 - Resource type Exchange                    Lock:Deadlock Chain 17887476    1       0X01    4   myserver        2008-11-28 10:16:46.210 Deadlock Chain SPID = 209 1:438102                                                                                                                                                                                                                                                          265006271       0   0X56AF060001000000000000001B0006        27              0 - LOCK    4 - U           0   72057594040352768   1 - TRANSACTION 0   6 - PAGE    mydatabase  971497  101 - Resource type Lock                
Lock:Deadlock Chain 17887477    1       0X01    4   myserver        2008-11-28 10:16:46.210 Parallel query worker thread was involved in a deadlock                                                                 0           971497  102 - Resource type Exchange                    Lock:Deadlock Chain 17887478    1       0X01    4   myserver        2008-11-28 10:16:46.210 Deadlock Chain SPID = 54 1:426206                                                                                                                                                                                                                                                           265006240       0   0XDE80060001000000000000001B0006        27              0 - LOCK    4 - U           0   72057594040352768   1 - TRANSACTION 0   6 - PAGE    mydatabase  971497  101 - Resource type Lock                
Lock:Deadlock Chain 17887479    1       0X01    4   myserver        2008-11-28 10:16:46.210 Deadlock Chain SPID = 209 1:426206                                                                                                                                                                                                                                                          265006271       0   0XDE80060001000000000000001B0006        27              0 - LOCK    4 - U           0   72057594040352768   1 - TRANSACTION 0   6 - PAGE    mydatabase  971497  101 - Resource type Lock                
Lock:Deadlock Chain 17887480    1       0X01    4   myserver        2008-11-28 10:16:46.210 Parallel query worker thread was involved in a deadlock                                                                 0           971497  102 - Resource type Exchange                    Lock:Deadlock Chain 17887481    1       0X01    4   myserver        2008-11-28 10:16:46.210 Deadlock Chain SPID = 54 1:426066                                                                                                                                                                                                                                                           265006240       0   0X5280060001000000000000001B0006        27              0 - LOCK    4 - U           0   72057594040352768   1 - TRANSACTION 0   6 - PAGE    mydatabase  971497  101 - Resource type Lock                
Lock:Deadlock Chain 17887482    1       0X01    4   myserver        2008-11-28 10:16:46.210 Deadlock Chain SPID = 209 1:426066                                                                                                                                                                                                                                                          265006271       0   0X5280060001000000000000001B0006        27              0 - LOCK    4 - U           0   72057594040352768   1 - TRANSACTION 0   6 - PAGE    mydatabase  971497  101 - Resource type Lock                
Lock:Deadlock Chain 17887483    1       0X01    4   myserver        2008-11-28 10:16:46.210 Parallel query worker thread was involved in a deadlock                                                                 0           971497  102 - Resource type Exchange                    Lock:Deadlock Chain 17887484    1       0X01    4   myserver        2008-11-28 10:16:46.210 Deadlock Chain SPID = 209 1:425614                                                                                                                                                                                                                                                          265006271       0   0X8E7E060001000000000000001B0006        27              0 - LOCK    4 - U           0   72057594040352768   1 - TRANSACTION 0   6 - PAGE    mydatabase  971497  101 - Resource type Lock                
Lock:Deadlock Chain 17887485    1       0X01    4   myserver        2008-11-28 10:16:46.210 Parallel query worker thread was involved in a deadlock                                                                 0           971497  102 - Resource type Exchange                    Lock:Deadlock Chain 17887486    1       0X01    4   myserver        2008-11-28 10:16:46.210 Deadlock Chain SPID = 209 1:426687                                                                                                                                                                                                                                                          265006271       0   0XBF82060001000000000000001B0006        27              0 - LOCK    4 - U           0   72057594040352768   1 - TRANSACTION 0   6 - PAGE    mydatabase  971497  101 - Resource type Lock                
Lock:Deadlock Chain 17887487    1       0X01    4   myserver        2008-11-28 10:16:46.210 Parallel query worker thread was involved in a deadlock                                                                 0           971497  102 - Resource type Exchange                
Lock:Deadlock Chain 17887488    1       0X01    4   myserver        2008-11-28 10:16:46.210 Deadlock Chain SPID = 209 1:425392                                                                                                                                                                                                                                                          265006271       0   0XB07D060001000000000000001B0006        27              0 - LOCK    4 - U           0   72057594040352768   1 - TRANSACTION 0   6 - PAGE    mydatabase  971497  101 - Resource type Lock                
Lock:Deadlock Chain 17887489    1       0X01    4   myserver        2008-11-28 10:16:46.210 Parallel query worker thread was involved in a deadlock                                                                 0           971497  102 - Resource type Exchange                
Lock:Deadlock Chain 17887491    1       0X01    4   myserver        2008-11-28 10:16:46.210 Parallel query worker thread was involved in a deadlock                                                                 0           971497  102 - Resource type Exchange                
Lock:Deadlock Chain 17887493    1       0X01    4   myserver        2008-11-28 10:16:46.210 Parallel query worker thread was involved in a deadlock                                                                 0           971497  102 - Resource type Exchange                
Lock:Deadlock Chain 17887494    1       0X01    4   myserver        2008-11-28 10:16:46.210 Deadlock Chain SPID = 209 1:435792                                                                                                                                                                                                                                                          265006271       0   0X50A6060001000000000000001B0006        27              0 - LOCK    4 - U           0   72057594040352768   1 - TRANSACTION 0   6 - PAGE    mydatabase  971497  101 - Resource type Lock                
Lock:Deadlock Chain 17887495    1       0X01    4   myserver        2008-11-28 10:16:46.210 Parallel query worker thread was involved in a deadlock                                                                 0           971497  102 - Resource type Exchange                
Lock:Deadlock Chain 17887496    1       0X01    4   myserver        2008-11-28 10:16:46.210 Deadlock Chain SPID = 209 1:438206                                                                                                                                                                                                                                                          265006271       0   0XBEAF060001000000000000001B0006        27              0 - LOCK    4 - U           0   72057594040352768   1 - TRANSACTION 0   6 - PAGE    mydatabase  971497  101 - Resource type Lock                
Lock:Deadlock   17887497        myuser  0XCD85FBB269700B4AA2F4E8579D118999  209 myserver    myuser  2008-11-28 10:16:45.930 1:426206    265006271   myapps  0   0XDE80060001000000000000001B0006    123 27  281 2008-11-28 10:16:46.210 myclient    0 - LOCK    4 - U           0   72057594040352768   1 - TRANSACTION 0   6 - PAGE    mydatabase  971498                  
4

6 回答 6

5

您有 2 个选项来减少锁升级:

1)添加 WITH (ROWLOCK) 提示以要求 sql server 采用更细粒度的锁(您的里程可能会有所不同:

UPDATE table WITH (ROWLOCK) SET column=@P1 WHERE PK=@P2; 而 PK varchar(11) 上有一个聚集索引。桌子上没有跳跳虎或桌子关系..等。

2) 以随机顺序更新行,这降低了行锁升级为页锁的可能性。

Also, ensuring that the indexes on that table are up to date can often reduce locking. As can leaving a Fill factor (90 is good) if you are going to be doing lots of inserts.

于 2008-11-26T15:17:57.360 回答
1

Have you run a profile trace?

Fire up SQL Profiler and create a standard trace with these events added:

  • Locks:Deadlock Graph
  • Locks:Lock:Deadlock Chain
  • Locks:Lock:Escalation

Should should provide details of the precise nature of the deadlock.

于 2008-11-27T03:43:21.147 回答
0

In the normal, simple case this type of begavior is not often seen. My question to you is this : what's on the 'other side' of this transaction? What's the other update statement that is running and causing this deadlock? That, I think, will be the key to diagnosing this problem. Honestly, my money is on this other, heretofore unidentified query being the culprit. And I'm in vegas now...

于 2008-11-26T16:30:48.383 回答
0

What select statements from same table AND same records do occur inside the same transaction before update statement? Use (updlock) locking hints in these selects.

于 2008-11-27T15:15:22.950 回答
0

do you have any UPDATE TRIGGERS on the table? If so, the action of the trigger, might be causing your deadlock.

于 2008-12-29T17:00:27.337 回答
0

I finally have to do a workaround by using cusror in a stored procedure.

But it is still interesting that how the PAGE lock happen and how to resolve.


After some more search on Google, there are some other people have the same problem and they(from MSDN forum) suggest to turn off the parallelism in SQL Server 2005 but I never get a chance to try.

于 2009-01-22T15:41:32.993 回答