简体   繁体   中英

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.

n index is typically

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

Where:

  • KeyColList = Key columns = used for row restriction and processing
    WHERE, JOIN, ORDER BY, GROUP BY etc
  • NonKeyColList = Non-key columns = used in SELECT and aggregation (eg SUM(col)) after selection/restriction

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

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 . Not sure how this will work with the group by , though. You may want to rewrite it as a DISTINCT since the exec plan and results are the same but it will be more readable.

If Col7 is a primary key and has a clustered index, you can leave this like it is. 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.

For this query it depends on how many rows are restricted by your where for each of col 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.

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.

But in any case you will need to test as it depends on the distribution of the data values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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