索引视图:如何选择聚集索引?
我将基于三个表(SQL Server 2005)创建一个索引视图,这些表之间具有内部和外部联接。我将针对此视图运行所有类型的查询。 所以,我想知道选择要聚集的索引的最佳方法是什么。标准是什么,或者有什么工具可以帮助我。
(抱歉,如果我的问题很无聊,我在设计数据库方面没有很多经验)。
提前致谢!
编辑:我应该在这里澄清一下,我在视图中使用的表的使用非常频繁,我为维护索引而花费的任何开销都应该得到回报。
I'm going to do an indexed view, based on three tables with inner and outer joins between them (SQL Server 2005). I will run all kind of queries against this view.
So, I wonder what is the best way to choose which index to be clustered. What are the criteria or is there any tools to help me around.
(Sorry if my question is dull, I don't have a lot of experience in designing databases).
Thanks in advance!
EDIT: I should make clarification here, that the tables I use in the view are with very intense use and any overhead I take for maintenance of the indexes, should be paid-off.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于它是一个索引,因此您必须选择一个列(或一组列),该列在所有情况下都保证为非空且唯一。这是最大、最严格的标准 - 任何可能为 NULL 或重复的内容从一开始就是不可能的。
根据您将在此索引视图上运行的查询类型,您可能还想查看是否有任何要对其运行范围查询的列(例如日期或其他列)。这可能会成为一个有趣的聚类键候选者。
但最重要的是:您的集群键必须在任何情况下都是唯一且非空的。根据我个人的经验,为了减少索引大小(从而增加每页的条目数),我会尝试使用尽可能小的键 - 单个 INT 最好,或者两个 INT 的组合 - 或者可能GUID - 但不要在集群键中使用 VARCHAR(500) 字段!
更新:致所有那些不断告诉我们聚集索引不必是唯一的发帖者 - 看看“索引女王”Kimberly Tripp 对此主题的看法:
资料来源: http ://www.sqlskills.com/blogs/kimberly/post/Ever-increasing-clustering-key-the-Clustered-Index-Debateagain!.aspx
Since it's an index, you have to pick a column (or set of columns) which is guaranteed to be non-null and unique in all cases. That's the biggest and most stringent criteria - anything that might be NULL or duplicate is out of the question right from the get-go.
Depending on the type of queries you'll be running on this indexed view, you might also want to see if you have any columns (e.g. a DATE or something) that you'll be running range queries against. That might make an interesting candidate for a clustering key.
But the main thing is: your clustering key must be unique and non-null in any circumstance. And in my personal experience, to reduce index size (and thus increase the number of entries per page), I'd try to use as small a key as possible - a single INT is best, or a combination of two INTs - or possibly a GUID - but don't use VARCHAR(500) fields in your clustering key!
UPDATE: to all those poster who keep telling us clustered indexes don't need to be unique - check out what the "Queen of Indexing", Kimberly Tripp, has to say on the topic:
Source: http://www.sqlskills.com/blogs/kimberly/post/Ever-increasing-clustering-key-the-Clustered-Index-Debateagain!.aspx
拇指法则:
选择您可能在查询中最常使用的列,如 WHERE、GROUP 等。这些列可能是非聚集索引的良好候选列。选择一列(或一组列),这可能会使您的行变得唯一,并且可能是聚集索引的良好候选者。
正如 marc 所提到的,聚集索引施加了唯一约束,因此它绝对需要您选择的列不应该有任何空值和重复项。
The thumb rule:
Select the columns which are you are probably going to use MOST in your queries as WHERE, GROUP etc. Those columns could be a good candidate for non-clustered indexes. Select a column (or a group of column) which would probably make your row unique, and that could be a good candidate for clustered index.
As mentioned by marc, a clustered index imposes a unique constraint, so it definately needed that the column you selct should not have any null and duplicate.
聚集索引不必是唯一的。其中的列甚至可以为空。例如,这将运行而不会出现错误:
聚集索引是磁盘上表结构的一部分。因此,聚集索引不使用额外的磁盘空间。
默认情况下,SQL Server 在主键上集群,这通常是一个不错的选择。如果您有大量表查找的密集查询,您可以更改它。更改聚集索引可以消除表查找。
A clustered index does not have to be unique. The columns in it can even be nullable. For example, this will run without an error:
A clustered index is part of the table structure on disk. As such, a clustered index uses no additional disk space.
By default, SQL Server clusters on the primary key, which is usually a good choice. You can change that if you have intensive queries with a lot of table lookups. Changing which index is clustered can eliminate table lookups.