我在想。 我有这个Trigger,它完全符合我的要求。
USE [IGOR]
GO
/****** Object: Trigger [dbo].[AfterInsertTarget] Script Date: 11/20/2013 14:46:06 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[AfterInsertTarget] ON [dbo].[DMM_TARGET]
after insert
as
set nocount on
update DMM_TARGET
set ID_WEEK = 0
WHERE FREQ = 'Daily'
我的问题是:如果仅在插入的行上FREQ ='Daily',更新语句是否将ID_WEEK设置为0?或者它是否通过整个表循环?因为现在它只有50000个条目。但是,如果它变得像数百万,那么由于触发器,只需要一段时间才能插入一行。
答案 0 :(得分:2)
您的触发器当前更新所有行,因为与inserted
伪表没有关联,伪表只包含触发器调用所涉及的记录。仅影响新行:
ALTER TRIGGER [dbo].[AfterInsertTarget]
ON [dbo].[DMM_TARGET]
AFTER INSERT
AS
BEGIN
SET NOCOUNT ON;
UPDATE t SET ID_WEEK = 0
FROM dbo.DMM_TARGET AS t
WHERE EXISTS (SELECT 1 FROM inserted WHERE key = t.key);
END
GO
或者:
ALTER TRIGGER [dbo].[AfterInsertTarget]
ON [dbo].[DMM_TARGET]
AFTER INSERT
AS
BEGIN
SET NOCOUNT ON;
UPDATE t SET ID_WEEK = 0
FROM dbo.DMM_TARGET AS t
INNER JOIN inserted AS i
ON t.key = t.key;
END
GO
但是,在ID_WEEK列上创建默认约束要容易得多。