Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
651 views
in Technique[技术] by (71.8m points)

indexing - Does SQL Server Index Null Values in a Non-Clustered Non-Unique index?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...