Difference between 2 indexes with columns defined in reverse order

后端 未结 5 1956
梦毁少年i
梦毁少年i 2021-01-02 04:07

Are there any differences between following two indexes?

  • IDX_IndexTables_1
  • IDX_IndexTables_2

If there are any, what are the difference

5条回答
  •  抹茶落季
    2021-01-02 05:00

    A multi-column index is conceptually no different than taking all the columns fields and concatinating them together -- indexing the result as a single field.

    Since indexes are b-trees they are always searched left to right. You have to begin your search from the left to pair down results as you move to the right for the index to do its job and provide useful results.

    With only a single field indexed:

    WHERE val1 LIKE 'myvalue%' (uses index)
    WHERE val1 LIKE '%myvalue' (cannot use index)
    

    The same concept is applied for multi-column indexes:

    When order is val1,val2

    WHERE val1='value1' (uses index)
    WHERE val2='value2' (cannot use index)
    

    When order is val2,val1

    WHERE val1='value1' (cannot use index)
    WHERE val2='value2' (uses index)
    

    If both fields are matched exactly order of indexes does not matter in that case.

    WHERE val1='value1' AND val2='value2' (uses index in any order)
    

提交回复
热议问题