Sql Server 中的索引

发布于 2024-07-17 04:34:04 字数 54 浏览 4 评论 0原文

什么是聚集索引和非聚集索引? 如何使用sql server 2000企业管理器对表建立索引?

What is Clustered and non clustered indexing? How to index a table using sql server 2000 Enterprise manager?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

婴鹅 2024-07-24 04:34:04

ID 上的聚集索引中,表行ID排序。

ID 上的非聚集索引中,对表行的引用ID排序。

我们可以将数据库与CSV文件进行比较:

ID,Value
-------
1,ReallyReallyLongValue1
3,ReallyReallyLongValue2

聚集表中,当我们插入新行时,我们需要将其挤压在现有行之间:

ID,Value
-------
1,ReallyReallyLongValue1
2,ReallyReallyLongValue2
3,ReallyReallyLongValue3

,这很慢插入时但检索时速度快。

非聚集表中,我们保留一个单独的文件索引文件来对行进行排序:

Id,RowNumber
------------
1, 1
3, 2

当我们插入新行时,我们只需将其附加到我们的主文件中并更新短索引文件:

ID,Value
-------
1,ReallyReallyLongValue1
3,ReallyReallyLongValue3
2,ReallyReallyLongValue2

Id,RowNumber
------------
1, 1
2, 3
3, 2

,插入速度快,但检索效率较低。

在实际数据库中,索引使用更高效的二叉树,但原理保持不变。

聚集索引在SELECT上更快,非聚集索引在INSERT / UPDATE / DELETE上更快

In a clustered index on ID, the table rows are ordered by ID.

In a non-clustered index on ID, the references to table rows are ordered by ID.

We can compare a database to a CSV file:

ID,Value
-------
1,ReallyReallyLongValue1
3,ReallyReallyLongValue2

In a clustered table, when we insert a new row, we need to squeeze it between the existing rows:

ID,Value
-------
1,ReallyReallyLongValue1
2,ReallyReallyLongValue2
3,ReallyReallyLongValue3

, which is slow on insert but fast on retrieve.

In a non-clustered table, we keep a separate file index file which orders our rows:

Id,RowNumber
------------
1, 1
3, 2

When we insert the new row, we just append it to our main file and update the short index file:

ID,Value
-------
1,ReallyReallyLongValue1
3,ReallyReallyLongValue3
2,ReallyReallyLongValue2

Id,RowNumber
------------
1, 1
2, 3
3, 2

, which is fast on insert but less efficient on retrieve.

In real databases indexes use more efficient binary trees, but the principle remains the same.

Clustered indexes are faster on SELECT, non-clustered indexes are faster on INSERT / UPDATE / DELETE

我很OK 2024-07-24 04:34:04

聚集索引意味着行按该索引中的值进行物理排序。 非聚集索引意味着索引表保持最新,允许根据值快速查找和排序,但不会对行进行物理排序。

一张表只能存在一个聚集索引,如果存在主键,则该索引就是聚集索引(在 SQL Server 中)。

A clustered index means that the rows are physically ordered by the values in that index. A non-clustered index means that an index table is kept up to date that allows for quick seeking and sorting based upon value, but does not physically order the rows.

Only one clustered index can exist for a table, and if a primary key exists then that is the clustered index (in SQL Server).

雨巷深深 2024-07-24 04:34:04

聚集索引定义了实际表的存储方式。 行的存储方式可以快速搜索聚集索引中的字段。 (它们实际上并不是按照索引字段的排序顺序存储的,而是存储在二叉树 或类似的东西。)

每个表只能有一个聚集索引。 聚集索引包含表中的所有字段,例如:

   indexfield1 - indexfield2 - field2 - field3 - ....

非聚集索引就像一个单独的表。 它包含索引中的字段以及对表中字段的引用。 例如:

  secondindexfield1 - secondindexfield2 - reference to table row

当搜索非聚集索引时,SQL Server将查找索引中的值,对表进行“书签查找”,并从那里检索其他行字段。 这就是为什么非聚集索引的性能略低于聚集索引的原因。

要在 SQL Server Management Studio 中添加索引,请在对象视图中展开表节点。 右键单击“索引”并选择“新建索引”。

A clustered index defines how the actual table is stored. The rows are stored in a way to make searches on the fields in the clustered index fast. (They're not physically stored in the sort order of the index fields, but in a binary tree or something similiar.)

You can have only one clustered index per table. The clustered index contains all fields in the table, for example:

   indexfield1 - indexfield2 - field2 - field3 - ....

A non-clustered index is like a separate table. It contains the fields in the index, and a reference to the fields in the table. For example:

  secondindexfield1 - secondindexfield2 - reference to table row

When searching a non-clustered index, SQL server will find the value in the index, do a "bookmark lookup" to the table, and retrieve the other row fields from there. This is why non-clustered indexes perform slightly less wel then clustered indexes.

To add an index in SQL Server Management Studio, expand the table node in object view. Right click on "Indexes" and select "New Index".

我不吻晚风 2024-07-24 04:34:04
  • 聚集索引:每个表只允许有一个聚集索引。 如果一个索引是聚集索引,则意味着聚集索引所基于的表是根据该索引进行物理排序的。 想想百科全书中的页码。

  • 非聚集索引:每个表可以有许多非聚集索引。 想想书后面的关键字索引。

  • Clustered Index: Only one clustered index per table is allowed. If an index is clustered, it means that the table on which the clustered index is based is physically sorted according to that index. Think of the page numbers in an encyclopedia.

  • Non-clustered Index: Can have many non-clustered indexes per table. Think of the keyword index at the back of the book.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文