为什么此触发器更改会加快我的查询速度?

发布于 2024-09-27 19:14:29 字数 1779 浏览 1 评论 0原文

我一直在尝试解决缓慢触发的问题,现在我已经通过反复试验,我仍然不知道原来的问题是什么。

我正在运行的查询如下:

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

它更新了大约 30k 行。

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

我相信上面的 IF EXISTS 是为了阻止潜在的循环 INSERT 触发器在没有实际发生插入时触发,但这实际上并不是 tblB 的问题因为它只有一个触发器。

所以我把它改成了这样:

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

更新查询时间现在已经从 > 下降到了 > 。 1小时到30秒左右。

我预计它会花费完全相同的时间。为什么更快?

更新:检查使用慢速触发器运行更新查询的执行计划后

IF EXISTS 检查的成本为 0%,其中 73% 的成本进入另一个触发器的语句,该语句将更改插入审核桌子。这本身似乎并不是不合理的,因为该语句非常复杂,有很多连接,但我不知道为什么我重写 IF EXISTS 的更改会产生任何影响。也许我的 IF EXISTS 检查会以某种方式干扰审计表插入,从而减慢它们的速度,但我不知道为什么新版本不执行相同的操作,因为它包含相同的 SELECT。 [大部分成本都花在了急切表假脱机上。]

另外 13% 的查询成本花在第三个触发器上,如果特定列值发生更改,该触发器会更新 tblA 上的时间戳。这再次连接插入和删除,以及 tblA。此更新语句对我的查询没有任何影响,因为 X 列更改不值得更新时间戳。 [此成本分为 tblA 和插入之间的哈希匹配内部联接和聚集索引更新 - 似乎是合理的。]

增加更多混乱:如果我禁用花费 73% 时间的触发器,但保留上面提到的旧触发器如果没有我的更改,我的查询仍然需要很多小时才能运行。我还没有尝试禁用时间戳触发器。

查看使用快速触发器时的查询计划,比率几乎完全相同,但总体时间更少。

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.

And the part of the AFTER INSERT, UPDATE trigger on tblA causing the problem was this:

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.

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.

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. 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. 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.
[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. This again joins on inserted and deleted, plus on tblA. This update statement would have had no effect for my query as column X changes are not worthy of updating the timestamp.
[This cost was split between a hash match inner join between tblA and inserted, and a clustered index update - seems reasonable.]

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. 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

听闻余生 2024-10-04 19:14:29

请调查执行计划并查看每次运行之间有什么差异。我猜想 SQL-server 对您的 contains(...) 查询使用与 insert-select 不同的执行计划,因为它不必到达第一种情况下的所有列。如果存在令人困惑的索引或令人困惑的统计数据,优化可能会感到困惑并选择一个非常糟糕的计划。因此,在调查并保存执行计划后,请尝试重新组织/重建该表的所有索引并重新计算统计信息。

问候,罗布

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. 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

山有枢 2024-10-04 19:14:29

好吧,我不太确定两者之间发生了什么,但我可以为您提供一些技巧来加快速度,

我要更改的第一件事是:

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

对此:

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

IS NULL 会导致索引查找,其中 > ;'' 允许 sql 进行查找并给出相同的结果集(取决于 y 是否为 int,如果它是 varchar 那么您可能会更改为 >='')

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 >='')

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文