为什么对分区视图执行删除会导致聚集索引插入?


9

我有一个分区视图,具有下面的插入触发器(可怜的芒分区)。当我执行DELETE时,我得到以下查询计划:

delete from factproductprice where pricedate = '20170725'

在此处输入图片说明

在视图上触发:

ALTER TRIGGER [dbo].[factProductPriceDelete] ON [dbo].[FactProductPrice]
INSTEAD OF DELETE AS
BEGIN
  IF @@ROWCOUNT = 0 RETURN;

  DECLARE @PriceDate DATE
  SELECT @PriceDate = CAST(PriceDate AS DATE) FROM DELETED
  IF @PriceDate BETWEEN '20140101' AND '20141231'
  BEGIN
    DELETE FROM dbo.FactProductPrice2014
    WHERE ProductId IN (SELECT ProductId FROM DELETED) AND SalesPriceSchemeId IN (SELECT SalesPriceSchemeId FROM DELETED) AND PriceDate IN (SELECT PriceDate FROM DELETED) 
  END
  IF @PriceDate BETWEEN '20150101' AND '20151231'
  BEGIN
    DELETE FROM dbo.FactProductPrice2015
    WHERE ProductId IN (SELECT ProductId FROM DELETED) AND SalesPriceSchemeId IN (SELECT SalesPriceSchemeId FROM DELETED) AND PriceDate IN (SELECT PriceDate FROM DELETED) 
  END
  IF @PriceDate BETWEEN '20160101' AND '20161231'

....等

Answers:


11

INSTEAD OF 触发器完全取代了触发动作。

插入删除的伪表表示将已进行了更改,有触发语句实际执行。行版本控制不能用于这些触发器,因为根据定义,尚未进行任何修改。

INSTEAD OF触发器存在时,SQL Server修改触发器DML语句的执行计划。执行计划不是直接修改受影响的表,而是将有关更改的信息写入隐藏的工作表。

该工作表包含执行原始更改所需的所有数据,在每一行(删除或插入)上执行的修改的类型以及OUTPUT子句的触发器所需的任何信息。

执行计划中的插入表示对该隐藏工作表的写入。当捕获该语句的执行后计划时,您将看到该隐藏的工作表被用作已删除和已插入的伪表。

请参阅我的SQLPerformance.com文章“ 关于INSTEAD OF触发器的有趣事情”

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.