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

后端 未结 4 2038
梦谈多话
梦谈多话 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:04

    Have you tries a table valued function?

    CREATE FUNCTION select_func1 (  
        @Key1 int=0,
        @Key2 int=0
    )
    RETURNS TABLE 
    AS RETURN (
        Select key3
        From Or_Table
        Where (@key1 =0 OR Key1 =@Key1) AND
              (@key2 =0 OR Key2 =@Key2)
    )
    
    
    select * from select_func1(1,2)
    

提交回复
热议问题