I have created a table
create table #temp(a int, b int, c int)
I have 2 indexes on this table:
- Non clustered non unique index on c
- Clustered Index on a
When I try to execute the following query:
select b from #temp where c = 3
I see that the system goes for index scan. This is fine, because the non clustered index doesn’t have b as the key value. Hence it does an index scan from column a.
But when I try to execute the below query:-
select b from #temp where c= 3 and a = 3
I see that the execute plan has got only index seek. No scan. Why is that?
Neither the clustered index nor the nonclustered index as b as one of the columns?
I was hoping an index scan.
Please clarify
If you have
aas your clustering key, then that column is included in all non-clustered indices on that table.So your index on
calso includesa, so the conditioncan be found in that index using an index seek. Most likely, the query optimizer decided that doing a index seek to find
aandcand a key lookup to get the rest of the data is faster/more efficient here than using an index scan.BTW: why did you expect / prefer an index scan over an index seek? The index seek typically is faster and uses a lot less resources – I would always strive to get index seeks over scans.