我将从一个非常简单的示例开始:两个表都具有相同的模式,它们都聚集在PK上,但是其中一个具有INSTEAD OF UPDATE
触发器:
CREATE TABLE Standard
(
PK UNIQUEIDENTIFIER PRIMARY KEY CLUSTERED,
V INT NOT NULL
)
GO
CREATE TABLE InsteadOf
(
PK UNIQUEIDENTIFIER PRIMARY KEY CLUSTERED,
V INT NOT NULL
)
GO
INSERT Standard (PK, V) VALUES ('1E58B555-B073-471E-B576-4B09C8E18976', 0)
INSERT InsteadOf (PK, V) VALUES ('1E58B555-B073-471E-B576-4B09C8E18976', 0)
GO
CREATE TRIGGER TR_InsteadOf_Update ON InsteadOf INSTEAD OF UPDATE
AS
BEGIN
DECLARE @PK UNIQUEIDENTIFIER
DECLARE @V INT
DECLARE @cursor CURSOR
SET @cursor = CURSOR FOR SELECT PK, V FROM Inserted
OPEN @cursor
FETCH NEXT FROM @cursor INTO @PK, @V
WHILE @@FETCH_STATUS = 0
BEGIN
UPDATE InsteadOf SET
V = @V
WHERE PK = @PK
FETCH NEXT FROM @cursor INTO @PK, @V
END
CLOSE @cursor
DEALLOCATE @cursor
END
GO
如果查看查询计划以针对标准表进行更新,则会得到预期的聚集索引更新:
UPDATE Standard SET
V = 1
WHERE PK = '1E58B555-B073-471E-B576-4B09C8E18976'
但是,如果我使用触发器对表执行类似的更新,则会得到似乎是聚集索引插入以及聚集索引更新的内容:
UPDATE InsteadOf SET
V = 1
WHERE PK = '1E58B555-B073-471E-B576-4B09C8E18976'
为什么是这样?我可以看到稍后在此查询计划(查询#4)中期望的聚集索引更新,但是为什么在查询#1处得到此额外的插入?