Skip to content Skip to sidebar Skip to footer

What Resource Does A Key Lock Actually Lock?

I know a key lock locks a key in an index. However, what does 'key' actually mean? For example, if I have a non-clustered index on a surname column and attempt an update where s

Solution 1:

A keylock affects all rows that match the given predicate (kind of) - in your example all rows with surname = 'Jones' will be affected.

The word "range" is used because depending on the predicate a range of rows may be affected, for example if the predicate was age > 18 then all rows where the age is greater than 18 would be affected.


It is also important to understand that a key lock does not simply individually lock each matching row in the index - your example keylock not only affects all existing rows in the index with surname "Jones", it also affects any attempt to modify an existing row or insert a new row with surname "Jones".

It may help to think about locks in a slightly different way - locks only have an effect when SQL Server attempts to obtain another lock which may be incompatible (i.e. it is not legal to have both locks at the same time). For example if you have an exclusive key-lock on rows with age > 18 and attempt to insert or modify a row with age = 42 then SQL server will first attempt to obtain the relevant locks - seeing that there is an existing key lock for rows with age > 18 SQL Server determines that the locks are incompatible and takes action accordingly.

Post a Comment for "What Resource Does A Key Lock Actually Lock?"