在日期范围之间查询性能

时间:2018-05-02 21:13:18

标签: sql sql-server performance between

我有一个简单的查询来获取几个日期范围之间的日期。它运行大约10秒钟,但我想尽可能提高它的性能。 提前致谢

declare @Fst_LY date, @Lst_LY date, @Fst_TY date, @Lst_TY date

----------------------------------------------
set @Fst_TY ='4/1/18'   --First day this year
set @Lst_TY ='4/30/18'  --Last day this year
----------------------------------------------
set @Fst_LY ='4/2/17'   --First day last year
set @Lst_LY ='5/1/17'   --Last day last year
----------------------------------------------

select distinct  hyp as H
    from myView
where       
([Contract] between  @Fst_LY  and  @Lst_LY)  or ([Contract] between @Fst_TY  and  @Lst_TY)) 

1 个答案:

答案 0 :(得分:0)

尝试使用union代替or:

select  hyp as H
        from myView
    where       
     [Contract] between  @Fst_LY  and  @Lst_LY
    union  
    select  hyp as H
        from myView
    where       
     [Contract] between  @Fst_TY  and  @Fst_LY