简体   繁体   English

要包含在非聚集索引中的列

[英]Columns to include in nonclustered index

I was just reading from here https://stackoverflow.com/questions/6187904/hard-and-fast-rule-for-include-columns-in-index regarding included columns.我刚刚从这里阅读https://stackoverflow.com/questions/6187904/hard-and-fast-rule-for-include-columns-in-index关于包含的列。

n index is typically n 索引通常是

CREATE INDEX <name> ON <table> (KeyColList) INCLUDE (NonKeyColList)

Where:在哪里:

  • KeyColList = Key columns = used for row restriction and processing KeyColList = Key columns = 用于行限制和处理
    WHERE, JOIN, ORDER BY, GROUP BY etc WHERE、JOIN、ORDER BY、GROUP BY 等
  • NonKeyColList = Non-key columns = used in SELECT and aggregation (eg SUM(col)) after selection/restriction NonKeyColList = 非键列 = 在选择/限制后用于 SELECT 和聚合(例如 SUM(col))

Now let's say my query is:现在假设我的查询是:

SELECT  Col7 ,
        Col8 ,
        Col9 ,
        Col10
FROM    [MyTable] M
WHERE   Col1 =1
        AND Col2=2
        AND Col3=3                         
        AND Col4=4 
        AND Col5=5 
        AND Col6=6
GROUP BY Col7 ,
         Col8 ,
         Col9 ,
         Col10
ORDER BY Col8

What should be the index for me here in this case?在这种情况下,我的索引应该是什么? and second case where Col7 is primary key和 Col7 是主键的第二种情况

I would think you would want something like:我想你会想要这样的东西:

CREATE NONCLUSTERED INDEX MyIndex ON MyTable(Col1, Col2, Col3, Col4, Col5, Col6)
INCLUDE (Col7, Col8, Col9, Col10)

You're filtering on Col1-Col6 and retrieving Col7-Col10 .您正在过滤Col1-Col6并检索Col7-Col10 Not sure how this will work with the group by , though.不过,不确定这将如何与group by一起使用。 You may want to rewrite it as a DISTINCT since the exec plan and results are the same but it will be more readable.您可能希望将其重写为DISTINCT ,因为执行计划和结果是相同的,但它会更具可读性。

If Col7 is a primary key and has a clustered index, you can leave this like it is.如果Col7是主键并具有聚集索引,则可以保持原样。 Col7 will be included in the index without being specifically mentioned as a cluster key is in every non-clustered index as a row identifier, but keeping it in won't hurt as SQL will ignore it. Col7将被包含在索引中而没有特别提及,因为集群键在每个非聚集索引中作为行标识符,但保留它不会受到伤害,因为 SQL 会忽略它。

For this query it depends on how many rows are restricted by your where for each of col 1-6.对于此查询,它取决于第 1-6 列中的每一个的位置限制了多少行。 The best index is the one that is for the column that has lthe column that has the highest selectivity and returns the least rows per value.最佳索引是针对具有最高选择性并返回每个值的行数最少的列的索引。

the group by will mean that Col7 might benefit by being part of the index so rows come back grouped already. group by 将意味着 Col7 可能会因成为索引的一部分而受益,因此行已经分组返回。

If all of the Col 1-6 provide a large amount of the table then perhaps Col7,Col8 might better to get the group by correct.如果所有的 Col 1-6 都提供了大量的桌子,那么 Col7,Col8 可能会更好地获得正确的组。

But in any case you will need to test as it depends on the distribution of the data values.但无论如何,您都需要进行测试,因为它取决于数据值的分布。

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

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