简体   繁体   English

更新基表中的任何字段会导致更新索引视图行的所有行吗?

[英]Will updating any field in base table cause updating will all rows of the indexed view rows?

Will updating any field in base table cause the auto maintain of the indexed view, even this field isn't included in the definition of the the indexed view?更新基表中的任何字段是否会导致索引视图的自动维护,即使该字段不包含在索引视图的定义中?

Or even worse, will all rows of the indexed view be rewritten or will only the rows that are affected be updated?或者更糟糕的是,索引视图的所有行都会被重写还是只会更新受影响的行?

No, it only updates when it needs to.不,它只在需要时更新。 Here's an example:这是一个例子:

USE tempdb;
GO

DROP VIEW IF EXISTS dbo.TestValueOnly;
GO

DROP TABLE IF EXISTS dbo.Test;
GO

CREATE TABLE dbo.Test
(
    TestID int IDENTITY(1,1) NOT NULL 
        CONSTRAINT PK_dbo_Test PRIMARY KEY,
    TestName varchar(50) NOT NULL,
    ValueToChange int NOT NULL
);
GO

INSERT dbo.Test (TestName, ValueToChange)
VALUES ('Hello', 12),
       ('There', 14),
       ('And', 16),
       ('Again', 18);
GO

CREATE VIEW dbo.TestValueOnly
WITH SCHEMABINDING
AS
SELECT TestID, ValueToChange 
FROM dbo.Test;
GO

CREATE UNIQUE CLUSTERED INDEX CX_dbo_TestValueOnly
ON dbo.TestValueOnly (TestID);
GO

Now look at the query plan of these two queries:现在看看这两个查询的查询计划:

UPDATE dbo.Test 
SET TestName = 'Changed' 
WHERE TestID = 14;

UPDATE dbo.Test 
SET ValueToChange = 17 
WHERE TestID = 14;

在此处输入图像描述

The first just updates the base table as expected and doesn't touch the indexed view.第一个只是按预期更新基表并且不触及索引视图。 The second hits the indexed view as well, again as expected.第二个也像预期的那样击中了索引视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM