This is direct continuation of this question, then solved with this answer.
The problem lies with the logic here:
,case when
Cast(o.[CreationDate] as time) > '16:00:00'
then (select top 1 [PKDate] from [calendar].[dbo].days
where is_workday = 1 and continuous_day > da.continuous_day
and continuous_day < da.continuous_day+7 order by PKDate asc)
else (select top 1 [PKDate] from [calendar].[dbo].days
where is_workday = 1 and continuous_day >= da.continuous_day
and continuous_day < da.continuous_day+7 order by PKDate asc)
end OpDate
This fragment tries to find next workday date based on time part of o.[CreationDate] in [calendar].[dbo].[days]. If I could replace that subquery with a join I think it would make the results faster, which is my aim.
However the only solution I could think of in this case is to add another column to [calendar].[dbo].[days], which would hold NextWorkdayDate. I can do that, but I would like query solution, as I may not always be able to amend schema.
The dataset to be crunched has doubled in size since last time (yeah, we had a great year), the full query also grew a bit, so it's taking too long to execute (~20min).
The above bit of code is now my main problem, as all its instances and variations account for 70% of the whole query's actual cost. Smoothing it out would be great.
This is for a Data Warehouse, so actually adding column makes a lot of sense. The Data Warehouse is getting bigger and more sophisticated so cutting down on cycles everywhere I can is good long-term strategy. Would you agree?