我在默认的"READ COMMITED"事务下,在平坦的普通表上有一个由于简单的SQL UPDATE查询引起的一些有趣的死锁.
UPDATE table SET column=@P1 WHERE PK=@P2
列PK
是varchar(11)
,上面有一个聚簇索引.没有触发器或表关系..表上的.
我做了一些检查,发现死锁发生在"PAGE"级别,而不是ROW /记录级别.然后,我发现对于每个更新查询,它确实需要100(和更多)PAGE锁.(这对我没有意义,因为我一次更新一行)
有没有办法防止发生死锁?或者,如何在不使用游标的情况下减少单行更新所需的锁数?
-
谢谢你的建议.
我曾尝试重建索引几次,填充因子高低.我曾试图让进程更新不同的位置/切片.但没有任何改善或最坏的.
-
我尝试过SQL Server Profiler.我捕获了一些"锁定:死锁链"和"锁定:死锁",但没有捕获到"死锁图".双方都在读取提交,自动提交模式下进行简单的更新查询.
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
Mitch Wheat.. 5
您有2个选项可以减少锁升级:
1)添加WITH(ROWLOCK)提示以要求sql server使用更精细的粒度锁定(您的里程可能有所不同:
UPDATE table WITH(ROWLOCK)SET column = @ P1 WHERE PK = @ P2; 而PK varchar(11)上具有聚簇索引。桌上没有老虎或表的关系..etc。
2)以随机顺序更新行,这减少了将行锁升级为页面锁的可能性。
同样,确保该表上的索引是最新的通常可以减少锁定。如果您要进行很多插入操作,则可以保留填充因子(90很好)。
您有2个选项可以减少锁升级:
1)添加WITH(ROWLOCK)提示以要求sql server使用更精细的粒度锁定(您的里程可能有所不同:
UPDATE table WITH(ROWLOCK)SET column = @ P1 WHERE PK = @ P2; 而PK varchar(11)上具有聚簇索引。桌上没有老虎或表的关系..etc。
2)以随机顺序更新行,这减少了将行锁升级为页面锁的可能性。
同样,确保该表上的索引是最新的通常可以减少锁定。如果您要进行很多插入操作,则可以保留填充因子(90很好)。