How can I generate the dates for the entire range?
Thanks
How can I generate the dates for the entire range? Thanks
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
As Mitch Wheat suggests is his comment, probably the best way to improve the performance of this query is to use a numbers table in place of the recursive CTE used to generate the list of dates.
If you can’t or won’t use a numbers table, the performance of the date range CTE can be improved for large ranges using a method suggested by Itzik Ben-Gan:
Whilst the execution plan shows this version to be slightly less efficient than the original (=~ 1%), measuring with
SET STATISTICS TIMEon my system shows both the elapsed and CPU time for this version to be less than half that of yours.