I'm seeing several problems with this query, besides the abomination where you concatenate two strings into a timestamp literal that's cast to a date - 1 day and finally cast again to a string literal where you remove the dashes. Phew!
Please consider using the date type!
Anyway, I don't know about your indexes, but I can see two more problems.
First you can lower the amount of data that you handle by filtering table
rptingdata.roactreacc
before joining with the other tables.
WITH a AS (
SELECT a.rmsfilenum,
a.attrnycode,
a.rmstrandte
FROM rptingdata.roactreacc a
WHERE mstrandte between Replace(Char((Cast(Timestamp({?Enddate}||'000000') As Date)) - 1 days, ISO),'-','') and {?Enddate}
)
Then you have a condition on an outer joined table
f.rmsbrglvl4 in ('C123','C124','C125')
which effectively turns a couple of the outer joins into inner joins.
So if these joins are specified as inner joins instead the optimizer have the possibility of using
hash joins
or
sort merge
instead of
nested loops
, like this.
,accts as (
SELECT
a.rmsfilenum,
d.misddesc,
a.attrnycode,
b.rmsacctnum,
c.rmszipcode,
f.rmsbrglvl4,
e.rmsoffcrcd,
cast(char(action_code) as char(16) ccsid 37) as action_code,
cast(char(result_code) as char(16) ccsid 37) as result_code,
a.rmstrandte,
case when action_code = '4' then 'Home Phone' else 'Other Phone' end as Phonetype,
rmstrantim,
rmsdateasg
FROM
a
JOIN reporting.rprdbal e
ON a.rmsfilenum = e.rmsfilenum
JOIN reporting.rofcrcd f
ON e.rmsoffcrcd = f.rmsoffcrcd
left OUTER JOIN reporting.rmaster b
ON a.rmsfilenum = b.rmsfilenum
left OUTER JOIN reporting.rcomker c
ON a.rmsfilenum = c.rmsfilenum
left OUTER JOIN reporting.rsasrecv d
ON a.attrnycode = d.rmsrecvrcd
WHERE (action_code = '4' OR action_code = '5')
AND f.rmsbrglvl4 in ('C123','C124','C125')
)
Note that you have a few columns where you haven't specified which table they belong to, so the query might need adjusting for that.
If action_code belongs to table
rptingdata.roactreacc
that condition needs to be moved to the first CTE.
Proper indexing is of course also necessary.
Please update the question with table prefix for all columns.
A plan would also be nice.
<edit>Should probably add an index on mstrandte is crucial for performance in this case</edit>