索引视图上的操作是否并行发生?

bri*_*ian 3 index sql-server-2005 execution-plan materialized-view

当我插入具有聚集索引视图的表时,估计的执行计划似乎并行执行表插入和视图的聚集索引插入操作。

  1. 这是真的?
  2. 为什么 %s 加起来不是 100%?

下面是一个有点人为的例子。

/*
DROP INDEX [IX__AView] ON [dbo].[_AView]  
DROP VIEW _AView
DROP TABLE _A
*/

CREATE TABLE _A (Name VARCHAR(10) NOT NULL)
GO

CREATE VIEW _AView WITH SCHEMABINDING AS 
SELECT Name FROM [dbo]._A
GO

CREATE UNIQUE CLUSTERED INDEX [IX__AView] ON [dbo].[_AView] ([Name] ASC) 
GO

/*
--run the estimated execution on this
INSERT INTO _A (Name) values ('cheese')
*/
Run Code Online (Sandbox Code Playgroud)

索引视图更新

Mar*_*ith 5

它似乎在您的计划中按顺序执行。不是并行的。插入发生在堆中。插入的行被插入到 Eager spool 中,这是一个阻塞操作符。当所有行都插入到基表中时,这些行就会从假脱机插入到视图中。

至于百分比问题,线轴似乎混淆了 SSMS。尽管在计划中出现了两次(相同NodeId),但它的成本应该只计算一次,但在计算计划的总体估计子树成本时,它的成本似乎完全被忽略,然后将操作员百分比显示为这个不正确的总数。

+---------------------+-----------+------------+--------------------------+
|                     |           | % of total | % of total without spool |
+---------------------+-----------+------------+--------------------------+
| Table Insert        | 0.0100022 | 39.84%     | 50.00%                   |
| Spool               | 0.0051012 | 20.32%     | 25.50%                   |
| CX Insert           | 0.010001  | 39.83%     | 49.99%                   |
| Sequence            | 0.000002  | 0.01%      | 0.01%                    |
| Total               | 0.0251064 |            |                          |
| Total without spool | 0.0200052 |            |                          |
+---------------------+-----------+------------+--------------------------+
Run Code Online (Sandbox Code Playgroud)

SQL Sentry Plan Explorer正确处理了这个问题。

计划