Is an index on A, B redundant if there is an index on A, B, C?

后端 未结 5 2041
庸人自扰
庸人自扰 2021-02-02 18:30

Having years of experience as a DBA, I do believe I know the answer to the question, but I figured it never hurts to check my bases.

Using SQL Server, assuming I have a

5条回答
  •  遥遥无期
    2021-02-02 18:53

    Much of what I was thinking was written by Jonathan in a previous answer. Uniqueness, faster work, and one other thing I think he missed.

    If the first index is made A desc, B asc and second A asc, B asc, C asc, then deleting the first index isn't really a way to go, because the second one isn't a superset of the first one, and your query cannot benefit from the second index if ordering is as written in the first one.

    In some cases like when you use the first index, you can order by A desc, B asc (of course) and A asc, B desc, but you can also make a query that will use any part of that index, like Order by A desc.

    But a query like order by A asc, B asc, will not be 'covered' by the first index.

    So I would add up, you can usually delete the first index, but that depends on your table configuration and your query (and, of course, indexes).

提交回复
热议问题