Compare performance difference of T-SQL Between and '<' '>' operator?

后端 未结 7 1245
情歌与酒
情歌与酒 2020-12-31 05:02

I\'ve tried searching through search engines,MSDN,etc. but can\'t anything. Sorry if this has been asked before. Is there any performance difference between using the T-SQL

7条回答
  •  灰色年华
    2020-12-31 05:28

    In more complex queries where you can't know your min or max value that you'll be going to compare to beforehand, then using BETWEEN is almost equivalent to using the >= and <= operators.

    But when you know beforehand exactly what your min and max values are, it is cheaper not to use the BETWEEN operator and instead compare the wanted results to 1 number below the min and 1 number above the max.

    SELECT n.Number FROM dbo.Numbers AS n
    WHERE n.Number BETWEEN 1 AND 100
    

    is almost equivalent to:

    SELECT n.Number FROM dbo.Numbers AS n
    WHERE n.Number >= 1 AND n.Number <= 100
    

    where each row is compared separately for >, < and for =.

    rather do:

    SELECT n.Number FROM dbo.Numbers AS n
    WHERE n.Number > 0 AND n.Number < 101
    

    where each row is compared only for > and <.

提交回复
热议问题