聚集索引和非聚集索引之间的区别是什么?
当前回答
你可能已经阅读了以上文章中的理论部分:
-聚类索引,我们可以看到直接指向记录,即它的直接,所以它需要更少的时间进行搜索。此外,它不会占用任何额外的内存/空间来存储索引
而在非聚集索引中,它间接指向聚集索引,然后它将访问实际的记录,由于它的间接性质,它将花费更多的时间来访问。此外,它需要自己的内存/空间来存储索引
其他回答
聚类基本上意味着数据在表中的物理顺序。这就是为什么每个表只能有一个。
非聚集意味着它“只是”一个逻辑顺序。
聚集索引物理地存储在表上。这意味着它们是最快的,并且每个表只能有一个聚集索引。
非聚集索引单独存储,您可以拥有任意数量的索引。
最好的选择是在最常用的唯一列上设置聚集索引,通常是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.
聚集索引
每张桌子只有一个 由于数据按索引顺序物理存储,因此读取速度比非集群更快
非聚类索引
每张表可以使用多次吗 插入和更新操作比聚集索引更快
当选择使用索引的字段的数据时,这两种类型的索引都将提高性能,但会降低更新和插入操作的速度。
由于插入和更新较慢,聚集索引应该设置在一个字段上,通常是增量的,即Id或时间戳。
SQL Server通常只使用选择性高于95%的索引。
推荐文章
- 比较两个SQL Server数据库(模式和数据)的最佳工具是什么?
- 在SQL中,如何在范围中“分组”?
- 选项(RECOMPILE)总是更快;为什么?
- 设置数据库从单用户模式到多用户
- 我如何转义一个百分比符号在T-SQL?
- SQL Server恢复错误-拒绝访问
- 为什么PostgreSQL要对索引列进行顺序扫描?
- 如何在SQL Server Management Studio中查看查询历史
- SQL Server索引命名约定
- 可以为公共表表达式创建嵌套WITH子句吗?
- SQL Server的NOW()?
- 在SQL Server中截断(不是四舍五入)小数位
- 如何在SQL Server数据库中更改列数据类型而不丢失数据?
- 保持简单,以及如何在一个查询中执行多个CTE
- 如何使用TSQL截断数据库中的所有表?