|
Home > Archive > MS SQL Server > February 2006 > Which is faster, datediff or dateadd?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Which is faster, datediff or dateadd?
|
|
| Bill Kouretsos 2006-02-16, 9:23 am |
| Hi,
Our db was not originally designed to track sales by a particular criteria,
so I have made an end-of-day query to track these stats and make them
available for analysis the next day (pretty heavy query, lots of joins and
cursors). The stats however are all based on time periods, so the query uses
many dateparts and datediffs to single out records in a particular time
period (ex. Yesterday, last 7 days, last 15 days, last 30, historical).
Right now, I'm selecting my records using datediff < 7, or datediff < 15
etc. this query takes really long (something like 2 hours).
Would converting my code to use "datecolumn between dateadd(y, -7, getdate()
) and getdate() " make it any faster?
Thanks,
-Bill
| |
| Scott Morris 2006-02-16, 11:23 am |
| > Would converting my code to use "datecolumn between dateadd(y, -7,
> getdate()
> ) and getdate() " make it any faster?
In general, yes. This would allow the use of any useful indexes on the
datecolumn (assuming you had useful indexes). However, you need to
carefully consider the actual values used for the boundaries. Have a look
at the following.
http://www.karaszi.com/sqlserver/info_datetime.asp
|
|
|
|
|