如果更新....Where 不影响任何行,是否会创建任何锁?
如果我运行类似 And no rows match,因此没有行更改的 SQL 语句
UPDATE table
SET col = value
WHERE X=Y
,更新是否会创建任何锁?
DBMS是Sybase + SQL Server
If I run a SQL statement like
UPDATE table
SET col = value
WHERE X=Y
And no rows match, therefore no rows are changed, are any locks created by the update?
The DBMS is Sybase + SQL Server
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用此脚本并亲自查看有时即使没有更新任何行也会获取并保留锁:
You can play with this script and see for yourself that sometimes locks are acquired and held even when no rows are updated:
如果字段
x
已建立索引,那么当您的UPDATE
检查匹配记录时,该索引上可能会存在共享锁。不应有任何行锁,但所有锁定行为都取决于您的服务器级别隔离设置。
If field
x
is indexed, then there will probably be a shared lock on that index while yourUPDATE
is checking it for matching records.There should not be any row locks, but all locking behavior is contingent on your server-level isolation settings.
如果使用不影响记录的更新语句,那么在事务中,将针对更新语句采取独占意向锁,首先将选择受影响的行,然后更新表,但是因为没有需要更新的行此意向锁是在事务的表上以独占模式获取的。
In case an update statement is used which does not effect the records then an exclusive intent lock is being taken for the update statement while in transaction as first the rows effected are to be selected followed by the update on the table, however as there are no rows that need to be updated this intent lock is taken on the table for the transaction in an exclusive mode.