SQL Server re-calculate or not?
There is an expression which I've build to reduce the time section out of date time and to ZERO it.
DateAdd(day, DateDiff(day, 0, PayDate), 0)
My question is in this query (pseudo code) :
select
DateAdd(day, DateDiff(day, 0, PayDate), 0),
DateAdd(day, DateDiff(day, 0, PayDate), 0) + 3
where DateAdd(day, DateDiff(day, 0, PayDate), 0) > 3
The DateAdd expressions are all the same.
Here is my question :
- Does SQL Server re-calculate each time it sees this expression (in the
sele开发者_如何学Pythonctand in thewhereclause...)
When you use a function several times in a query, it will be called every time it is used.
The fact that you call it with the same parameters make no difference.
So yes, DATEDIFF and DATEADD will be called three times each.
You can always use:
SELECT pd, pd+3
FROM
( SELECT DateAdd(day, DateDiff(day, 0, PayDate), 0) AS pd
FROM ...
)
WHERE pd > 3
The code looks more elegant and you can check performance comparing to your previous code.
You can also check this (half-improving):
SELECT pd, pd+3
FROM
( SELECT DateAdd(day, DateDiff(day, 0, PayDate), 0) AS pd
FROM ...
WHERE DateAdd(day, DateDiff(day, 0, PayDate), 0) > 3
)
Testing with a big table will show some evidence.
加载中,请稍侯......
精彩评论