无法使用index seek的写法

create table tb(id int,c1 int,c2 int,c3 int)
go
declare @N int=0
while @N<10000
begin
    
insert tb values(@N,@N,@N,@N)
    
set @N+=1
end
go
create index index1 on tb(id)
go
declare @id int=0
select * from tb
where id=@id or @id is null
option(recompile)

多条件查询时为了方便书写sql,经常会这么写,但这样的缺点是即使返回的结果集很小,也无法使用index seek。

示例中的语句会使用index scan+lookup

原文地址:https://www.cnblogs.com/stswordman/p/1881268.html