So we have a simple query that runs with joins to several tables and we date restrict this data. These queries form part of our data warehouse load and are time critical. We noticed a massive difference in both execution speeds and execution plans when we changed from using the dates in a where clause to joining to a temp table with the dates in. so:
Declare @StartDate DateTime = Cast(Floor(Cast(GetDate() AS FLOAT))AS DateTime)
Declare @EndDate DateTime = GetDate()
Select *
From Table A
Inner Join Table B
On A.A = B.A
Where A.Date Between @StartDate AND @EndDate
A simplified version of the query but returns 11k rows in around 50secs.
Declare @StartDate DateTime = Cast(Floor(Cast(GetDate() AS FLOAT))AS DateTime)
Declare @EndDate DateTime = GetDate()
Select @StartDate StartDate, @EndDate EndDate
Into #Dates
Select *
From Table A
Inner Join Table B
On A.A = B.A
Inner Join #Dates
On A.Date Between StartDate AND EndDate
Returns the same 11k rows but sub second. The difference in the execution plan is also noticeable, the second query is full of nested loops as opposed to hash matches in the first query.
My question is why? Why the 50 or so second difference?
/* Edit */
The two QEP's


What data type is A.Date? And does it have an index?WHERE a.Date >= @startDate AND a.Date <= @EndDate? I've not encountered your scenario, but then I rarely use BETWEEN (As I normally need<rather than<=)