June 4, 2013 at 10:57 pm
Hi All,
I am having a performance issue while updating records into sql server table,
I have created below trigger to update related tables if the status of this table is updated/Changed.
USE [EMEA_SERVICEMAX]
GO
/****** Object: Trigger [Servicemax].[ERP_ACCOUNT_DATE_UPDATE] Script Date: 06/05/2013 10:19:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TRIGGER [Servicemax].[ERP_ACCOUNT_DATE_UPDATE]
ON [EMEA_SERVICEMAX].[Servicemax].[ERP_ACCOUNT_STG]
FOR UPDATE
AS BEGIN
DECLARE @nOldValue varchar(20),@nNewValue varchar(20),@record_dt date,@CUST_NUM varchar(20),
@SOURCE_SERVER varchar(15),@COMPANY_NO int
SELECT @nOldValue=b.CUSTOMER_STATUS, @nNewValue=a.CUSTOMER_STATUS ,@record_dt=a.record_insert_dt
,@CUST_NUM=a.customer_number,@SOURCE_SERVER = a.SOURCE_SERVER,@COMPANY_NO=a.COMPANY_NO
FROM inserted a, deleted b
IF @nNewValue = @nOldValue
return
else
BEGIN
update EMEA_SERVICEMAX.Servicemax.SALES_ORDERS_STG
set record_insert_dt=@record_dt
where customer_number=@CUST_NUM
and SOURCE_SERVER=@SOURCE_SERVER
and COMPANY_NO=@COMPANY_NO
update EMEA_SERVICEMAX.Servicemax.INSTALLATION_STG
set record_insert_dt=@record_dt
where LOCATION_CUSTOMER_NO=@CUST_NUM
and SOURCE_SERVER=@SOURCE_SERVER
and COMPANY_NO=@COMPANY_NO
update EMEA_SERVICEMAX.Servicemax.ERP_ACCOUNT_PROJECTS_STG
set record_insert_dt=@record_dt
where customer_number=@CUST_NUM
and SOURCE_SERVER=@SOURCE_SERVER
and COMPANY_NO=@COMPANY_NO
update EMEA_SERVICEMAX.Servicemax.SERVICE_CONTRACTS_STG
set record_insert_dt=@record_dt
where customer_number=@CUST_NUM
and SOURCE_SERVER=@SOURCE_SERVER
and COMPANY_NO=@COMPANY_NO
update EMEA_SERVICEMAX.Servicemax.SERVICE_ORDER_STG
set record_insert_dt=@record_dt
where customer_number=@CUST_NUM
and SOURCE_SERVER=@SOURCE_SERVER
and COMPANY_NO=@COMPANY_NO
END
END
GO
Any help on this may be appreciated.
Thanks,
Nilesh
June 4, 2013 at 11:55 pm
I'd suggest that the performance problem is a secondary concern. That trigger will only work correctly if a single row is updated. Any time there's more than one row changed, the trigger will not do what's intended
nil.hajare (6/4/2013)
SELECT @nOldValue=b.CUSTOMER_STATUS, @nNewValue=a.CUSTOMER_STATUS ,@record_dt=a.record_insert_dt,@CUST_NUM=a.customer_number,@SOURCE_SERVER = a.SOURCE_SERVER,@COMPANY_NO=a.COMPANY_NO
FROM inserted a, deleted b
Scrap that, rewrite the trigger to handle any number of rows in the inserted/deleted tables. If you still have a performance problem after that, post execution plans.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
June 7, 2013 at 4:07 am
Thanks,
I am new on sql server. Any suggestions would be helpful for me to rewrite this trigger.
Could you please help me on this?
June 7, 2013 at 6:07 am
Without knowing DDL and having sample data to test against, this is a W.A.G. but maybe it'll help point you in the correct direction.
UPDATE stg
SET Record_insert_dt = a.record_insert_dt
FROM inserted a
INNER JOIN EMEA_SERVICEMAX.Servicemax.SALES_ORDERS_STG stg
ON a.Customer_Number = stg.Customer_Number
AND a.Source_Server = stg.SourceServer
AND a.Company_No = stg.Company_No;
The question is why are you trying to update multiple tables with one trigger?
And why are you doing an inner join on deleted from inserted? (EDIT: I have an idea, but I want to hear it from you.)
June 7, 2013 at 6:19 am
it's going to look a lot like this:
the only thing i could not determine was the primary key that woudl related the INSERTED to the DELETED ; your example did not make that clear so far:
CREATE TRIGGER [Servicemax].[ERP_ACCOUNT_DATE_UPDATE]
ON [EMEA_SERVICEMAX].[Servicemax].[ERP_ACCOUNT_STG]
FOR UPDATE
AS
BEGIN
--SALES_ORDERS_STG
UPDATE MyTarget
SET MyTarget.record_insert_dt = INSERTED.record_insert_dt
FROM EMEA_SERVICEMAX.Servicemax.SALES_ORDERS_STG MyTarget
INNER JOIN INSERTED
ON MyTarget.customer_number = INSERTED.customer_number
AND MyTarget.SOURCE_SERVER = INSERTED.SOURCE_SERVER
AND MyTarget.COMPANY_NO = INSERTED.COMPANY_NO
INNER JOIN DELETED ON INSERTED.PRIMARYKEY = DELETED.PRIMARYKEY
WHERE INSERTED.CUSTOMER_STATUS <> DELETED.CUSTOMER_STATUS
--INSTALLATION_STG
UPDATE MyTarget
SET MyTarget.record_insert_dt = INSERTED.record_insert_dt
FROM EMEA_SERVICEMAX.Servicemax.INSTALLATION_STG MyTarget
INNER JOIN INSERTED
ON MyTarget.customer_number = INSERTED.customer_number
AND MyTarget.SOURCE_SERVER = INSERTED.SOURCE_SERVER
AND MyTarget.COMPANY_NO = INSERTED.COMPANY_NO
INNER JOIN DELETED ON INSERTED.PRIMARYKEY = DELETED.PRIMARYKEY
WHERE INSERTED.CUSTOMER_STATUS <> DELETED.CUSTOMER_STATUS
--ERP_ACCOUNT_PROJECTS_STG
UPDATE MyTarget
SET MyTarget.record_insert_dt = INSERTED.record_insert_dt
FROM EMEA_SERVICEMAX.Servicemax.ERP_ACCOUNT_PROJECTS_STG MyTarget
INNER JOIN INSERTED
ON MyTarget.customer_number = INSERTED.customer_number
AND MyTarget.SOURCE_SERVER = INSERTED.SOURCE_SERVER
AND MyTarget.COMPANY_NO = INSERTED.COMPANY_NO
INNER JOIN DELETED ON INSERTED.PRIMARYKEY = DELETED.PRIMARYKEY
WHERE INSERTED.CUSTOMER_STATUS <> DELETED.CUSTOMER_STATUS
--SERVICE_CONTRACTS_STG
UPDATE MyTarget
SET MyTarget.record_insert_dt = INSERTED.record_insert_dt
FROM EMEA_SERVICEMAX.Servicemax.SERVICE_CONTRACTS_STG MyTarget
INNER JOIN INSERTED
ON MyTarget.customer_number = INSERTED.customer_number
AND MyTarget.SOURCE_SERVER = INSERTED.SOURCE_SERVER
AND MyTarget.COMPANY_NO = INSERTED.COMPANY_NO
INNER JOIN DELETED ON INSERTED.PRIMARYKEY = DELETED.PRIMARYKEY
WHERE INSERTED.CUSTOMER_STATUS <> DELETED.CUSTOMER_STATUS
--SERVICE_ORDER_STG
UPDATE MyTarget
SET MyTarget.record_insert_dt = INSERTED.record_insert_dt
FROM EMEA_SERVICEMAX.Servicemax.SERVICE_ORDER_STG MyTarget
INNER JOIN INSERTED
ON MyTarget.customer_number = INSERTED.customer_number
AND MyTarget.SOURCE_SERVER = INSERTED.SOURCE_SERVER
AND MyTarget.COMPANY_NO = INSERTED.COMPANY_NO
INNER JOIN DELETED ON INSERTED.PRIMARYKEY = DELETED.PRIMARYKEY
WHERE INSERTED.CUSTOMER_STATUS <> DELETED.CUSTOMER_STATUS
END
GO
Lowell
June 7, 2013 at 4:44 pm
Lowell (6/7/2013)
it's going to look a lot like this:the only thing i could not determine was the primary key that woudl related the INSERTED to the DELETED ; your example did not make that clear so far:
THAT is the actual performance problem. The lack of criteria between the virtual trigger tables makes a cross join.
--Jeff Moden
Change is inevitable... Change for the better is not.
June 7, 2013 at 4:45 pm
nil.hajare (6/7/2013)
Thanks,I am new on sql server. Any suggestions would be helpful for me to rewrite this trigger.
Could you please help me on this?
Yes. Before you take anyone's word for how to write a trigger, look up how to create them in Books Online (the free help system that comes up when you press the {f1} key.
--Jeff Moden
Change is inevitable... Change for the better is not.
June 10, 2013 at 1:00 pm
Not 100% on point but definitely useful for your developement.
https://www.simple-talk.com/sql/t-sql-programming/rbar--row-by-agonizing-row/
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply