Skip to content
Advertisement

Two indexes for same column and change the order

I have a large table in Microsoft SQL Server 2008. It has two indexes. One index having column A descending order and another index having the column A ascending with some other columns.

My application is doing below:

  1. Select for the record.
  2. If there is no record then insert
  3. If find then update the records

Note that this table has millions of records.

The question is: Are these indexes affect the any select/insert/update performance?

Any suggestions?

Advertisement

Answer

Having the exact same two indexes with the only difference being the ordering will make no difference to the SQL engine and will just pick either (practially).

Imagine you 2 dictionaries of the english language, one sorts words from A to Z and the other from Z to A. The effort you will need to search for a particular word will be roughly the same in both cases.

A different case would be if you had 2 lists of people’s data, one ordered by first name then last name and the other by last name first, then first name. If you have to look for “John Doe”, the one that’s ordered first by last name will be practically useless compared to the other one.

These examples are very simplified representations of indexes on SQL Server. Indexes store their data on a structure that’s called a B-Tree, but for searching purposes these examples work to understand when will a index be useful or not.

Resuming: you can drop the first index and keep the one that has additional columns on it, since it can be used for more different scenarios and also all cases that would require the other one. Keeping an unuseful index brings additional maintenance tasks like keeping the index updated on every insert, update, delete and refreshing statistics, so you might want to drop it.

PD: As for the 2nd index being used, this greatly depends on the query you are using to access that particular table, it’s joins and where clauses. You can use the “Display Estimated Execution Plan” having highlighted the query on SSMS to see the access plan of the engine to each object to perform the operation. If the index is used, you will see it there.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement