3

IMO, please correct me...
the leaf of clustered index contains the real table row, so full clustered index, with intermediate leaves, contain much more data than the full table(?)
Why/when/how is ever whole clustered index scan chosen over the full table scan?

How is clustered index on CUSTOMER_ID column used in SELECT query which does not contain it in either SELECT list or in WHERE condition [1]?

Update:
Should I understand that full clustered scan is faster than full table scan because "Each data page contains pointers to the next and previous leaf node page so the scan does not need to use the higher level pages in the index"?
Are there any other reasons like (non-participating in query) clustered index is used in sorting?

Update2:
As afterthought, consecutive access cannot give performance boost while loading table through IAM pointers can be parallelized.
Does clustered index scan imply consecutive page reading?
Does clustered table imply absence of IAM pointers (impossibility of full table scan)?
Why cannot clustered table be full table scanned?
I still do not understand how/why clustered index full scan can be "better" over full table scan.
Does it mean that having clustered index can result in performance worsening?

The question is about clustered table not heap (non-indexed) table.

Update3:
Is "full clustered index scan" really synonym to "full table scan"?
What are differences?

[1] Index Covering Boosts SQL Server Query Performance
http://www.devx.com/dbzone/Article/29530

4

3 回答 3

2

聚集索引的叶级是表。“表扫描”是指没有聚集索引的堆。

每个数据页都包含指向下一个和上一个叶节点页的指针,因此扫描不需要使用索引中的更高级别的页。

于 2010-10-19T16:27:15.127 回答
2

聚集索引 - 或者更准确地说:它的叶页表数据 - 所以聚集索引扫描实际上与表扫描相同(对于具有聚集索引的表)。

如果你没有聚集索引,那么你的表就是一个堆——显然,在这种情况下,如果你需要查看所有数据,你不能进行聚集索引扫描,因为没有聚集索引,所以你最终会进行一次表扫描,该扫描仅触及该堆表的所有数据页。

于 2010-10-19T16:25:35.857 回答
0

请阅读我在“无法直接访问聚集表中的数据行 - 为什么?”下的答案。, 第一的。

“聚集索引的叶子包含真正的表行,因此带有中间叶子的完整聚集索引包含比完整表更多的数据(?)”

看到您将“表格”与存储结构混合在一起。在你的问题的背景下,例如。考虑 CI 的大小而不是“表”,那么你必须考虑 CI 减去叶级别(即数据行)。CI,仅索引部分,很小。中间层(像任何 B-Tree 一样)包含部分(非完整)键条目;它不包括最低级别,即完整的键条目,它位于行本身中,并且不重复。

该表(完整 CI)可能为 10GB。CI 可能只有 10MB。从 10MB 可以确定很多东西,而不必去 100GB。

为了理解:同一张表(CI)上的等效NCI可能是22MB;如果您删除 CI,同一张表上的等效 NCI 可能是 21.5MB(假设 CI 密钥是合理的,而不是太宽)。

“为什么/何时/如何选择整个聚集索引扫描而不是全表扫描?”

经常。同样的上下文是,我们正在谈论 CI-minus-Leaf 级别。对于仅使用 CI 中的列的查询,CI 中这些列(实际上是任何索引)的存在允许查询成为“覆盖查询”,这意味着它可以完全从索引中获得服务,无需去到数据行。考虑部分键的范围扫描:BETWEEN x AND yY;x <= y; 等等

(当您认为优化器应该选择索引扫描时,优化器总是有可能选择表扫描,但这是另一回事。)

“我仍然不明白聚集索引全扫描如何/为什么比全表扫描“更好”。”

(MS 使用的术语不如我在这里的答案精确。)对于可以从 10MB CI 回答的任何查询,我宁愿通过数据缓存搅动 10MB,而不是 100GB。对于相同的查询,受 CI 键范围的限制,这只是 10MB 的一小部分。

对于需要“全表扫描”的查询,是的,您必须阅读 CI 的所有叶子页面,即 100GB。

于 2010-10-29T13:19:08.567 回答