SQL Server索引是否在非聚集的非唯一索引中为空值?

时间:2021-02-22 04:19:27

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.

我正在考虑向数据库表添加一个列,并且对于大多数行,该列可能为null但是我确实希望能够在该列上查询特定的非空值。因此,索引将有助于检索时间,但是如果包含空值会破坏我的索引的选择性。

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.

我已经做了一些阅读,我得到的印象是,当涉及NULL值和索引时,SQL Server不一定遵守标准,但我在文档中没有找到关于该主题的明确声明(索引设计基础,非聚簇索引结构)和非聚集索引设计指南)。我也找不到关于这个特定主题的问题。

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.

注意我使用的是SQL Server 2008但是如果在'05到'12之间有相关的变化,我有兴趣听到这个。

1 个解决方案

#1


10  

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).

我建议过滤索引,例如WHERE列IS NOT NULL; - 这将允许您创建仅困扰索引非NULL值的索引,并忽略所有没有值的行。您可能希望确保索引涵盖了您希望使用此类谓词运行的查询,这样您就不必返回整个表来查找查询需要输出的其他列(或使用在连接中,或以其他方式过滤,等)。

More details here.

更多细节在这里。

#1


10  

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).

我建议过滤索引,例如WHERE列IS NOT NULL; - 这将允许您创建仅困扰索引非NULL值的索引,并忽略所有没有值的行。您可能希望确保索引涵盖了您希望使用此类谓词运行的查询,这样您就不必返回整个表来查找查询需要输出的其他列(或使用在连接中,或以其他方式过滤,等)。

More details here.

更多细节在这里。