如果更新....Where 不影响任何行,是否会创建任何锁?

发布于 2024-11-25 15:22:29 字数 168 浏览 1 评论 0原文

如果我运行类似 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

时常饿 2024-12-02 15:22:29

您可以使用此脚本并亲自查看有时即使没有更新任何行也会获取并保留锁:

CREATE TABLE dbo.Test
    (
      i INT NOT NULL
            PRIMARY KEY ,
      j INT NULL
    ) ;
go

INSERT  dbo.Test
        ( i, j )
VALUES  ( 1, 2 ) ;
GO

SELECT  @@spid ;

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE ;

BEGIN TRANSACTION ;

UPDATE  dbo.Test
SET     j = 3
WHERE   i = 3 ;

SELECT  *
FROM    sys.dm_tran_locks 
WHERE request_session_id = @@spid;

COMMIT ;

You can play with this script and see for yourself that sometimes locks are acquired and held even when no rows are updated:

CREATE TABLE dbo.Test
    (
      i INT NOT NULL
            PRIMARY KEY ,
      j INT NULL
    ) ;
go

INSERT  dbo.Test
        ( i, j )
VALUES  ( 1, 2 ) ;
GO

SELECT  @@spid ;

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE ;

BEGIN TRANSACTION ;

UPDATE  dbo.Test
SET     j = 3
WHERE   i = 3 ;

SELECT  *
FROM    sys.dm_tran_locks 
WHERE request_session_id = @@spid;

COMMIT ;
路还长,别太狂 2024-12-02 15:22:29

如果字段 x 已建立索引,那么当您的 UPDATE 检查匹配记录时,该索引上可能会存在共享锁。

不应有任何行锁,但所有锁定行为都取决于您的服务器级别隔离设置。

If field x is indexed, then there will probably be a shared lock on that index while your UPDATE 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.

过期情话 2024-12-02 15:22:29

如果使用不影响记录的更新语句,那么在事务中,将针对更新语句采取独占意向锁,首先将选择受影响的行,然后更新表,但是因为没有需要更新的行此意向锁是在事务的表上以独占模式获取的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文