SQL Server:在条件需要更多时间的情况下

时间:2017-12-16 12:38:00

标签: sql sql-server

我有以下SQL查询需要更多时间来运行,即执行相同查询时执行4分钟以上静态“1”或“0”值需要的时间少得多,即少于3-4秒:

select 
    column1, column2 
from 
    tablename
where  
    bitColumn_1 = (case when @bitColumn_1_param is null 
                           then bitColumn_1 
                           else @bitColumn_1_param 
                   end)
    and bitColumn_2 = (case when @bitColumn_2_param is null 
                               then bitColumn_2 
                               else @bitColumn_2_param 
                       end)
    and bitColumn_3 = (case when @bitColumn_3_param is null 
                               then bitColumn_3 
                               else @bitColumn_3_param 
                       end)
    and bitColumn_4 = (case when @bitColumn_4_param is null 
                               then bitColumn_4 
                               else @bitColumn_4_param 
                       end)
    and bitColumn_5 = (case when @bitColumn_5_param is null 
                          then bitColumn_5 
                          else @bitColumn_5_param 
                       end)
    and bitColumn_6 = (case when @bitColumn_6_param is null 
                               then bitColumn_6 
                               else @bitColumn_6_param 
                       end)
    and bitColumn_7 = (case when @bitColumn_7_param is null 
                               then bitColumn_7 
                               else @bitColumn_7_param 
                       end)

任何改进查询的帮助都会有所帮助!

提前致谢

2 个答案:

答案 0 :(得分:2)

您可以尝试以下查询,这可能会对您有所帮助

 select column1,column2 from tablename
 where bitColumn_1 = ISNULL(@bitColumn_1_param,bitColumn_1)
 AND bitColumn_2 =ISNULL(@bitColumn_2_param,bitColumn_2) 
 AND bitColumn_3 =ISNULL(@bitColumn_3_param,bitColumn_3) 
 AND bitColumn_4 =ISNULL(@bitColumn_4_param,bitColumn_4) 
 AND bitColumn_5 =ISNULL(@bitColumn_5_param,bitColumn_5)
 AND bitColumn_6 =ISNULL(@bitColumn_6_param,bitColumn_6) 
 AND bitColumn_7 =ISNULL(@bitColumn_1_param,bitColumn_7) 

答案 1 :(得分:0)

dd=df1.stack().reset_index().groupby('level_0').level_1.apply(list).tolist()
dd
Out[67]: [['1', '3'], ['2', '3']]

df2.apply(lambda x : sum([all(x.loc[y]==1) for y in dd]),1 )

Out[78]: 
0    0
1    0
2    1
dtype: int64
df2[df2.apply(lambda x : sum([all(x.loc[y]==1) for y in dd]),1 )==1]
Out[80]: 
   1  2  3  4  5
2  1  0  1  0  0