Multiple and single indexes

后端 未结 4 918
忘了有多久
忘了有多久 2021-01-30 17:08

I\'m kinda ashamed of asking this since I\'ve been working with MySQL for years, but oh well.

I have a table with two fields, a and b. I will b

4条回答
  •  长发绾君心
    2021-01-30 17:45

    Yes, at least one case is considerably slower. If you only define the following index:

    ALTER TABLE ... ADD INDEX (a, b);
    

    ... then the query SELECT * FROM ... WHERE B = 1; will not use that index.

    When you create an index with a composite key, the order of the columns of the key is important. It is recommended to try to order the columns in the key to enhance selectivity, with the most selective columns to the left-most of the key. If you don't do this, and put a non-selective column as the first part of the key, you risk not using the index at all. (Source: Tips on Optimizing SQL Server Composite Index)

提交回复
热议问题