I'm checking if this statement could take less time to execute. The execution plan is absolutely confusing. The query and plan are below.
There is a join between temp table and normal table with clustered index on Date column. Table contains ~1000 rows and temp table has a bit over 1 million.
In execution plan this join is reflected in two nested loops with lots of strange things like Constant Scans and Compute Scalars taking part in the process.
Clustered Index Seek here takes 74% and estimated number of rows is 1 there. Actual number of rows is huge.
select t.StartDateTime
, t.EndDateTime
, t.StartDateTime_BusinessHours
, t.EndDateTime_BusinessHours
, count (c.Date) as DaysOffCount
--into #DaysOff
from #CorrectedStartEndDateTime as t
left join util.Calendar as c on c. Date >= t.StartDateTime_BusinessHours and c. Date <= t.EndDateTime_BusinessHours and c.IsDayOff = 1
group by t.StartDateTime
, t.EndDateTime
, t.StartDateTime_BusinessHours
, t.EndDateTime_BusinessHours
Here is a link to the execution plan: https://www.brentozar.com/pastetheplan/?id=r1crVr6rB
Temporary table is a 'select into' dump from another query without any indexing. And table looks this way:
CREATE TABLE [util].[Calendar](
[Date] [date] NOT NULL,
[IsDayOff] [bit] NOT NULL,
[DaysTillNextBusinessDay] [int] NOT NULL,
CONSTRAINT [PK_Calendar] PRIMARY KEY CLUSTERED ([Date] ASC)
)
I would expect this to be a simple nested loop between temp table scan and clustered index seek or anything similar, could you please help me understand what's happenning and why?
Calendar
have no indexes apart from the one on[Date]
? – Larnu