Datediff performance

后端 未结 1 342
我寻月下人不归
我寻月下人不归 2020-12-31 09:36

I have a number of days variable which I want to compare against a datetime column (senddate) .

Im currently doing this :

DECLARE @RunDate datetime =         


        
相关标签:
1条回答
  • 2020-12-31 10:23

    The expression

    WHERE datediff(dd, senddate, @RunDate) > @CalculationInterval 
    

    won't be able to use an index on the senddate column, because of the function on the column senddate

    In order to make the WHERE clause 'SARGable' (i.e. able to use an index), change to the equivalent condition:

    WHERE senddate < dateadd(dd, -@CalculationInterval, @RunDate)
    

    [Thanks to @Krystian Lieber, for pointing out incorrect condition ].

    0 讨论(0)
提交回复
热议问题