尝试理解sql执行计划

发布于 2024-12-03 12:10:15 字数 463 浏览 3 评论 0原文

我在 SQL Server 中创建了一个简单的表:

MemberId INT PRIMARY KEY Identity
Name NVARCHAR(100) NULL
Description NVARCHAR(250) NULL

当我创建 MemberId 作为主键时,添加的唯一索引是聚集索引。我没有向名称或描述添加索引。

我向表中添加了大约 30,000 行测试数据,并执行了以下查询:

SELECT * FROM Members WHERE Name = 'Foo'

执行计划如下:

Clustered Index Scan - cost 100%

这是如何进行聚集索引扫描?我不是基于聚集索引。我认为这更像是表扫描。有人可以向我解释一下吗?如果不这样做的话,到底是什么会导致表扫描呢?

I created a simple table in SQL Server:

MemberId INT PRIMARY KEY Identity
Name NVARCHAR(100) NULL
Description NVARCHAR(250) NULL

The only index that was added was a clustered index when I created MemberId as a primary key. I didn't add an index to Name or Description.

I added about 30,000 rows of test data to the table and did the following query:

SELECT * FROM Members WHERE Name = 'Foo'

The execution plan said the following:

Clustered Index Scan - cost 100%

How is this a clustered index scan? I'm not predicating on the clustered index. I thought this would be more of a table scan. Can someone explain this to me? What exactly would cause a table scan if this does not?

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

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

发布评论

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

评论(2

浮光之海 2024-12-10 12:10:15

如果表有聚集索引,聚集索引扫描与全表扫描相同。聚集索引本身包含所有数据。

If the table has clustered index, clustered index scan is the same as full table scan. Clustered index by itself contains all data.

梦里的微风 2024-12-10 12:10:15

更改主键声明

如果您使用此MemberId INT PRIMARY KEY NONCLUSTERED IDENTITY

,您将扫描查找表,直到没有聚集索引。创建聚集索引后,您的表结构将发生变化,并按聚集索引键进行物理排序。我认为表扫描并不意味着之后,而是用聚集索引扫描代替。每当没有找到有用的索引来覆盖

另一个单词中的查询时,仅当表数据未聚集并且存储为 堆结构

表扫描和集群扫描之间有什么区别索引扫描?

if you change your primary key declaration with this

MemberId INT PRIMARY KEY NONCLUSTERED IDENTITY

you will find table scan until there is no clustered index . after creating a clustered index, your table structure will change and is ordered physically by your clustered index key(s). i think the table scan does not mean after that and is substituted with clustered index scan. whenever no useful index is found to cover the query

in another word table scan have meaning only when table data is not clustered and are stored as heap structure

What's the difference between a Table Scan and a Clustered Index Scan?

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