在SQL Server中,复合主键是否会增加死锁的可能性?

我想知道,或者是因为增加了行锁定,还是因为增加了持有锁的时间,在表上定义了复合主键会增加在更新时遇到死锁的可能性一旦?

感谢您的任何帮助.

解决方法

如果您正在使用复合PK并且并行插入大量数据,则可能会受到资源哈希冲突的影响.有关真实示例,请参见 “The Curious Case of the Dubious Deadlock and the Not So Logical Lock”.

对于资源哈希冲突的解释,我将在“%%lockres%% collision probability magic marker: 16,777,215”引用Remus Rusanu(推荐阅读):

The lock manager in sql Server doesn’t kNow what it locks,it just locks ‘resources’ (basically strings). It is the job of higher level components like the the access methods of the storage engine to present the ‘resource’ to the lock manager and ask for the desired lock. When locking rows in a heap or a b-tree the storage engine will synthesize a ‘resource’ from the record identifier. Since these resources have a limited length,the storage engine has to reduce the effective length of a key to the maximum length is allowed to present to the lock manager,and that means that the record’s key will be reduced to 6 bytes. This is achieved by hashing the key into a 6 byte hash value.

[…]

On 6 bytes there are 281,474,976,710,656 distinct possible values. Its a pretty big number? Not that big actually. […] So the sql %%lockres%% hash will produce two records with same hash,with a 50% probability,out of the table,any table,of only 16,215 record.

相关文章

SELECT a.*,b.dp_name,c.pa_name,fm_name=(CASE WHEN a.fm_n...
if not exists(select name from syscolumns where name=&am...
select a.*,pano=a.pa_no,b.pa_name,f.dp_name,e.fw_state_n...
要在 SQL Server 2019 中设置定时自动重启,可以使用 Window...
您收到的错误消息表明数据库 'EastRiver' 的...
首先我需要查询出需要使用SQL Server Profiler跟踪的数据库标...