聚集索引和非聚集索引之间的区别是什么?
当前回答
你可能已经阅读了以上文章中的理论部分:
-聚类索引,我们可以看到直接指向记录,即它的直接,所以它需要更少的时间进行搜索。此外,它不会占用任何额外的内存/空间来存储索引
而在非聚集索引中,它间接指向聚集索引,然后它将访问实际的记录,由于它的间接性质,它将花费更多的时间来访问。此外,它需要自己的内存/空间来存储索引
其他回答
聚集索引物理地存储在表上。这意味着它们是最快的,并且每个表只能有一个聚集索引。
非聚集索引单独存储,您可以拥有任意数量的索引。
最好的选择是在最常用的唯一列上设置聚集索引,通常是PK。在表中应该始终有一个精心选择的聚集索引,除非有一个非常令人信服的理由——想不出一个,但是,嘿,可能有——不这样做。
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.
你可能已经阅读了以上文章中的理论部分:
-聚类索引,我们可以看到直接指向记录,即它的直接,所以它需要更少的时间进行搜索。此外,它不会占用任何额外的内存/空间来存储索引
而在非聚集索引中,它间接指向聚集索引,然后它将访问实际的记录,由于它的间接性质,它将花费更多的时间来访问。此外,它需要自己的内存/空间来存储索引
聚集索引
一个表中只能有一个聚集索引 对记录进行排序,并根据顺序进行物理存储 数据检索比非聚类索引快 不需要额外的空间来存储逻辑结构
非聚类索引
一个表中可以有任意数量的非聚集索引 请勿影响物理秩序。为数据行创建逻辑顺序,并使用指向物理数据文件的指针 数据插入/更新比聚类索引快 使用额外空间存储逻辑结构
除了这些差异,你必须知道,当表是非聚集的(当表没有聚集索引)数据文件是无序的,它使用堆数据结构作为数据结构。
聚集索引实际上描述了记录在磁盘上物理存储的顺序,因此只能有一个聚集索引。
非聚集索引定义的逻辑顺序与磁盘上的物理顺序不匹配。
推荐文章
- 在SQL Server中查找重复的行
- 执行带有Output参数的存储过程?
- 我如何在T-SQL用逗号格式化一个数字?
- LEFT OUTER JOIN如何返回比左表中存在的记录更多的记录?
- 如何用SQL语句计算百分比
- Postgres唯一约束与索引
- SQL Server动态PIVOT查询?
- 如何等待2秒?
- SQL Server: CROSS JOIN和FULL OUTER JOIN的区别是什么?
- varchar和nvarchar SQL Server数据类型之间的主要性能差异是什么?
- 向现有表添加主键
- 我应该在SQL varchar(长度)中考虑电话的最长的全球电话号码是什么
- 表中标识列的显式值只能在使用列列表且IDENTITY_INSERT为ON SQL Server时指定
- 如何确定已安装的SQL Server实例及其版本?
- Scope_Identity()、Identity()、@@Identity和Ident_Current()之间的区别是什么?