Hi There,
I have a script that is taking too long to run due to two joins. I have marked the two joins.
Please help to make it faster. I think it should be done in two parts, perhaps with a view and a second pass or a cursor.
Thanks for helping me with other scripts previously.
This is the script:
Declare @Today Date, @30DaysAgo Date, @120DaysAgo Date, @150DaysAgo Date, @180DaysAgo Date, @StatusChangedDays Date
Set @30DaysAgo = DATEADD(day, -30, GETDATE())
Set @120DaysAgo = DATEADD(day, -120, GETDATE())
Set @150DaysAgo = DATEADD(day, -150, GETDATE())
Set @180DaysAgo = DATEADD(day, -180, GETDATE())
Set @StatusChangedDays = DATEADD(day, -90, GETDATE())
Select --Top 100
xm.ID As 'Excalibur Reference'
, LTrim(IsNull(xd.D_FullNames,'') + ' ' + IsNull(xd.D_Surname,'')) As 'Debtor'
, ISNULL(xd.D_DebtorIdentityNo,'') As 'Debtor ID'
, CONVERT(varchar, xm.M_IntructionReceived, 103) 'Instructed'
, xdt.DP_Name as 'Department'
, Case When Format(XPC.LastPhoneCall,'yyyy-MM-dd') Is Null Then '' Else Format(XPC.LastPhoneCall,'yyyy-MM-dd') End As 'Last Phone Call'
, Isnull(XPC.LastPhoneNo,'') As 'Last Number Called'
, Isnull(XPC.WhoAnswered,'No Selection') As 'Who Answered'
--, Isnull(XMP.PN_Number,'') As 'RPC Number'
--, Isnull(Format(XMP.PN_RPC_Date,'yyyy-MM-dd'),'') As 'RPC Date'
, Isnull(XDB.DebtorBalance,0) As 'Debtors Balance'
, Isnull(Format(XDBR.DB_Date,'yyyy/MM/dd'),'') As 'Last Receipt'
, IsNull(XDBR.DB_Credit,0) As 'L/R Amount'
, Isnull(Format(xmfs.M_ActionChanged,'yyyy/MM/dd'),'') As 'Status Changed'
, Isnull(xfs.St_Status,'') As 'Status'
, Isnull(xfss.FSS_SubStatusName,'') As 'Sub Status'
, Isnull(CONVERT(varchar, XTR.BackFromTrace, 111),'') As 'Back From Trace'
, Case When xmfs.M_FileStatus = 4 Then 1 Else 0 End As 'PTPs 0=Attempting, 1=Broken'
, Case Isnull(XTR.TraceResult,0)
When 50003 Then 0 -- 50003 = RPC Trace Successful, 50004 = RPC Trace unsuccessful
When 50004 Then
Case When CONVERT(varchar, XTR.BackFromTrace, 111) > CONVERT(varchar, @StatusChangedDays, 111) Then
1 -- 1 = No Successful Trace < 90 Days
Else
2 -- 2 = No Successful Trace > 90 Days
End
Else
3 -- No Trace Done
End As '0=Successful Trace, 1=No Successful Trace, 2=No Successful Trace - Status Changed > 90 Days, 3=No Trace Done'
-- Attempting
, Case When xmfs.M_FileStatus <> 4 Then
Case When CONVERT(varchar, xm.M_IntructionReceived, 111) > CONVERT(varchar, @30DaysAgo, 111) Then 1 Else 0 End
Else
''
End 'Attempting < 30 Days Ago'
, Case When xmfs.M_FileStatus <> 4 Then
Case When CONVERT(varchar, xm.M_IntructionReceived, 111) <= CONVERT(varchar, @30DaysAgo, 111) And
CONVERT(varchar, xm.M_IntructionReceived, 111) > CONVERT(varchar, @120DaysAgo, 111) Then 1 Else 0 End
Else
''
End 'Attempting 30-120 Days Ago'
, Case When xmfs.M_FileStatus <> 4 Then
Case When CONVERT(varchar, xm.M_IntructionReceived, 111) <= CONVERT(varchar, @120DaysAgo, 111) And
CONVERT(varchar, xm.M_IntructionReceived, 111) > CONVERT(varchar, @150DaysAgo, 111) Then 1 Else 0 End
Else
''
End 'Attempting 120-150 Days Ago'
, Case When xmfs.M_FileStatus <> 4 Then
Case When CONVERT(varchar, xm.M_IntructionReceived, 111) <= CONVERT(varchar, @150DaysAgo, 111) And
CONVERT(varchar, xm.M_IntructionReceived, 111) > CONVERT(varchar, @180DaysAgo, 111) Then 1 Else 0 End
Else
''
End 'Attempting 150-180 Days Ago'
, Case When xmfs.M_FileStatus <> 4 Then
Case When CONVERT(varchar, xm.M_IntructionReceived, 111) <= CONVERT(varchar, @180DaysAgo, 111) Then 1 Else 0 End
Else
''
End 'Attempting > 180 Days Ago'
-- Broken
, Case When xmfs.M_FileStatus = 4 Then
Case When CONVERT(varchar, xmfs.M_ActionChanged, 111) > CONVERT(varchar, @30DaysAgo, 111) Then 1 Else 0 End
Else
''
End 'Broken < 30 Days Ago'
, Case When xmfs.M_FileStatus = 4 Then
Case When CONVERT(varchar, xmfs.M_ActionChanged, 111) <= CONVERT(varchar, @30DaysAgo, 111) And
CONVERT(varchar, xmfs.M_ActionChanged, 111) > CONVERT(varchar, @120DaysAgo, 111) Then 1 Else 0 End
Else
''
End 'Broken 30-120 Days Ago'
, Case When xmfs.M_FileStatus = 4 Then
Case When CONVERT(varchar, xmfs.M_ActionChanged, 111) <= CONVERT(varchar, @120DaysAgo, 111) And
CONVERT(varchar, xmfs.M_ActionChanged, 111) > CONVERT(varchar, @150DaysAgo, 111) Then 1 Else 0 End
Else
''
End 'Broken 120-150 Days Ago'
, Case When xmfs.M_FileStatus = 4 Then
Case When CONVERT(varchar, xmfs.M_ActionChanged, 111) <= CONVERT(varchar, @150DaysAgo, 111) And
CONVERT(varchar, xmfs.M_ActionChanged, 111) > CONVERT(varchar, @180DaysAgo, 111) Then 1 Else 0 End
Else
''
End 'Broken 150-180 Days Ago'
, Case When xmfs.M_FileStatus = 4 Then
Case When CONVERT(varchar, xmfs.M_ActionChanged, 111) <= CONVERT(varchar, @180DaysAgo, 111) Then 1 Else 0 End
Else
''
End 'Broken > 180 Days Ago'
From Matter xm With (Nolock)
Inner Join MatterFileStatus xmfs With (Nolock)on xmfs.IDX = xm.M_IDX
Inner join FileStatus xfs with (nolock) on xfs.ID = xmfs.M_FileStatus
Inner join FileSubStatus xfss with (Nolock) on xmfs.M_FileSubStatus = xfss.ID
Inner join Department xdt with (Nolock) on xmfs.M_DepartmentID = xdt.ID
Inner Join BookListView xb with (Nolock) on xb.ID = xm.M_ClientID
Left Join Debtor xd With (Nolock) on xd.D_IDX = xm.M_IDX
Left Join (
Select H_IDX ,H_DateTime, H_HCRS_ID As AnsweredBy,
ROW_NUMBER() Over(Partition By H_IDX Order by H_IDX, H_DateTime Desc) rowNum
From History with (nolock)
Where H_HA_ID = 6 -- 6 = Call
And H_HCRS_ID = 1 -- 1 = Debtor Answered
) XPCNC On XPCNC.H_IDX = xm.M_IDX And XPCNC.rowNum = 1
Left Join ( -- UP TO HERE IT TAKES 6 SECONDS TO LOAD
Select PN_PL_TblID , PN_Number, PN_RPC_Date,
ROW_NUMBER() Over(Partition By PN_PL_TblID Order by PN_PL_TblID, PN_RPC_Date Desc) rowNum
From Phone with (nolock)
Where PN_RPC > 0 -- > 0 = RPC
) XMP On XMP.PN_PL_TblID = xm.M_IDX And XMP.rowNum = 1
Left Join ( -- ADDS 1 MIN TO SCRIPT
Select DB_IDX , DB_Date, DB_Credit, DB_Transaction_TypeID, ROW_NUMBER() Over(Partition By DB_IDX Order by DB_IDX, DB_Date Desc) rowNum
From Debtor_Balance with (nolock)
Where DB_Transaction_TypeID = 1 -- 1 = Receipt
) XDBR On XDBR.DB_IDX = xm.M_IDX And XDBR.rowNum = 1
Left Join ( -- UP TO HERE IT TAKES 1:25 MINUTES TO LOAD
Select H_IDX ,H_DateTime As BackFromTrace, H_SubSt_ID As TraceResult, ROW_NUMBER() Over(Partition By H_IDX Order by H_IDX, H_DateTime Desc) rowNum
From History with (nolock)
Where H_HA_ID = 11 -- 11 = Status Changed
And H_SubSt_ID In (50003,50004) -- 50003 - Trace Successful, 50004 - Trace Unsucessful
) XTR On XTR.H_IDX = xm.M_IDX And XTR.rowNum = 1
-- THE NEXT TWO JOINS SLOW THE SCRIPT DOWN VERY BADLY - IF I DON'T LOAD THE NEXT TWO JOINS IT TAKES 1:25 MINUTES TO LOAD.
-- HOW DO I FIX THIS?
-- I THOUGHT OF A SCRIPT WITHIN A SCRIPT MIGHT SPEED IT UP. I.E. A CURSOR OR MAYBE A VIEW AND THEN ADD THE BOTTOM TWO JOINS?
Left Join ( -- ADDS 6:30 MINUTES TO SCRIPT; 8:03 MINUTES; 10:58 MINUTES; 7:43 MINUTES
Select DB_IDX, SUM(IsNull(DB_Debit,0)) - SUM(IsNull(DB_Credit,0)) As DebtorBalance
From Debtor_Balance with (nolock)
Group By DB_IDX
) As xdb On xdb.DB_IDX = xm.M_IDX
Left Join ( -- ADDS 3 HOURS TO SCRIPT
Select H_IDX ,H_DateTime As LastPhoneCall, hcsr.HCSR_Desc As WhoAnswered, H_PhoneNumber As LastPhoneNo,
ROW_NUMBER() Over(Partition By H_IDX Order by H_IDX, H_DateTime Desc) rowNum
From History with (nolock)
Left Join HistoryCallSelectionResult hcsr on hcsr.ID = H_HCRS_ID
Where H_HA_ID = 6 -- 6 = Call
) XPC On XPC.H_IDX = xm.M_IDX And XPC.rowNum = 1
Where xb.Name Not Like '%TEST%' -- Test Matters -- EXCLUDE
And xb.Name Not Like '%ACME%' -- Test Matters -- EXCLUDE
And xb.Name Not Like '%Ithala%' -- Ithala Matters -- EXCLUDE
And xb.Name Not Like '%First National%' -- First National Matters -- EXCLUDE
And xm.ID Not Like '%TEST%' -- Test Matters -- EXCLUDE
And xm.ID Not Like '%ACME%' -- ACME Test Matters -- EXCLUDE
And xb.ID Not In (901305, 901499, 901507) -- Outsourced Matters -- EXCLUDE
And xmfs.M_FileStatus Not In (2,5,6,10,11,16,25,29,40,51,52,65,70,71,106,111,112,10000)
And xmfs.M_DepartmentID = 2 -- Call Centre
And Isnull(XPCNC.AnsweredBy,0) <> 1 -- 1 = Debtor Answered
And Isnull(XMP.PN_Number,'') = ''
ORDER BY xm.ID