Query taking more than 2 hours | SQL Server Performance Forums

SQL Server Performance Forum – Threads Archive

Query taking more than 2 hours

Hello experts,<br /><br />I have a query as below<br />UPDATE [Edge].[dbo].[tblCustomerFile] SET <br /> BillingCurrCd=Stg.EdgeCd<br /> ,UpdateDt=GETDATE()<br /> ,UpdateId=1<br /> ,AppId=1<br /> ,FunctionId=1<br /> FROM [Edge_Staging].[dbo].[IC2_CustomerFileBillingCurrencyCd] As Stg<br /> INNER JOIN [Edge].[dbo].[tblCustomerFile] As Tcf<br /> ON Stg.CustFileId=Tcf.CustFileId<br /> AND Stg.ConvStatus=0<br /><br />In this query I am trying to update the target table, and the record count is near a million. I know this is lot of an ask to look into my issue…but I need you guys help.<br />I tried all the combination but nothing seems effective on the table indexing and query optimizer. <br /><br />Can somebody take sometime and help in this? There is a table tblCustomerFile at the bottom of this posting. I can’t add any indexes to this table, and the changes can be done only on IC2_CustomerFileBillingCurrencyCd.<br /><br />Execution Plan:<br />UPDATE [Edge].[dbo].[tblCustomerFile] SET BillingCurrCd=Stg.EdgeCd ,UpdateDt=GETDATE() ,UpdateId=1 ,AppId=1 ,FunctionId=1 FROM [Edge_Staging].[dbo].[IC2_CustomerFileBillingCurrencyCd] As Stg I710NULLNULL1NULL<br /> |–Clustered Index Update(OBJECT<img src=’/community/emoticons/emotion-6.gif’ alt=’:(‘ />[Edge].[dbo].[tblCustomerFile].[PK_tblCustomerFile]), SET<img src=’/community/emoticons/emotion-6.gif’ alt=’:(‘ />[tblCustomerFile].[BillingCurrCd]=RaiseIfNull([Stg].[EdgeCd]), [tblCustomerFile].[FunctionId]=RaiseIfNull(1), [tblCustomerFile].[UpdateDt]=RaiseIfNull(getdate())721Clustered Index UpdateUpdateOBJECT<img src=’/community/emoticons/emotion-6.gif’ alt=’:(‘ />[Edge].[dbo].[tblCustomerFile].[PK_tblCustomerFile]), SET<img src=’/community/emoticons/emotion-6.gif’ alt=’:(‘ />[tblCustomerFile].[BillingCurrCd]=RaiseIfNull([Stg].[EdgeCd]), [tblCustomerFile].[FunctionId]=RaiseIfNull(1), [tblCustomerFile].[UpdateDt]=RaiseIfNull(getdate()), [tblCustomerFile].[UpdateINULL<br /> |–Compute Scalar(DEFINE<img src=’/community/emoticons/emotion-6.gif’ alt=’:(‘ />[Expr1008]=getdate()))732Compute ScalarCompute ScalarDEFINE<img src=’/community/emoticons/emotion-6.gif’ alt=’:(‘ />[Expr1008]=getdate())[Expr1008]=getdate()<br /> |–Top(ROWCOUNT est 0)743TopTopNULLNULL<br /> |–Hash Match(Aggregate, HASH<img src=’/community/emoticons/emotion-6.gif’ alt=’:(‘ />[Bmk1000]), RESIDUAL<img src=’/community/emoticons/emotion-6.gif’ alt=’:(‘ />[Bmk1000]=[Bmk1000]) DEFINE<img src=’/community/emoticons/emotion-6.gif’ alt=’:(‘ />[Stg].[EdgeCd]=ANY([Stg].[EdgeCd])))754Hash MatchAggregateHASH<img src=’/community/emoticons/emotion-6.gif’ alt=’:(‘ />[Bmk1000]), RESIDUAL<img src=’/community/emoticons/emotion-6.gif’ alt=’:(‘ />[Bmk1000]=[Bmk1000])[Stg].[EdgeCd]=ANY([Stg].[EdgeCd])<br /> |–Hash Match(Inner Join, HASH<img src=’/community/emoticons/emotion-6.gif’ alt=’:(‘ />[Tcf].[CustFileId])=([Stg].[CustFileId]), RESIDUAL<img src=’/community/emoticons/emotion-6.gif’ alt=’:(‘ />[Tcf].[CustFileId]=[Stg].[CustFileId]))765Hash MatchInner JoinHASH<img src=’/community/emoticons/emotion-6.gif’ alt=’:(‘ />[Tcf].[CustFileId])=([Stg].[CustFileId]), RESIDUAL<img src=’/community/emoticons/emotion-6.gif’ alt=’:(‘ />[Tcf].[CustFileId]=[Stg].[CustFileId])NULL<br /> |–Index Scan(OBJECT<img src=’/community/emoticons/emotion-6.gif’ alt=’:(‘ />[Edge].[dbo].[tblCustomerFile].[IDXCustomerFile4] AS [Tcf]), ORDERED FORWARD)776Index ScanIndex ScanOBJECT<img src=’/community/emoticons/emotion-6.gif’ alt=’:(‘ />[Edge].[dbo].[tblCustomerFile].[IDXCustomerFile4] AS [Tcf]), ORDERED FORWARD[Bmk1000], [Tcf].[CustFileId]<br /> |–Clustered Index Scan(OBJECT<img src=’/community/emoticons/emotion-6.gif’ alt=’:(‘ />[Edge_Staging].[dbo].[IC2_CustomerFileBillingCurrencyCd].[IX_IC2_CustomerFileBillingCurrencyCd] AS [Stg]), WHERE<img src=’/community/emoticons/emotion-6.gif’ alt=’:(‘ />[Stg].[convstatus]=0))786Clustered Index ScanClustered Index ScanOBJECT<img src=’/community/emoticons/emotion-6.gif’ alt=’:(‘ />[Edge_Staging].[dbo].[IC2_CustomerFileBillingCurrencyCd].[IX_IC2_CustomerFileBillingCurrencyCd] AS [Stg]), WHERE<img src=’/community/emoticons/emotion-6.gif’ alt=’:(‘ />[Stg].[convstatus]=0)[Stg].[convstatus], [Stg].[CustFileId], [Stg].[EdgeCd]<br /><br />Table Structures:<br />CREATE TABLE [dbo].[IC2_CustomerFileBillingCurrencyCd] (<br />[FileId] [int] NOT NULL ,<br />[TYPECD] [char] (4) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,<br />[EdgeCd] [int] NULL ,<br />[CustFileId] [int] NULL ,<br />[BillingCurrCd] [int] NULL ,<br />[UpdateDt] [datetime] NULL ,<br />[UpdateId] [int] NULL ,<br />[AppId] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,<br />[FunctionId] [int] NULL ,<br />[convstatus] [int] NULL <br />) ON [PRIMARY]<br />GO<br /><br /> CREATE CLUSTERED INDEX [IX_IC2_CustomerFileBillingCurrencyCd] ON [dbo].[IC2_CustomerFileBillingCurrencyCd]([CustFileId]) WITH FILLFACTOR = 99 ON [PRIMARY]<br />GO<br /><br /> CREATE INDEX [IX_IC2_CustomerFileBillingCurrencyCd_1] ON [dbo].[IC2_CustomerFileBillingCurrencyCd]([convstatus]) WITH FILLFACTOR = 100 ON [PRIMARY]<br />GO<br /><br />CREATE TABLE [dbo].[tblCustomerFile] (<br />[CustFileId] [int] NOT NULL ,<br />[BusnPartEmpId] [int] NOT NULL ,<br />[GroupMoveId] [int] NOT NULL ,<br />[BusnPartTmId] [int] NOT NULL ,<br />[RepaymentAgreeId] [int] NOT NULL ,<br />[CustFileStatusCd] [int] NOT NULL ,<br />[CustFileStatusDt] [datetime] NOT NULL ,<br />[CustFileStartDt] [datetime] NULL ,<br />[CustFileEndDt] [datetime] NULL ,<br />[TransId] [int] NULL ,<br /& gt;[TransEffDt] [datetime] NULL ,<br />[TransEntryDt] [datetime] NULL ,<br />[EffXferDt] [datetime] NULL ,<br />[AccompNonDependentFlg] [bit] NOT NULL ,<br />[AccompDependentCnt] [int] NULL ,<br />[AccompNonDependentCnt] [int] NULL ,<br />[CustTypCd] [int] NOT NULL ,<br />[CustMonthlySalaryAmt] [money] NULL ,<br />[CustTaxFilingExemptionsCnt] [int] NULL ,<br />[TaxFilingStatusCd] [int] NOT NULL ,<br />[TransferDt] [datetime] NULL ,<br />[RevenueLocTypCd] [int] NOT NULL ,<br />[PriorityTypCd] [int] NOT NULL ,<br />[MoveTypCd] [int] NOT NULL ,<br />[CustFileCmnt] [nvarchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,<br />[DeptrDeptNm] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,<br />[DeptrCityNm] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,<br />[DeptrStProvNm] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,<br />[DeptrStAbbr] [nchar] (4) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,<br />[DeptrCountryNm] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,<br />[DeptrCountryCd] [int] NOT NULL ,<br />[DestDeptNm] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,<br />[DestCityNm] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,<br />[DestStProvNm] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,<br />[DestStAbbr] [nchar] (4) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,<br />[DestCountryNm] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,<br />[DestCountryCd] [int] NOT NULL ,<br />[GovtAllowLimitMIDAAmt] [money] NULL ,<br />[GovtAllowLimitRDAAmt] [money] NULL ,<br />[IntlFlg] [bit] NOT NULL ,<br />[ClientIntlFlg] [bit] NOT NULL ,<br />[NewHireFlg] [bit] NOT NULL ,<br />[ConfidentialFlg] [bit] NOT NULL ,<br />[IntlMoveTypCd] [int] NOT NULL ,<br />[DeprtResidenceOwnCd] [int] NOT NULL ,<br />[EmpTaxMoveDt] [datetime] NULL ,<br />[TempLivingBeginDt] [datetime] NULL ,<br />[TempLivingEndDt] [datetime] NULL ,<br />[FARTrackingFlg] [bit] NOT NULL ,<br />[LumpSumAmt] [money] NULL ,<br />[GeoOriginTypCd] [int] NOT NULL ,<br />[GeoOriginDesc] [nvarchar] (80) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,<br />[OnlineExpRptEnabledFlg] [bit] NOT NULL ,<br />[RepaymentAgreeReceiveFlg] [bit] NOT NULL ,<br />[RepaymentAgreeSignedDt] [datetime] NULL ,<br />[RepaymentAgreeReceiveDt] [datetime] NULL ,<br />[EstMileageBetweenLocsCnt] [int] NULL ,<br />[SplInstructionsCmnt] [nvarchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,<br />[TransferAllowAmt] [money] NULL ,<br />[HoldUntilDt] [datetime] NULL ,<br />[CancelCd] [int] NOT NULL ,<br />[PHHFileNo] [nchar] (1<img src=’/community/emoticons/emotion-11.gif’ alt=’8)’ /> COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,<br />[EquinoxNo] [nchar] (1<img src=’/community/emoticons/emotion-11.gif’ alt=’8)’ /> COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,<br />[BusnSrcCd] [int] NOT NULL ,<br />[ExposureCd] [int] NOT NULL ,<br />[InterestCd] [int] NOT NULL ,<br />[JobCandFlg] [bit] NOT NULL ,<br />[JobCandAcceptDt] [datetime] NULL ,<br />[VIPFlg] [bit] NOT NULL ,<br />[LocalMoveFlg] [bit] NOT NULL ,<br />[CallRepId] [int] NOT NULL ,<br />[CallRepNm] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,<br />[CustContactPrefCd] [int] NOT NULL ,<br />[RevenueLocCd] [int] NOT NULL ,<br />[GlobalCustId] [int] NOT NULL ,<br />[CustInfoObtainedFlg] [bit] NOT NULL ,<br />[GovtFileFlg] [bit] NOT NULL ,<br />[EmpAssignFlg] [bit] NOT NULL ,<br />[InitInterviewCompFlg] [bit] NOT NULL ,<br />[MortgagePayoffFlg] [bit] NOT NULL ,<br />[PreviousHomeOwnerFlg] [bit] NOT NULL ,<br />[WEBEnabledFlg] [bit] NOT NULL ,<br />[LegacyPolicyExcptnFlg] [bit] NOT NULL ,<br />[LegacyOperationalPolicyExcptnFlg] [bit] NOT NULL ,<br />[LegacyPolicyExcptnDesc] [nvarchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,<br />[LegacyPolicyExcptnAmt] [money] NULL ,<br />[NewHirePolicyCd] [int] NOT NULL ,<br />[LegacyCustomerSiteNo] [nchar] (9) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,<br />[PrimaryReloContactCd] [int] NULL ,<br />[CustPrevCloseDt] [datetime] NULL ,<br />[CreateId] [int] NOT NULL ,<br />[CreateDt] [datetime] NOT NULL ,<br />[UpdateId] [int] NOT NULL ,<br />[UpdateDt] [datetime] NOT NULL ,<br />[FunctionId] [int] NOT NULL ,<br />[AppId] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,<br />[DBAPartitionKey] [int] NOT NULL ,<br />[CustViewPolicyCd] [int] NOT NULL ,<br />[CustViewHandbookCd] [int] NOT NULL ,<br />[CustViewExceptionCd] [int] NOT NULL ,<br />[ConciergeIndCd] [int] NOT NULL ,<br />[ExecutiveOfficerIndCd] [int] NOT NULL ,<br />[DeptrJobDesc] [nvarchar] (80) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,<br />[DestJobDesc] [nvarchar] (80) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,<br />[ClientAuthDt] [datetime] NULL ,<br />[AuthRecdDt] [datetime] NULL ,<br />[CustAuthTakeoverSrcCd] [int] NOT NULL ,<br />[CustAuthTakeoverOtherDesc] [nvarchar] (80) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,<br />[AuthRecdTime] [nchar] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,<br />[JobLevelCd] [int] NOT NULL ,<br />[JobClassCd] [int] NOT NULL ,<br />[AuthCreateById] [int] NOT NULL ,<br />[AuthCreateDt] [datetime] NULL ,<br />[AuthUpdDt] [datetime] NULL ,<br />[AuthUpdById] [int] NOT NULL ,<br />[AuthRequestByCd] [int] NOT NULL ,<br />[BillingCurrCd] [int] NOT NULL ,<br />[ClientCustFileIndCd] [int] NOT NULL ,<br />[CustCappedMoveIndCd] [int] NOT NULL ,<br />[CustCappedMoveAmt] [real] NULL ,<br />[CustMilitaryRankCd] [int] NOT NULL ,<br />[DestResidenceOwnCd] [int] NOT NULL <br />) ON [PRIMARY]<br />GO<br /><br /> CREATE INDEX [IDXCustomerFile1] ON [dbo].[tblCustomerFile]([RepaymentAgreeId]) WITH FILLFACTOR = 99 ON [PRIMARY]<br />GO<br /><br /> CREATE INDEX [IDXCustomerFile2] ON [dbo].[tblCustomerFile]([BusnPartEmpId]) WITH FILLFACTOR = 99 ON [PRIMARY]<br />GO<br /><br /> CREATE INDEX [IDXCustomerFile3] ON [dbo].[tblCustomerFile]([BusnPartTmId]) WITH FILLFACTOR = 99 ON [PRIMARY]<br />GO<br /><br /> CREATE INDEX [IDXCustomerFile4] ON [dbo].[tblCustomerFile]([GroupMoveId]) WITH FILLFACTOR = 99 ON [PRIMARY]<br />GO<br /><br /> CREATE INDEX [IDXCustomerFile5] ON [dbo].[tblCustomerFile]([TransId]) WITH FILLFACTOR = 99 ON [PRIMARY]<br />GO<br /><br />Thanks in advance!<br /><br />
Have you tried a WHERE EXISTS clause instead of the INNER JOIN? Compare the response time on SELECT queries, that should give you some indication for the time that will be required for UPDATE. SELECT CF.CustFileId
FROM [Edge].[dbo].[tblCustomerFile] CF
WHERE EXISTS
(SELECT Stg.* FROM [Edge_Staging].[dbo].[IC2_CustomerFileBillingCurrencyCd] As Stg
WHERE Stg.CustFileId = CF.CustFileId
AND Stg.ConvStatus=0) Also check: SELECT Stg.ConvStatus, COUNT(*)
FROM [Edge_Staging].[dbo].[IC2_CustomerFileBillingCurrencyCd] As Stg
GROUP BY Stg.ConvStatus If there is a relatively small number of rows with ConvStatus=0, then an IN clause with a subquery might perform better than the EXISTS version: SELECT CF.CustFileId
FROM [Edge].[dbo].[tblCustomerFile] CF
WHERE CF.CustFileId IN
(SELECT Stg.CustFileId FROM [Edge_Staging].[dbo].[IC2_CustomerFileBillingCurrencyCd] As Stg
WHERE Stg.ConvStatus=0)

is there any real need to use nvarchar instead of varchar and datetime instead of smalldatetime. —————————————-
quote:
CREATE CLUSTERED INDEX [IX_IC2_CustomerFileBillingCurrencyCd] ON [dbo].[IC2_CustomerFileBillingCurrencyCd]([CustFileId]) WITH FILLFACTOR = 99 ON [PRIMARY]
GO CREATE INDEX [IX_IC2_CustomerFileBillingCurrencyCd_1] ON [dbo].[IC2_CustomerFileBillingCurrencyCd]([convstatus]) WITH FILLFACTOR = 100 ON [PRIMARY]
GO

Have you tried lowering the fillfactor to a default value (90) or even lesser than that (85 or 80)?
It is not advisable to have fillfactor 100 when you have frequent/moderately frequent inserts and updates in your table. this will result in lot of page splits which may affect performance. Thanks,
Ram "It is easy to write code for a spec and walk in water, provided, both are freezed…"
Adriaan and others, I got it working as a charm. I used EXISTS and the update runs within 6 mins. Great! I got my work done for the day. Thanks to all of your suggestions. RoyalSher.
*********
The world is the great gymnasium where we come to make ourselves strong.
]]>