我正在考虑向数据库表添加一个列,并且对于大多数行,该列可能为null但是我希望能够在该列上查询特定的非空值。因此索引会有助于检索时间,但是如果包含空值会破坏我的索引的选择性。
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值和索引时,SQL Server不一定遵守标准,但我在文档中找不到关于该主题的明确声明(索引基础知识,非聚集索引结构和非聚集索引设计指南)。我也找不到关于SO这个特定主题的问题。
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!
注意我使用的是SQL Server 2008,但如果在'05到'12之间有相关的变化,我有兴趣听到这个。
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.
推荐答案我建议过滤索引,例如 WHERE列IS NOT NULL; - 这将允许你创建一个索引只困扰索引非NULL值,并忽略所有没有值的行。您可能希望确保索引涵盖了您希望使用此类谓词运行的查询,这样您就不必返回整个表来查找查询需要输出的其他列(或使用在加入,或以其他方式过滤等)。
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).
此处有更多详情。
更多推荐
SQL Server索引是否在非聚集的非唯一索引中为空值?
发布评论