从数据库(SQL Server)中的一个表中删除比较慢

时间:2018-08-08 09:34:58

标签: sql-server

我有一个表,其中有两个用于插入和更新的触发器。记录号为700000,并且从表中删除的速度很慢,对于插入和更新后的触发器,是否会出现此问题?

我的触发器:

ALTER TRIGGER [dbo].[FillFinallAmount_UpdateTR] ON [dbo].[StuffPriceInfo] 
after Update AS 
--declare var
    select @fatherid=i.FatherPriceInfoId from inserted i; 
    select @priceid=i.PriceInfoId from inserted i; 
    select @stuffcodeseq=i.stuffcodeseq from inserted i; 
    select @StuffServiceCode=i.StuffServiceCode from inserted i; 
    select @PriceType=i.PriceType from inserted i; 
    select @TaskType=i.TaskType from inserted i; 
    select @basepercent=i.basepercent from inserted i; 
    select @NewAmount=i.NewAmount from inserted i; 
    select @FinalAmount=i.FinalAmount from inserted i; 
    select @dbasepercent=i.basepercent from deleted i; 
    select @dNewAmount=i.NewAmount from deleted i; 
    select @dFinalAmount=i.FinalAmount from deleted i; 
    select @dfatherid=i.FatherPriceInfoId from deleted i; 
    IF(@dbasepercent!=@basepercent or @dNewAmount!=@NewAmount or @dFinalAmount!=@FinalAmount or @dfatherid!=@fatherid) 
    BEGIN 
        IF update(FatherPriceInfoId) 
        BEGIN 
            IF @fatherid is not null 
            BEGIN 
                set @fatheramount=(select FinalAmount from StuffPriceInfo where PriceInfoId=@fatherid) 
                set @FinalAmount=((@fatheramount*@basepercent*0.01)+@NewAmount) 
                update StuffPriceInfo set FinalAmount=@FinalAmount where PriceInfoId=@priceid 
                update StuffPriceInfo set FinalAmount=((@FinalAmount*basepercent*0.01)+NewAmount)   where FatherPriceInfoId=@priceid 
            END 
            else 
            begin 
                update StuffPriceInfo set FinalAmount=@NewAmount where PriceInfoId=@priceid 
                update StuffPriceInfo set FinalAmount=((@NewAmount*basepercent*0.01)+NewAmount)   where FatherPriceInfoId=@priceid 
            END 
        END 
        ELSE if update(basepercent) 
        BEGIN 
            IF @fatherid is not null 
            BEGIN 
                SET @fatheramount=(select FinalAmount from StuffPriceInfo where PriceInfoId=@fatherid) 
                SET @FinalAmount=((@fatheramount*@basepercent*0.01)+@NewAmount) 
                UPDATE StuffPriceInfo set FinalAmount=@FinalAmount where PriceInfoId=@priceid 
                UPDATE StuffPriceInfo set FinalAmount=((@FinalAmount*basepercent*0.01)+NewAmount)   where FatherPriceInfoId=@priceid 
            END 
            ELSE 
            BEGIN 
                UPDATE StuffPriceInfo set FinalAmount=@NewAmount where PriceInfoId=@priceid 
                UPDATE StuffPriceInfo set FinalAmount=((@NewAmount*basepercent*0.01)+NewAmount)   where FatherPriceInfoId=@priceid 
            END 
        END 

AND .....

1 个答案:

答案 0 :(得分:0)

通过为与“ PriceInfoId”列相关的“ FatherPriceInfoId”列创建索引来解决问题(两列都在同一表中,而“ PriceInfoId”是主键)