索引基本上是表的副本吗?

Are indexes basically a copy of the tables?

今天我去参加工作面试,当时我听说 "Indexes are bascially a clones of the tables, on which they're made"。

有人能理解这个说法吗?老实说,我从来没有听说过这种索引定义

如果您谈到聚簇索引,那是真的。只需检查文档:

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.

但是,如果您谈到非聚集索引,那么它是错误的,因为 table 存储为堆和索引,与 table 分开。在这种情况下,索引是另一个看起来像数据结构的对象。

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.

You can add nonkey columns to the leaf level of the nonclustered index to by-pass existing index key limits, and execute fully covered, indexed, queries. For more information, see Create Indexes with Included Columns. For details about index key limits see Maximum Capacity Specifications for SQL Server.

不是真的,虽然他们可能是。

每个索引(包括聚簇索引)都将在其所有内部节点中使用索引 keys。不同之处在于当我们到达索引的 时会发生什么。

在 SQL 服务器中的普通老式非聚集索引中,您会在叶子中找到聚集索引的键值(或堆的某种形式的行 ID tables)。而在聚簇索引中,您会找到 所有列的值 ,而不仅仅是聚簇键和(对于该索引)特定键的值。

INCLUDE 在索引中通过在非聚集索引中的叶级别包含额外的列来稍微混淆水。

如果非聚集索引的(索引键、聚集索引键、包含列)中的总列集与 table 中所有列的集合相同,则在某种程度上,非聚集索引似乎确实是 table 的副本 - 至少在某种程度上,任何使用此索引的查询都不必执行任何 table 查找来检索所有数据.

如果上面的一组列与 table 中所有列的一组不同,则它不是 table 的副本。它是 table 列子集的副本。当然,如果这个列子集是特定查询所需的所有列,那么仍然可以避免 table 查找。