请解释为什么非聚集索引和聚集索引之间存在以下差异。首先,我运行以下两个select语句。
select *
from [dbo].[index_test2]
where id = 1 -- Nonclustered index on id column
select *
from [dbo].[index_test1]
where id = 1 -- Clustered index on id column
执行计划为第一个查询显示“表扫描”,为第二个查询显示“聚集索引搜索(聚集)”。
然后我运行下面的两个语句。
select id
from [dbo].[index_test2]
where id = 1 -- Nonclustered index on id column
select id
from [dbo].[index_test1]
where id = 1 -- Clustered index on id column
执行计划为第一个查询显示“索引查找(NonClustered)”,为第二个查询显示“聚集索引查找(群集)”。
从以上两种情况可以看出,当使用聚集索引时,它将执行“索引查找”,但对于NonClustered索引,它将显示“表扫描”(使用*执行),并显示“索引查找(NonClustered)”(使用索引应用列id执行)。
有人能解释一下为什么NonClustered指数在这两种情况下的反应不同吗?
发布于 2019-12-24 07:56:38
聚集索引定义数据在物理上存储在表中的顺序,但非聚集索引不会对table.In事实中的物理数据进行排序,非聚集索引存储在一个位置,而表数据存储在另一个位置。
如果您使用Non-Clustered Index
,则当您调用它属性时,它将在Index seek (NonClustered)
模式下工作,但如果您将where
置于Non-Clustered Index
模式,但在select
中调用了更多未覆盖索引更改模式的表达式,则会将其更改为Table scan
包含列的索引在覆盖查询时提供了最大的好处。这意味着索引包括查询引用的所有列,因为您可以添加数据类型、number或size不允许作为索引键列的列
但是在Clustered Index
中,因为实际的排序是由它来完成的,所以在Clustered index seek (clustered)
模式下可以同时完成这两项工作。
https://stackoverflow.com/questions/59464449
复制