sql-server - 索引 View : How to choose the Clustered Index?

标签 sql-server performance database-design indexed-view

我将基于三个表(SQL Server 2005)创建一个索引 View ,这些表之间具有内部和外部联接。我将针对此 View 运行所有类型的查询。 所以,我想知道选择要聚集的索引的最佳方法是什么。标准是什么,或者有什么工具可以帮助我。

(抱歉,如果我的问题很无聊,我在设计数据库方面没有很多经验)。

提前致谢!

编辑:我应该在这里澄清一下,我在 View 中使用的表的使用非常频繁,我为维护索引而花费的任何开销都应该得到返回。

最佳答案

由于它是一个索引,因此您必须选择一个列(或一组列),该列在所有情况下都保证为非空且唯一。这是最大、最严格的标准 - 任何可能为 NULL 或重复的内容从一开始就是不可能的。

根据您将在此索引 View 上运行的查询类型,您可能还想查看是否有任何要对其运行范围查询的列(例如 DATE 或其他列)。这可能会成为一个有趣的聚类键候选者。

但最重要的是:您的集群键必须在任何情况下都是唯一且非空的。根据我个人的经验,为了减少索引大小(从而增加每页的条目数),我会尝试使用尽可能小的键 - 单个 INT 最好,或者两个 INT 的组合 - 或者可能GUID - 但不要在集群键中使用 VARCHAR(500) 字段!

更新:致所有那些不断告诉我们聚集索引不需要唯一的发帖者 - 看看“索引女王”Kimberly Tripp 对此主题的看法:

Let's start with the key things that I look for in a clustering key:

* Unique
* Narrow
* Static

Why Unique?
A clustering key should be unique because a clustering key (when one exists) is used as the lookup key from all non-clustered indexes. Take for example an index in the back of a book - if you need to find the data that an index entry points to - that entry (the index entry) must be unique otherwise, which index entry would be the one you're looking for? So, when you create the clustered index - it must be unique. But, SQL Server doesn't require that your clustering key is created on a unique column. You can create it on any column(s) you'd like. Internally, if the clustering key is not unique then SQL Server will “uniquify” it by adding a 4-byte integer to the data. So if the clustered index is created on something which is not unique then not only is there additional overhead at index creation, there's wasted disk space, additional costs on INSERTs and UPDATEs, and in SQL Server 2000, there's an added cost on a clustereD index rebuild (which because of the poor choice for the clustering key is now more likely).

来源:http://www.sqlskills.com/blogs/kimberly/post/Ever-increasing-clustering-key-the-Clustered-Index-Debateagain!.aspx

关于sql-server - 索引 View : How to choose the Clustered Index?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2206541/

相关文章:

sql - 用户消息数据库架构?

mysql - 使用外键在 MySQL 中建模 ISA 关系

sql - 截断表和更新统计信息

SQL 生成 2 个已知整数之间的有效值范围

sql-server - SQL Server 2008 R2 中的 Unicode 规范化

performance - Flash 游戏中 child 的内存问题

sql-server - 插入前检查现有记录

sql-server - 表越大,数据库插入越慢

c++ - MSVC 发布 exe 的不同性能

sql-server - SQL中如何存储DropDownList信息