开发者

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开发者_如何学Pythonct and in the where clause...)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜