MySQL index for MIN and MAX

前端 未结 2 2215
误落风尘
误落风尘 2021-02-15 12:38

Could anyone clarify this point from the official MySQL documentation

Indexes are used ... To find the MIN() or MAX() value for a specific indexed column

2条回答
  •  一生所求
    2021-02-15 13:20

    In their example they are not trying to find the min and max values of the column called key_col. They are trying to find the min and max of the second key in a multi-column key. The issue is further complicated since the query is limiting its search to the subset of the multi-column key set that is defined by having the key_part1 = 10.

    10, 2, 3
    10, 9, 5
    10, 7, 7
    10, 3, 1
    10, 2, 5
    11, 3, 0
    11, 0, 3
    

    So in this example the query selects only the rows where the first column is 10. Then it finds that among the rows with 10 in the first column the min for the second column is 2 and the max is 9. Notice that 0 in the second column is not chosen as the min because the first column does not match on 10. Hope that helps.

提交回复
热议问题