Does Order of Fields of Multi-Column Index in MySQL Matter

前端 未结 3 958
一个人的身影
一个人的身影 2020-12-13 12:46

I know the importance of indexes and how order of joins can change performance. I\'ve done a bunch of reading related to multi-column indexes and haven\'t found the answer t

3条回答
  •  一生所求
    2020-12-13 12:50

    The general rule is that in a multi-column index, you want to put the most selective -- that is, the one that will give you fewest results -- first. So if you are creating a multiple-column index on a table with a status column of say 10 possible values, and also a dateAdded column, and you're typically writing queries like

    SELECT * FROM myTable WHERE status='active' and dateAdded='2010-10-01'
    

    ...then you'd want dateAdded first, because that would limit the scan to just a few rows rather than 10% (or whatever proportion are 'active') of your rows.

    This takes a fair bit of thought and tuning; you should check out the Lahdenmaki and Leach book.

提交回复
热议问题