SQL Server : How to decide which composite index to create?

Creating new indexes can be a significant change and it is important to note that adding unnecessary indexes is not a good strategy. While indexes lead to better READ performance they will reduce the WRITE performance on the tables.

If you have more than one SELECT query to tune and create indexes for then you have to analyze if there is one index that will satisfy all the queries. If one index won’t suffice for all the queries then find the least number of indexes and their definitions that will improve the performance of those queries.

To illustrate an example, I am going to use the following table

`CREATE TABLE [dbo].[table1](`

[col1] [int] NULL,

[col2] [int] NULL,

[col3] [int] NULL,

[col4] [int] NULL

`) ON [PRIMARY]`

Consider the following SELECT statements and lets see what kind of indexes will help.

1) select col1, col2 from table1 where col1=10

2) select col1, col2, col3 from table1 where col1=10 and col3=3

3) select col3, col2 from table1 where col3=12

4) select col1, col2, col3, col4 from table1 where col1=10 and col3=12 and col4=15

Note: Any column that is part of the selected columns but is not part of the conditions in the WHERE clause, can be part of the included columns of the index. That column does not need to be part of the columns on which the index is created. In all the above queries col2 is part of the selected columns but not part of the WHERE clause.

For the first query all you need is an index on col1 that also includes col2. This will result in index seeks.

`CREATE NONCLUSTERED INDEX [index1] ON [dbo].[table1]`
`( [col1] ASC )INCLUDE( [col2])`

For the second query you will need an index on col1 and col3. Also include col2 in that index.

`CREATE NONCLUSTERED INDEX [index2] ON [dbo].[table1]`
`( [col1] ASC, [col3] ASC ) INCLUDE( [col2])`

Note that since the first column in index2 is col1, it will lead to index seeks for the first query. Hence, index2 will make index1 redundant.

But for the third query the index ‘index2’ will not help. This query will lead to index scans. The order of the columns in a composite index is important. In index2, col3 is not the first column in the order hence it will lead to index scans for any query which has a WHERE clause on just the col3. So the following index would be appropriate. This index will be created on col3 and include col2.

`CREATE NONCLUSTERED INDEX [index3] ON [dbo].[table1]`
`( [col3] ASC ) INCLUDE( [col2])`

Now consider the 4th query, to force an index seek you will have to create the following index.

`CREATE NONCLUSTERED INDEX [index4] ON [dbo].[table1]`
`( [col1] ASC, [col3] ASC, [col4] ASC ) INCLUDE( [col2])`

Notice that index4 will lead to index seeks for query 1 and 2. So there is no need create index1 and index2

If you analyze the indexes closely you will see that Index4 will be sufficient for Queries 1, 2 and 4.

To force index seeks for all the queries mentioned above Index4 and Index 3 will be sufficient.
{"ft_lang_label":"__label__en","ft_lang_prob":0.84669703,"math_prob":0.82930046,"size":2975,"snap":"2021-31-2021-39","text_gpt3_token_len":762,"char_repetition_ratio":0.15079097,"word_repetition_ratio":0.03787879,"special_character_ratio":0.26016808,"punctuation_ratio":0.088435374,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9668209,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-09-26T11:52:31Z\",\"WARC-Record-ID\":\"<urn:uuid:60cfc70a-fe1f-465c-9f2c-7b82fbff36b2>\",\"Content-Length\":\"208128\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:3e86606a-cb6e-471a-82ab-0670a64f0947>\",\"WARC-Concurrent-To\":\"<urn:uuid:bc58d7cc-07fd-478a-98a8-1a479f670c11>\",\"WARC-IP-Address\":\"161.35.229.227\",\"WARC-Target-URI\":\"https://www.xtivia.com/blog/composite-indexes/\",\"WARC-Payload-Digest\":\"sha1:K3BWWWEWA5NMSLDTJAEYMU7KXUYNKQ53\",\"WARC-Block-Digest\":\"sha1:GCEOLOEU47CMY3HY7DFG3X67MK2CZ6TK\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-39/CC-MAIN-2021-39_segments_1631780057861.0_warc_CC-MAIN-20210926114012-20210926144012-00654.warc.gz\"}"}