4

In looking through some of our databases, in the past we were perhaps a bit eager to take any suggestion from the DTA or the estimated query plan, and so we have a lot of indexes and many of them similar to others. By "similar" I mean they have the same key, but different or overlapping includes. Here are some examples of 3 indexes I would describe as "similar":

CREATE INDEX Index1 ON Table1 (Col1)

CREATE INDEX Index2 ON Table1 (Col1) INCLUDES (Col2)

CREATE INDEX Index3 ON Table1 (Col1) INCLUDES (Col3)

My expectation is that consolidating these 3 into 1 index (ie. CREATE INDEX Index4 ON Table1 (Col1) INCLUDES (Col2, Col3)) would clean things up and improve insert/update/delete on the table without harming query performance. Is that accurate? Is there any better or more efficient way to go about this than to drop the existing similar indexes and then creating the new index?

Thanks!

1 Answer 1

8

Consildation of overlapping indexes is typically a good practice.

  1. You'll see improved performance on inserts, due to fewer index write per insert.
  2. Deletes will be faster, as fewer indexes need to be deleted.
  3. In most cases, updates will be faster too, depending on the columns being updated.
  4. You consume less disk space due to less duplicating of data.

Those points alone should likely help over performance of the table, as those locks are held for shorter periods of time. A side effect of shorter locks could also possibly lead to improvement in performance of selects against the table, as they may be less likely to run into locks.

Additionally, the order of included columns doesn’t have an impact on performance, as these columns are not sorted in the index. In other words, a query attempting to read col3 from and index where col3 is the first included column would perform the same as reading from an index where it's the second included column.

In the example you provided, Index4 would likely be the best index to use for consolidation of the initial 3 indexes.

Other points to consider before consolidating indexes

  1. As you add more columns to an index, you do increase the size of the index, leading to potentially slower reads from the index.
  2. Review the data types of the columns being consolidated.
  3. Review the read/writes patterns against the table and the affected indexes.
  4. Review query plans before and after the change.

For example, if Index A may be read from 1,000 time per minute and have very few small columns, such as varchar(20). Index B may be read from once a month during reporting and have column with a data type of varchar(2000). In this case, consolidation would likely have a negative impact on overall performance during regular production query times, with little to no benefit on the monthly reporting job.

With that said, index consolidation still makes sense the majority of the time. But that's just a guideline, not a rule. You need to review all factors before making the final decision, and decide what you're goal is in that scenario. Brent Ozar often suggests a guideline of staring with no more than 5 columns per index, and no more than 5 indexes per table. Ronaldo actually linked to a good article in the comments from Brent Ozer on SQL Server Index Tuning Tip: Identify Overlaps

3
  • 1
    Agreed. I would also recommend reading Brent Ozar's article SQL Server Index Tuning Tip: Identify Overlaps.
    – Ronaldo
    Jun 29, 2022 at 0:10
  • 1
    Queries will run slower at the cost of additional reads due to size increase and locks when editing included columns. Consolidation of indexes - saving space at the expense of performance. Jun 29, 2022 at 4:02
  • 1
    @VitaliyZlobin The affect on reads would likely be negligible, at best. Though it's hard to say without knowing the data types fo all columns defined, read/write patterns, etc... It's certainly something to consider when consolidating indexes, thought it's likely to still be the right path 99 out of 100 times. Any increase in page size due to the consolidated indexes would likely be offset by the improved performance on inserts, updated and deletes. Jun 29, 2022 at 10:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.