聚集索引和非聚集索引之间的区别是什么?
当前回答
聚集索引
一个表只能有一个聚集索引。 通常在主键上进行。 聚集索引的叶节点包含数据页。
非聚簇索引
一个表只能有249个非聚集索引(直到sql version 2005的后续版本支持多达999个非聚集索引)。 通常在任意键上。 非聚集索引的叶节点不包含数据页。相反,叶节点包含索引行。
其他回答
优点:
聚集索引适用于范围(例如:select * from my_table where my_key在@min和@max之间)
在某些情况下,如果使用orderderby语句,DBMS将不需要做排序工作。
缺点:
聚集索引可能会减慢插入速度,因为如果新键不是按顺序排列的,那么在放入记录时必须修改记录的物理布局。
An indexed database has two parts: a set of physical records, which are arranged in some arbitrary order, and a set of indexes which identify the sequence in which records should be read to yield a result sorted by some criterion. If there is no correlation between the physical arrangement and the index, then reading out all the records in order may require making lots of independent single-record read operations. Because a database may be able to read dozens of consecutive records in less time than it would take to read two non-consecutive records, performance may be improved if records which are consecutive in the index are also stored consecutively on disk. Specifying that an index is clustered will cause the database to make some effort (different databases differ as to how much) to arrange things so that groups of records which are consecutive in the index will be consecutive on disk.
For example, if one were to start with an empty non-clustered database and add 10,000 records in random sequence, the records would likely be added at the end in the order they were added. Reading out the database in order by the index would require 10,000 one-record reads. If one were to use a clustered database, however, the system might check when adding each record whether the previous record was stored by itself; if it found that to be the case, it might write that record with the new one at the end of the database. It could then look at the physical record before the slots where the moved records used to reside and see if the record that followed that was stored by itself. If it found that to be the case, it could move that record to that spot. Using this sort of approach would cause many records to be grouped together in pairs, thus potentially nearly doubling sequential read speed.
In reality, clustered databases use more sophisticated algorithms than this. A key thing to note, though, is that there is a tradeoff between the time required to update the database and the time required to read it sequentially. Maintaining a clustered database will significantly increase the amount of work required to add, remove, or update records in any way that would affect the sorting sequence. If the database will be read sequentially much more often than it will be updated, clustering can be a big win. If it will be updated often but seldom read out in sequence, clustering can be a big performance drain, especially if the sequence in which items are added to the database is independent of their sort order with regard to the clustered index.
//复制自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.
聚类基本上意味着数据在表中的物理顺序。这就是为什么每个表只能有一个。
非聚集意味着它“只是”一个逻辑顺序。
聚集索引
一个表中只能有一个聚集索引 对记录进行排序,并根据顺序进行物理存储 数据检索比非聚类索引快 不需要额外的空间来存储逻辑结构
非聚类索引
一个表中可以有任意数量的非聚集索引 请勿影响物理秩序。为数据行创建逻辑顺序,并使用指向物理数据文件的指针 数据插入/更新比聚类索引快 使用额外空间存储逻辑结构
除了这些差异,你必须知道,当表是非聚集的(当表没有聚集索引)数据文件是无序的,它使用堆数据结构作为数据结构。
推荐文章
- 在SQL server查询中将NULL替换为0
- 在SQL中修改表的模式名
- 如何得到累计和
- 如何在SQL Server 2005的一条语句中更新两个表?
- 如何创建临时表与SELECT * INTO tempTable从CTE查询
- 在SQL Server的选择语句中使用带TOP的变量,而不是动态的
- SQL变量保存整数列表
- MySQL索引的最佳实践是什么?
- 在SQL中转换月号到月名函数
- 改变一个varchar列的最大长度?
- 如何在SQL中从DateTime格式获取时间?
- 在MySQL中使用INDEX和KEY有什么区别?
- 暂时关闭约束(MS SQL)
- WHERE子句中的IF子句
- 如何在SSMS中从ntext或nvarchar(max)查看所有文本?