列以相反顺序定义的 2 个索引之间的差异
以下两个指标有什么区别吗?
- IDX_IndexTables_1
- IDX_IndexTables_2
如果有的话,有什么区别?
create table IndexTables (
id int identity(1, 1) primary key,
val1 nvarchar(100),
val2 nvarchar(100),
)
create index IDX_IndexTables_1 on IndexTables (val1, val2)
GO
create index IDX_IndexTables_2 on IndexTables (val2, val1)
GO
Are there any differences between following two indexes?
- IDX_IndexTables_1
- IDX_IndexTables_2
If there are any, what are the differences?
create table IndexTables (
id int identity(1, 1) primary key,
val1 nvarchar(100),
val2 nvarchar(100),
)
create index IDX_IndexTables_1 on IndexTables (val1, val2)
GO
create index IDX_IndexTables_2 on IndexTables (val2, val1)
GO
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
多列索引在概念上与获取所有列字段并将它们连接在一起没有什么不同——将结果索引为单个字段。
由于索引是 B 树,因此它们总是从左到右搜索。您必须从左侧开始搜索,以便在向右移动时将结果配对,以便索引完成其工作并提供有用的结果。
仅对单个字段建立索引:
相同的概念适用于多列索引:
当 order 为 val1,val2
当 order 为 val2,val1
如果两个字段完全匹配,则在这种情况下索引的顺序并不重要。
A multi-column index is conceptually no different than taking all the columns fields and concatinating them together -- indexing the result as a single field.
Since indexes are b-trees they are always searched left to right. You have to begin your search from the left to pair down results as you move to the right for the index to do its job and provide useful results.
With only a single field indexed:
The same concept is applied for multi-column indexes:
When order is val1,val2
When order is val2,val1
If both fields are matched exactly order of indexes does not matter in that case.
你拥有的是一个综合索引。当 WHERE 子句未使用复合索引中的所有列时,顺序很重要。
考虑这个查询:
为了了解可能考虑使用什么索引,请从左到右读取复合索引中的列。如果在读取查询中的所有列之前查询中不存在该列,则不会使用索引。
IDX_IndexTables_1
(val1, val2):从左到右读取 val1 存在,并且它是我们唯一的列,因此该索引将被视为IDX_IndexTables_2
(val2, val1):从左读取右边的 val2 在此查询中不存在,因此不会使用它。What you have is a composite index. The order is important when your WHERE clause is not using all columns in the composite index.
Consider this query:
In order to know what index might be considered read from left to right the columns in your composite indexes. If the column doesn't exist in your query before you read all the columns in your query then the index won't be used.
IDX_IndexTables_1
(val1, val2): Reading from left to right val1 exists and it is our only column so this index would be consideredIDX_IndexTables_2
(val2, val1): Reading from left to right val2 doesn't exist in this query so it won't be used.前面的答案描述了如何使用每个索引的第一列。 (在 where 子句中)。
我认为指出第二列很有用也很重要,因为它可能会提高涉及第二列的查询的性能。
以下查询将仅通过 IDX_1 上的索引查找来完成,从而保存对基表的有价值的查找(因为 val2 已经是索引的一部分)。
同样,反向索引将优化此查询:
但是,只需要两个索引中的一个(无论是哪一个)来优化以下查询:
这表明,根据您的表收到的查询,可能会出现以下情况:拥有这两个索引的正当理由。
The previous answers describe how to use the first column of each index. (in the where clause).
I think it's also important to point out that the second column is useful because it potentially increases performance of queries that involve the second column.
The following query will be completed with JUST an index seek on IDX_1, saving valuable lookups to the base table (since val2 is already part of the index).
Likewise, the reversed index will optimize this query:
However, only one (it doesn't matter which) of the two indexes is need to optimize the following query:
This shows that, depending on the queries your table receives, there may be a legitimate reason to have both indexes.
其他人回答说他们是不同的,我同意。
不过,我会添加一些其他想法...
Other folk have answered that they are different, and I agree.
I'll add some other thoughts though...
是的。有一个区别。
复合索引
IDX_IndexTables_1
可用于在 where 子句中使用val1
列的任何查询。复合索引
IDX_IndexTables_2
可用于在 where 子句中使用val2
列的任何查询。因此,例如,
IDX_IndexTables_2
不能用于此查询(但可以使用 IDX_IndexTables_1):但可以用于此查询:
考虑复合索引的方法就像考虑纸质电话簿;它按姓氏列进行索引,然后按名字列进行索引:您可以按姓氏进行查找,但不能按名字本身进行查找。
Yes. There is a difference.
The composite index
IDX_IndexTables_1
can be used for any query where theval1
column is used in the where clause.The composite index
IDX_IndexTables_2
can be used for any query where theval2
column is used in the where clause.So, for instance
IDX_IndexTables_2
cannot be used for this query (but IDX_IndexTables_1 can be used):but can be used for this query:
The way to think about a composite index is think about a paper telephone directory; It is indexed by the surname column, and then the firstname column: you can look up by surname but not by firstname on its own.