聚集索引和非聚集索引之间的区别是什么?


当前回答

聚集索引

一个表只能有一个聚集索引。 通常在主键上进行。 聚集索引的叶节点包含数据页。

非聚簇索引

一个表只能有249个非聚集索引(直到sql version 2005的后续版本支持多达999个非聚集索引)。 通常在任意键上。 非聚集索引的叶节点不包含数据页。相反,叶节点包含索引行。

其他回答

聚类索引本质上是索引列中数据的排序副本。

聚集索引的主要优点是,当查询(seek)在索引中定位数据时,不需要额外的IO来检索该数据。

维护聚集索引的开销,特别是在频繁更新的表中,可能会导致性能较差,因此最好创建非聚集索引。

//复制自MSDN,其他答案中没有明确提到非聚类索引的第二点。

集群

Clustered indexes sort and store the data rows in the table or view based on their key values. These are the columns included in the index definition. There can be only one clustered index per table, because the data rows themselves can be stored in only one order. The only time the data rows in a table are stored in sorted order is when the table contains a clustered index. When a table has a clustered index, the table is called a clustered table. If a table has no clustered index, its data rows are stored in an unordered structure called a heap.

非聚集

Nonclustered indexes have a structure separate from the data rows. A nonclustered index contains the nonclustered index key values and each key value entry has a pointer to the data row that contains the key value. The pointer from an index row in a nonclustered index to a data row is called a row locator. The structure of the row locator depends on whether the data pages are stored in a heap or a clustered table. For a heap, a row locator is a pointer to the row. For a clustered table, the row locator is the clustered index key.

群集索引对磁盘上的数据进行物理排序。这意味着索引不需要额外的数据,但只能有一个聚集索引(显然)。使用聚集索引访问数据是最快的。

All other indexes must be non-clustered. A non-clustered index has a duplicate of the data from the indexed columns kept ordered together with pointers to the actual data rows (pointers to the clustered index if there is one). This means that accessing data through a non-clustered index has to go through an extra layer of indirection. However if you select only the data that's available in the indexed columns you can get the data back directly from the duplicated index data (that's why it's a good idea to SELECT only the columns that you need and not use *)

聚类基本上意味着数据在表中的物理顺序。这就是为什么每个表只能有一个。

非聚集意味着它“只是”一个逻辑顺序。

聚集索引

每张桌子只有一个 由于数据按索引顺序物理存储,因此读取速度比非集群更快

非聚类索引

每张表可以使用多次吗 插入和更新操作比聚集索引更快

当选择使用索引的字段的数据时,这两种类型的索引都将提高性能,但会降低更新和插入操作的速度。

由于插入和更新较慢,聚集索引应该设置在一个字段上,通常是增量的,即Id或时间戳。

SQL Server通常只使用选择性高于95%的索引。