简体   繁体   English

为什么这会触发更改我的查询速度?

[英]Why did this trigger change speed my query up?

I've been trying to solve a slow trigger problem and now that I have through trial and error, I still don't know what the original problem was. 我一直试图解决一个缓慢的触发问题,现在我已经通过反复试验,我仍然不知道原来的问题是什么。

The query I'm running is the following: 我正在运行的查询如下:

UPDATE tblA 
SET X = NULL
WHERE X IS NOT NULL AND Z = 0

It updates around 30k rows. 它更新大约30k行。

And the part of the AFTER INSERT, UPDATE trigger on tblA causing the problem was this: 并且tblA上的AFTER INSERT,UPDATE触发器导致问题的部分是这样的:

IF EXISTS(SELECT 1
          FROM inserted
          LEFT JOIN deleted ON deleted.PK = inserted.PK
          WHERE (inserted.Y IS NOT NULL AND deleted.Y IS NULL)
              OR inserted.Y <> deleted.Y
BEGIN

    -- The above condition is not met for my query so we would never get here
    INSERT INTO tblB
    (...)
    SELECT
    inserted.X,
    ...
    FROM
    inserted
    LEFT JOIN deleted ON deleted.PK = inserted.PK
    WHERE (inserted.Y IS NOT NULL AND deleted.Y IS NULL)
        OR inserted.Y <> deleted.Y

END

I believe the above IF EXISTS was included to stop potential looping INSERT triggers from firing when no inserts actually happened, but that isn't actually a problem for tblB as it only has one trigger. 我相信上面的IF EXISTS是为了阻止潜在的循环INSERT触发器在没有插入实际发生时触发,但这实际上不是tblB的问题,因为它只有一个触发器。

So I changed it to this: 所以我改成了这个:

INSERT INTO tblB
(...)
SELECT
inserted.X,
...
FROM
inserted
LEFT JOIN deleted ON deleted.PK = inserted.PK
WHERE (inserted.Y IS NOT NULL AND deleted.Y IS NULL)
    OR inserted.Y <> deleted.Y

And the update query time has now gone down from > 1 hour to around 30 seconds. 并且更新查询时间现已从> 1小时降至约30秒。

I expected it to take exactly the same amount of time. 我预计它会花费相同的时间。 Why is it faster? 为什么它更快?

UPDATE: After examining execution plan for running my update query with the slow trigger 更新:检查使用慢速触发器运行更新查询的执行计划

The IF EXISTS check had a cost of 0%, with 73% of the cost going to another trigger's statement which inserts changes into an audit table. IF EXISTS检查的成本为0%,其中73%的成本转到另一个触发器语句,该语句将更改插入到审计表中。 This doesn't seem unreasonable in itself as that statement is quite complex with lots of joins, but I am none the wiser as to why my change to rewrite the IF EXISTS has made any difference. 这本身似乎并不合理,因为该语句在很多连接中非常复杂,但我并不清楚为什么我改写IF EXISTS的改变有所不同。 Perhaps my IF EXISTS check is interfering with the audit table insertions somehow to slow them down, but I don't know why the new version doesn't do the same thing as it contains the same SELECT. 也许我的IF EXISTS检查干扰了审计表插入以某种方式减慢它们,但我不知道为什么新版本不会做同样的事情,因为它包含相同的SELECT。 [Most of this cost was going to an eager table spool.] [大部分成本都流向了一个急切的表盘。]

Another 13% of query cost is spent on a third trigger which updates the timestamp on tblA if particular column values have changed. 另外13%的查询成本花费在第三个触发器上,如果特定列值已更改,则更新tblA上的时间戳。 This again joins on inserted and deleted, plus on tblA. 这再次加入插入和删除,加上tblA。 This update statement would have had no effect for my query as column X changes are not worthy of updating the timestamp. 此更新语句对我的查询没有任何影响,因为列X更改不值得更新时间戳。 [This cost was split between a hash match inner join between tblA and inserted, and a clustered index update - seems reasonable.] [此成本在tblA和插入之间的哈希匹配内部联接和聚集索引更新之间分配 - 似乎是合理的。]

To add more confusion: if I disable the trigger that cost 73% of the time but leave the old trigger mentioned above in place without my changes, my query still takes many hours to run. 添加更多混淆:如果我禁用触发器,该触发器花费73%的时间但是在没有我的更改的情况下保留上面提到的旧触发器,我的查询仍然需要花费数小时才能运行。 I haven't tried disabling the timestamp trigger. 我还没有尝试禁用时间戳触发器。

Looking at the query plan when using the fast trigger, the ratios are almost exactly the same, but the overall time is just less. 在使用快速触发器时查看查询计划,比率几乎完全相同,但总体时间更短。

Please investigate the execution plan and see what are the differences between each runs. 请调查执行计划,看看每次运行之间有什么区别。 I guess SQL-server uses a different execution plan for your exists(...) query than for insert-select as it doesn't have to reach for all the columns in the first case. 我猜SQL-server对exists(...)查询使用的执行计划与insert-select不同,因为它不必覆盖第一种情况下的所有列。 If there are confusing indexes or confusing statistics, optimization may get confused and pick a really bad plan. 如果存在令人困惑的索引或令人困惑的统计数据,优化可能会混淆并选择一个非常糟糕的计划。 For this reason, after you investigate and save execution plans, try to reorganize/rebuild all indexes and recompute statistics on that table. 因此,在调查并保存执行计划后,尝试重新组织/重建所有索引并重新计算该表上的统计信息。

Regards, Rob 问候,Rob

well, i'm not really sure what happened between the two, but i can offer you a couple of tips to speed it up more 好吧,我不确定两者之间发生了什么,但我可以提供一些技巧来加快速度

the first thing i would change is this: 我要改变的第一件事是:

WHERE (inserted.Y IS NOT NULL AND deleted.Y IS NULL) 

to this: 对此:

WHERE (inserted.Y >'' AND deleted.Y IS NULL) 

IS NULL causes an index seek, where as >'' allows sql do to a seek and giving you the same result set (depending on if y is an int, if it's a varchar then you might change to >='') IS NULL导致索引搜索,其中as'''允许sql执行搜索并为您提供相同的结果集(取决于y是否为int,如果它是varchar,那么您可能会更改为> ='')

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

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