I have a simple table not related to any other. It has a not PK column that it is a date. I have created a non-clustered index to that column. If I make this query:
select * from table where datecolumn is not null <– does not use the index and goes really slow.
But if I remove the not, this way:
select * from table where datecolum is null <– uses the index and goes really fast.
There are much more not nulls than nulls.
Am I forgetting something? Could I use filtered index here?
Thanks in advance.
This is normal. It won’t use the index unless the predicate is selective enough to warrant it.
It sounds like the vast majority of records are not NULL so instead of finding these via the non clustered index then having to do loads of bookmark lookups and random I/O to retrieve the rest of the columns to return it is quicker and more efficient to just scan the whole clustered index.
You can use
FORCESEEKto force the behaviour that you say you want. You will likely find that the time taken and I/O stats go through the roof compared to the clustered index scan.