Sql-server – Index creation with multiple columns – should I combine as many as possible

indexperformancequery-performancesql serversql-server-2016

When creating indexes and especially those with multiple columns, should I look to combine as many columns in the index as would be useful?

I've ran a script on the stats to pick up missing indexes and there's some suggestions such as:

CREATE INDEX [IX_Tbl_ContractId_OpenClosedStatus] ON [tbl]
([ContractId], [OpenClosedStatus]) INCLUDE ([Id], [StringId], [DateNextDeadline])

And

CREATE INDEX [IX_Tbl_ContractId_OpenClosedStatus] ON [tbl]
([ContractId], [OpenClosedStatus]) INCLUDE ([Id], [DateNextDeadline])

And

CREATE INDEX [IX_Tbl_ContractId_OpenClosedStatus] ON [tbl]
([ContractId], [OpenClosedStatus]) INCLUDE ([Id], [ClientId], [DateNextDeadline])

They all have the same [Id] and [DateNextDeadline] columns in there, and they all are indexed on the same two main columns.

Should I, in this instance, create a single index with ([Id], [StringId], [ClientId], [DateNextDeadline]) and will SQL Server use the same column indexes to and pick the including columns depending on the query? Or would it only use the index if there's an exact match? So I would need to create three indexes?

Best Answer

Based on those 3 specific recommendations, I would use

CREATE INDEX [IX_Tbl_ContractId_OpenClosedStatus] ON [tbl]
([ContractId], [OpenClosedStatus]) INCLUDE ([Id], [ClientId], [DateNextDeadline],[StringID])

The equality/inequality columns are the same, which is the bigger part. SQL Server should use this for any queries that have all or a subset of those Included cols.

And of course, you run this through a rigorous set of test queries to verify :)