使用INSTEAD OF DELETE触发超时

时间:2011-08-31 00:07:11

标签: sql-server sql-server-2008 triggers

我遇到了以下问题,而不是SQL Server 2008 R2下的删除触发器。只有在同一交易中删除了许多(> 6个左右)CustomFieldOption时才会出现这种情况。

CREATE TRIGGER dbo.[DeleteCustomFieldOptionInsteadOfTrigger] ON dbo.[CustomFieldOption] INSTEAD OF DELETE
AS

BEGIN

    SET NOCOUNT ON;

    UPDATE ucf  SET ucf.PendingCustomFieldOptionIdValue = NULL, ucf.PendingProposedStateId = NULL
                FROM UserCustomField ucf
    INNER JOIN deleted d ON d.CustomFieldOptionId = ucf.PendingCustomFieldOptionIdValue
    WHERE ucf.CurrentCustomFieldOptionIdValue IS NOT NULL;

    SET NOCOUNT ON;

    DELETE ucf FROM UserCustomField ucf
                INNER JOIN deleted d ON d.CustomFieldOptionId = ucf.PendingCustomFieldOptionIdValue
    WHERE ucf.CurrentCustomFieldOptionIdValue IS NULL;

    SET NOCOUNT ON;

    DELETE ucf FROM UserCustomField ucf
                INNER JOIN deleted d ON d.CustomFieldOptionId = ucf.CurrentCustomFieldOptionIdValue;

    SET NOCOUNT ON;


    DELETE cfo FROM CustomFieldOption cfo
    INNER JOIN deleted d ON d.CustomFieldOptionId = cfo.CustomFieldOptionId;

    SET NOCOUNT OFF;

END

我意识到可能需要进一步的背景(我会根据需要更新问题) - 我在这里做了一些明显错误的事情吗?

2 个答案:

答案 0 :(得分:2)

我首先要确定您是否遇到死锁:How to Track Down Deadlocks Using SQL Server 2005 Profiler(也适用于SQL Server 2008 R2)

[顺便说一下:每个存储过程只需要一个SET NOCOUNT ON;实例]

其次,(根据@ Martin的评论)你对这些表有什么索引?正确(覆盖)索引可以对整体性能和阻塞产生巨大影响。

答案 1 :(得分:1)

您的表是否具有适用于您正在删除的联接的索引?这些陈述的执行计划是什么样的?