简体   繁体   English

SQL Server表索引列顺序

[英]SQL Server table index columns order

Is there any difference when I create a table index for more columns if I use the columns in different order? 如果我以不同顺序使用列,则为更多列创建表索引时会有什么区别?

Exactly is difference between ID, isValid, Created and ID, Created, isValid indices? ID, isValid, CreatedID, Created, isValid索引之间到底有什么区别?

And is there any difference in querying order? 查询顺序有什么不同吗?

where ID = 123 
  and isValid = 1 
  and Created < getdate()

vs.

where ID = 123 
  and Created < getdate() 
  and isValid = 1

Column types: ID [int] , isValid [bit] , Created [datetime] ) 列类型:ID [int] ,isValid [bit] ,Created [datetime]

Exactly is difference between ID, isValid, Created and ID, Created, isValid indices? ID, isValid, CreatedID, Created, isValid索引之间到底有什么区别?

If you always use all three columns in your WHERE clause - there's no difference. 如果您始终在 WHERE子句中使用 所有三列,则没有区别。
( as Martin Smith points out in his comment - since of the criteria is not an equality check, the sequence of the columns in the index does matter ) 正如马丁·史密斯Martin Smith)在其评论中所指出的那样-由于条件不是相等检查,因此索引各列的顺序确实很重要

However: an index can only ever used if the n left-most columns (here: n between 1 and 3) are used. 但是:只有使用了最左边的n列(此处为1到3之间的n),才能使用索引。

So if you have a query that might only use ID and isValid for querying, then the first index can be used - but the second one will never be used for sure. 因此,如果您的查询可能仅使用IDisValid进行查询,则可以使用第一个索引-但肯定不会使用第二个索引。

And if you have queries that use ID and Created as their WHERE parameters, then your second index might be used, but the first one can never be used. 如果你有使用的查询IDCreated作为他们的WHERE参数,那么可以使用你的第二个指标,但第一个不能被使用。

AND is commutative, so the order of ANDed expressions in WHERE doesn't matter. AND是可交换的,因此WHERE中ANDed表达式的顺序无关紧要。 Order of columns in an index does matter, it should match your queries. 索引列的顺序确实很重要,它应该与您的查询匹配。

If ID is your table's clustered primary key and your queries ask for specific ID , don't bother creating an index. 如果ID是表的群集主键,并且您的查询要求输入特定的ID ,则不必费心创建索引。 That would be like giving an index to a book saying "page 123 is on page 123" etc. 这就像给一本书的索引说“ 123页在123页上”等。

The order in the query makes no difference. 查询中的顺序没有区别。 The order in the index makes a difference. 索引中的顺序有所不同。 I'm not sure how good this will look in text but here goes: 我不确定这在文本中看起来有多好,但是可以这样:

where ID = 123 and isValid = 1 and Created < Date 'Jan 3'

Here are a couple of possible indexes: 以下是一些可能的索引:

ID   IsValid Created
===  ======= =========
122  0       Jan 4
122  0       Jan 3
...  ...     ...
123  0       Jan 4
123  0       Jan 3
123  0       Jan 2
123  0       Jan 1
123  1       Jan 4
123  1       Jan 3
123  1       Jan 2 <-- Your data is here...
123  1       Jan 1 <-- ... and here
...  ...     ...

ID   Created IsValid
===  ======= ========
122  Jan 4   0
122  Jan 4   1
...  ...     ...
123  Jan 4   0
123  Jan 4   1
123  Jan 3   0
123  Jan 3   1
123  Jan 2   0
123  Jan 2   1     <-- Your data is here...
123  Jan 1   0
123  Jan 1   1     <-- ... and here
...  ...     ...

As you can probably tell, creating an index(IsValid, Created, ID) or any other order, will separate your data even more. 如您所知,创建索引(IsValid,Created,ID)或任何其他顺序将进一步分离数据。 In general, you want to design the indexes to make your data as "clumpy" as possible for the queries executed most often. 通常,您希望设计索引,以使最常执行的查询的数据尽可能“笨拙”。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM