我有一个存储过程来更新表,并且我将隔离级别设置为READ UNCOMMITTED。在此示例中,我设置了注释计数(CommentCount + = 1)。如果同时有一个以上的用户调用此sp,则注释计数的增加量是否可能少于添加注释的用户数?
答案 0 :(得分:1)
SQL Server仍以隔离级别READ UNCOMMITTED
获取对更新行的锁定。像这样的UPDATE
语句在同时由多个READ UNCOMMITTED
会话执行时不会丢失增量:
UPDATE dbo.Post
SET CommentCount += 1
WHERE PostID = @PostID;
这是此语句的锁痕,由PostID上的群集主键更新。排他锁将阻止该行的其他并发更新。
+---------------+----+--------------+----------+--+
| Lock:Acquired | 58 | 5 - OBJECT | 8 - IX | |
| Lock:Acquired | 58 | 6 - PAGE | 8 - IX | |
| Lock:Acquired | 58 | 7 - KEY | 5 - X | |
| Lock:Released | 58 | 7 - KEY | 0 - NULL | |
| Lock:Released | 58 | 6 - PAGE | 0 - NULL | |
| Lock:Released | 58 | 7 - KEY | 5 - X | |
| Lock:Released | 58 | 6 - PAGE | 8 - IX | |
| Lock:Released | 58 | 5 - OBJECT | 8 - IX | |
+---------------+----+--------------+----------+--+
这是使用非聚集主键索引的行所在位置的跟踪信息。非群集键上的更新锁将序列化同一键的其他更新语句,而群集键上的排他锁将阻止其他数据修改。
+---------------+----+------------+----------+--+
| Lock:Acquired | 58 | 5 - OBJECT | 8 - IX | |
| Lock:Acquired | 58 | 6 - PAGE | 7 - IU | |
| Lock:Acquired | 58 | 7 - KEY | 4 - U | |
| Lock:Acquired | 58 | 6 - PAGE | 7 - IU | |
| Lock:Acquired | 58 | 7 - KEY | 4 - U | |
| Lock:Acquired | 58 | 6 - PAGE | 8 - IX | |
| Lock:Acquired | 58 | 7 - KEY | 5 - X | |
| Lock:Released | 58 | 7 - KEY | 0 - NULL | |
| Lock:Released | 58 | 6 - PAGE | 0 - NULL | |
| Lock:Released | 58 | 7 - KEY | 4 - U | |
| Lock:Released | 58 | 6 - PAGE | 7 - IU | |
| Lock:Released | 58 | 7 - KEY | 5 - X | |
| Lock:Released | 58 | 6 - PAGE | 8 - IX | |
| Lock:Released | 58 | 5 - OBJECT | 8 - IX | |
+---------------+----+------------+----------+--+