I am using the following where predicate to get only Monday to Fridays tickets from one of our DW tables.
SET DATEFIRST 1
SELECT TicketID
FROM Tickets AS T
WHERE DATEPART(DW,T.StartDate) BETWEEN 1 AND 5 ....`
Would it be possible to let me know if the DATEPART
predicate is sargable please?
I took a look at the actual execution plan and it uses an index seek even with the DATEPART
function.
We have already optimised many of our old queries by modifying the where predicates, analysing the plan, set statistics io on, etc. For this one I am not sure if there is a better way to get tickets only from mon - fri (shifts..)?
The issue with your query is likely index selectivity because of your BETWEEN clause. If you're selecting 5 out of 7 week days, the optimizer is going to perform a table scan.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.