Why is SQL Server using index scan instead of index seek when WHERE clause contains parameterized values

前端 未结 4 1075
滥情空心
滥情空心 2020-12-18 05:02

We have found that SQL Server is using an index scan instead of an index seek if the where clause contains parametrized values instead of string literal.

<
4条回答
  •  鱼传尺愫
    2020-12-18 05:38

    Try

    declare @val1 nvarchar(40), @val2 nvarchar(40);
    set @val1 = 'val1';
    set @val2 = 'val2';
    
    select 
        min(id) 
    from 
        scor_inv_binaries 
    where 
        col1 in (@val1, @val2) 
    group by 
        col1
    OPTION (RECOMPILE)
    

提交回复
热议问题