SQL Server 中的复合索引与 INCLUDE 覆盖索引

发布于 2024-09-26 16:52:36 字数 186 浏览 2 评论 0原文

我知道复合索引总是从左到右使用(例如,如果索引位于城市、州,WHERE City = "Blah" 或 WHERE City = "Blah" AND St​​ate = "AA" 可以正常工作,但 WHERE State = "AA “不会)。

同样的原则也适用于 INCLUDE 索引吗?

提前致谢!

黏土

I understand that Composite Indexes are always used Left to Right (e.g. if an Index was on City, State, WHERE City = "Blah" or WHERE City = "Blah" AND State = "AA" would work fine but WHERE State = "AA" would not).

Does this same principle apply to INCLUDE indexes?

Thanks in advance!

Clay

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

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

发布评论

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

评论(2

倒数 2024-10-03 16:52:36

包含列只能用于向查询的 SELECT 部分提供列。它们不能用作过滤索引的一部分。

编辑:为了进一步阐明我的观点,请考虑以下示例:

我创建一个简单的表并填充它:

create table MyTest (
    ID int,
    Name char(10)
)

insert into MyTest
    (ID, Name)
    select 1, 'Joe' union all
    select 2, 'Alex'

现在考虑一个简单 SELECT 的这 3 个索引及其相应的执行计划。

select ID, Name
    from MyTest
    where Name = 'Joe'

情况 1:仅 ID 上的索引会导致表扫描。

create index idx_MyTest on MyTest(ID)

alt text

情况 2:包含名称的 ID 索引。稍微好一些,因为索引覆盖了查询,但我仍然得到了 SCAN 操作。

create index idx_MyTest on MyTest(ID) include (Name)

alt text

情况 3:包含 ID 的 Name 索引。这是最好的。索引是建立在我的 WHERE 子句中的列上的,因此我得到了 SEEK 操作,并且由于包含列,索引覆盖了查询。

create index idx_MyTest on MyTest(Name) include (ID)

替代文字

Include columns can only be used to supply columns to the SELECT portion of the query. They cannot be used as part of the index for filtering.

EDIT: To further clarify my point, consider this example:

I create a simple table and populate it:

create table MyTest (
    ID int,
    Name char(10)
)

insert into MyTest
    (ID, Name)
    select 1, 'Joe' union all
    select 2, 'Alex'

Now consider these 3 indexes and their corresponding execution plans for a simple SELECT.

select ID, Name
    from MyTest
    where Name = 'Joe'

Case 1: An index on just ID results in a TABLE SCAN.

create index idx_MyTest on MyTest(ID)

alt text

Case 2: An index on ID including name. Somewhat better because the index covers the query, but I still get a SCAN operation.

create index idx_MyTest on MyTest(ID) include (Name)

alt text

Case 3: An index on Name including ID. This is the best. The index is built on the column in my WHERE clause, so I get a SEEK operation, and the index covers the query because of the included column.

create index idx_MyTest on MyTest(Name) include (ID)

alt text

梦一生花开无言 2024-10-03 16:52:36

不,包含字段未排序。

以下是一些其他设计注意事项:

http://msdn.microsoft.com/en-我们/library/ms190806.aspx

No, include fields are not ordered.

Here are some additional design considerations:

http://msdn.microsoft.com/en-us/library/ms190806.aspx

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