我正在使用SSIS使用SQL Server 2008 R2更新表中的13000行,该表上有一个更新触发器,没有更新所需的触发器< 5分钟,触发它> 1小时。
我原本预计它会在触发器上花费两倍的时间(<10分钟)但它会占用更长的时间。任何人都可以给出任何可能导致此问题的指示,或解决问题的方法吗?
表:
CREATE TABLE [Dim].[Company]
(
[CompanyKey] INT NOT NULL IDENTITY ,
[Name] nCHAR(255) NOT NULL,
[City] nCHAR(100) NOT NULL,
[Country] nCHAR(100) NOT NULL,
[PostCode] nCHAR(20) NOT NULL,
[Continent] nCHAR(100) NOT NULL,
[EcconomicRegion] nCHAR(50) NOT NULL,
[ErdfAreaType] nCHAR(10) NOT NULL,
[PostCodeLong] nCHAR(10) NOT NULL,
[PostCodeLat] nCHAR(10) NOT NULL,
[Type] nCHAR(50) NOT NULL,
[NumEmployees] int NOT NULL,
[AnnualRevenue] MONEY NOT NULL,
[BalanceSheetTotal] MONEY NOT NULL,
[OwnershipType] nCHAR(50) NOT NULL,
[MembershipType] nCHAR(50) NOT NULL,
[AccountManagerKey] INT NOT NULL
CONSTRAINT [PK_Company] PRIMARY KEY CLUSTERED ([CompanyKey] asc)
)
GO
CREATE INDEX [IX_Company_NameCityPostcode] ON [Dim].[Company] ([Name], [City], [PostCode])
GO
CREATE INDEX [IX_Company_Name] ON [Dim].[Company] ([Name])
GO
CREATE INDEX [IX_Company_City] ON [Dim].[Company] ([City])
GO
CREATE INDEX [IX_Company_Postcode] ON [Dim].[Company] ([PostCode])
触发:
CREATE TRIGGER [Dim].[Company_Update] ON [Dim].[Company] FOR UPDATE
AS BEGIN SET NOCOUNT ON update [dim].[company] set ModifiedOn = GETDATE() END
更新执行计划:不确定它是否可见,但它显示执行1,运算符成本0.040004(92%)
触发更新执行计划:不确定其可见但是它表示执行1,运营商成本22.300197(97%)
解答: 基于Emmad Kareem回答和这个问题 - trigger updates entire table even on single-row update
我将触发器更改为:
CREATE TRIGGER [Dim].[Company_Update] ON [Dim].[Company] FOR UPDATE AS BEGIN
SET NOCOUNT ON
update [dim].[company] set ModifiedOn = GETDATE()
from [Dim].[Company] bf
inner join inserted i on i.CompanyKey = bf.CompanyKey
END
GO
现在更新按预期执行
答案 0 :(得分:1)
更新触发器SQL没有WHERE子句来指定要更新的特定行。
答案 1 :(得分:0)
你错过了关于触发器的where子句,所以基本上它每次更新表中的每一行。尝试这样的事情:
CREATE TRIGGER [Dim].[Company_Update]
ON [Dim].[Company] FOR UPDATE
AS
UPDATE [dim].[company]
SET ModifiedOn = GETDATE()
WHERE CompanyKey IN (SELECT DISTINCT CompanyKey FROM Inserted)