How to Optimize the Use of the “OR” Clause When Used with Parameters (SQL Server 2008)

后端 未结 4 2042
梦谈多话
梦谈多话 2020-12-17 22:57

I wonder if there is any wise way to rewrite the following query so that the indexes on columns get used by optimizer?



        
4条回答
  •  抹茶落季
    2020-12-17 23:20

    MSSQL 2008 has optimization syntax of condition simplification, here it is

     Where (@key1 =0 OR Key1 =@Key1) AND
          (@key2 =0 OR Key2 =@Key2) option(recompile)
    

    This will optimize usage of constants

提交回复
热议问题