I am considering adding a column to a database table, and that column will be null possibly for most rows however I do want to be able to query on that column for particular non-null values. Therefore an index would help with retrieval time, however if null values are included that would ruin the selectivity of my index.
I have done some reading and I have got the impression that SQL Server doesn't necessarily adhere to standards when it comes to NULL values and indexes, but I can find no explicit statement about the topic in documentation (Index Design Basics, Nonclustered Index Structures and Nonclustered Index Design Guidelines). I also couldn't find a question on SO addressing this particular topic.
Any input (especially with a documentation reference) would be very much appreciated!
Note I am using SQL Server 2008 but if there have been relevant changes between '05 to '12 I'm interested in hearing about that.
I would suggest a filtered index, such as WHERE column IS NOT NULL;
- this will allow you to create an index that only bothers to index the non-NULL values, and ignores all of the rows with no value. You'll probably want to make sure the index covers the queries you want to run with this type of predicate, so that you don't have to go back into the whole table to lookup the other columns the query needs to output (or use in a join, or otherwise filter, etc).
More details here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With