I have an odd issue (mainly revolving around proper index usage.)
For some reason when I use a DATEADD function within a join the between or >= , <= operators the optimizer ignores the index and performs a full table scan.
Example
This will NOT use the INDEX
Basically the query will return dates today and a 1 month ago. which works fine but in looking at the query analyzer I find the optimizer is not using the index where as if i had used a hard coded date or DATEADD(DAY, 0, DATEDIFF(DAY, 0, GETDATE())) without supplying DATEADD range.
This WILL use the INDEX
OR
OR EVEN.. ( AND THIS ONE IS WEIRD)
Any Thoughts
Thanks
TalenX
For some reason when I use a DATEADD function within a join the between or >= , <= operators the optimizer ignores the index and performs a full table scan.
Example
This will NOT use the INDEX
Code:
--code start
Select * from Time_Dimension where Date_Number BETWEEN DATEADD(DAY, 0, DATEDIFF(MONTH, -1, GETDATE())) AND DATEADD(DAY, 0, DATEDIFF(DAY, 0, GETDATE()))
--code end
Basically the query will return dates today and a 1 month ago. which works fine but in looking at the query analyzer I find the optimizer is not using the index where as if i had used a hard coded date or DATEADD(DAY, 0, DATEDIFF(DAY, 0, GETDATE())) without supplying DATEADD range.
This WILL use the INDEX
Code:
--CODE START
Select * from Time_Dimension where Date_Number BETWEEN DATEADD(DAY, 0, DATEDIFF(DAY, 0, GETDATE())) AND DATEADD(DAY, 0, DATEDIFF(DAY, 0, GETDATE()))
--CODE END
Code:
--CODE START
Select * from Time_Dimension where Date_Number BETWEEN '2/1/2005' AND '2/28/2005'
--CODE END
OR EVEN.. ( AND THIS ONE IS WEIRD)
Code:
--CODE START
Select * from Time_Dimension where Date_Number DATEADD(DAY, 1, DATEDIFF(DAY, 0, GETDATE()))
Any Thoughts
Thanks
TalenX