简体   繁体   English

SQL Server:为什么聚集索引扫描而不是表扫描?

[英]SQL Server: Why Clustered Index Scan and not Table Scan?

I have following table schema - 我有以下表格架构 -

CREATE TABLE [dbo].[TEST_TABLE]
(
    [TEST_TABLE_ID] [int] IDENTITY(1,1) NOT NULL,
    [NAME] [varchar](40) NULL,
    CONSTRAINT [PK_TEST_TABLE] PRIMARY KEY CLUSTERED 
    (
        [TEST_TABLE_ID] ASC
    )
)

I have inserted huge data in TEST_TABLE . 我在TEST_TABLE插入了大量数据。

As I have marked TEST_TABLE_ID column as primary key, clustered index will be created on TEST_TABLE_ID . 由于我已将TEST_TABLE_ID列标记为主键,因此将在TEST_TABLE_ID上创建聚簇索引。

When I am running following query, execution plan is showing Clustered Index Scan which is expected. 当我运行以下查询时,执行计划显示预期的Clustered Index Scan

SELECT * FROM TEST_TABLE WHERE TEST_TABLE_ID = 34

But, when I am running following query I was expecting Table Scan as NAME column does not have any index: 但是,当我运行以下查询时,我期待表扫描,因为NAME列没有任何索引:

SELECT * FROM TEST_TABLE WHERE NAME LIKE 'a%'

But in execution plan it is showing Clustered Index Scan . 但在执行计划中,它显示了Clustered Index Scan

As NAME column does not have any index why it is accessing the clustered index? 由于NAME列没有任何索引,为什么它正在访问聚簇索引?

I believe, this is happening as clustered index resides on data pages. 我相信,随着聚集索引驻留在数据页面上,这种情况正在发生。

Can anyone tell me if my assumption is correct? 谁能告诉我我的假设是否正确? Or is there any other reason? 或者还有其他原因吗?

A clustered index is the index that stores all the table data. 聚簇索引是存储所有表数据的索引。 So a table scan is the same as a clustered index scan. 因此,表扫描与聚簇索引扫描相同。

In a table without a clustered index (a "heap"), a table scan requires crawling through all data pages. 在没有聚簇索引(“堆”)的表中,表扫描需要爬行所有数据页。 That is what the query optimizer calls a "table scan". 这就是查询优化器调用“表扫描”的内容。

As others explained already, for a table that has a clustered index, a Clustered Index Scan means a Table Scan . 正如其他人已经解释的那样,对于具有聚簇索引的表,聚簇索引扫描意味着表扫描

In other words, the table is the clustered index. 换句话说, 该表是聚集索引。

What you have wrong is your first query execution plan: 你的错误是你的第一个查询执行计划:

SELECT * 
FROM TEST_TABLE 
WHERE TEST_TABLE_ID = 34 ;

It does a Clustered Index Seek and not a Scan. 它执行Clustered Index Seek而不是Scan。 It doesn't have to search ( scan ) the whole table (clustered index), it goes directly to the point ( seeks ) and checks if a row with id=34 exists. 它没有搜索( 扫描 )整个表(聚集索引),它直接进入点( 寻求 ),如果有一排检查id=34存在。

You can see a simple test in SQL-Fiddle , and how the two execution plans differ. 您可以在SQL-Fiddle中看到一个简单的测试,以及两个执行计划的不同之处。

The table is stored as a clustered index. 该表存储聚簇索引。 The only way to scan the table is to scan the clustered index. 扫描表的唯一方法是扫描聚簇索引。 Only tables with no clustered index can have a "table scan" per se. 只有没有聚集索引的表本身才能有“表扫描”。

It is because this table has a clustered index and it will scan the entire clustered index to return all the rows base on the where clause. 这是因为该表具有聚簇索引,它将扫描整个聚簇索引以返回基于where子句的所有行。 How ever you should be seeing a missing index message. 你应该怎么看到丢失的索引消息。

When you build a Clustered Index on a table, then SQL Server logically orders the rows of that table based on the Clustered Index Key, which in your case is Test_Table_ID. 当您在表上构建群集索引时,SQL Server会根据群集索引键(在您的情况下为Test_Table_ID)逻辑地对该表的行进行排序。

However, when you see the Clustered Index Scan operator, this COULD be a little misleading. 但是,当您看到聚集索引扫描运算符时,这可能会产生一些误导。 If certain conditions are met, (which equate to SQL Server not caring about the order of the data) then SQL Server is still able to perform an unordered allocation scan, which is more similar to a table scan than an clustered index scan, as it actually reads the leaf level of the CI (the tables data pages) in allocation order, based on the IAM chain, as opposed to following the pointers in the index. 如果满足某些条件(等同于SQL Server不关心数据的顺序),则SQL Server仍然能够执行无序分配扫描,这与表扫描比聚簇索引扫描更相似,因为它实际上,基于IAM链,按照分配顺序读取CI的叶级(表数据页),而不是跟随索引中的指针。 This can potentially give you a performance improvement, as fragmentation (pages being out of physical order) does not decrease performance 这可能会提高性能,因为碎片(页面超出物理顺序)不会降低性能

To see if this is happening, look at the Ordered property in the execution plan. 要查看是否发生这种情况,请查看执行计划中的Ordered属性。 If this is set to False, then you have an unordered allocation scan. 如果将其设置为False,则您将进行无序分配扫描。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么 SQL 服务器不使用我的聚集索引并进行非聚集索引扫描? - Why isn't SQL Server using my clustered index and doing a non-clustered index scan? SQL Server 执行计划中的“Clustered Index Scan (Clustered)”是什么意思? - What "Clustered Index Scan (Clustered)" means on SQL Server execution plan? 为什么我的聚集索引上有扫描? - Why is there a scan on my clustered index? 是在SQL Server中的聚集索引扫描期间读取实际表数据还是仅读取索引指针? - Is the actual table data read during clustered index scan in SQL server or just the index pointers? SQL中的表扫描和索引扫描 - Table Scan and Index Scan in SQL SQL Server执行计划,非聚集索引扫描的成本 - Sql Server execution plan, cost of non-clustered index scan TOP(1) vs MIN 问题 - 为什么 SQL 服务器使用聚集扫描而不是寻找覆盖索引? - TOP(1) vs MIN problem - Why does SQL Server use a clustered scan rather than seeking the covering index? 为什么NonClustered索引扫描比聚簇索引扫描更快? - Why NonClustered index scan faster than Clustered Index scan? 表扫描和聚集索引扫描之间有什么区别? - What's the difference between a Table Scan and a Clustered Index Scan? 为什么有时表扫描比索引扫描更快? - Why sometimes table scan is faster then index scan?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM