I have a SP the sp execute on a table with an index in three columns col_1,col_2,col_3,col_4,col_n. Col 1 is an identity. there is an index on col_2,col_3,col_4 in that order.
When I query something like
@parameter=1
SELECT col_2,col_3,col_4
WHERE col_2=@parameter
AND col_3='2'
AND col_4=10.00
It uses the index I mention before but if I query something like
@paramete =null; -- the parameter is null in this case when parameter
has value it uses the index
SELECT col_2,col_3,col_4
WHERE (@parameter is null OR col_2 = @parameter)
AND col_3='2'
AND col_4=10.00
It shows a query plan with a key look up on col_1 with a 99% cost.
Sometimes the value is null and I must remain like that.
Can someone explain this? can it be fixed?
Thanks.