Can MySQL use multiple indexes for a single query?

后端 未结 4 2000
[愿得一人]
[愿得一人] 2020-12-02 09:32

Imagine a table with multiple columns, say, id, a, b, c, d, e. I usually select by id, however, there are multiple queries in the client app that u

4条回答
  •  盖世英雄少女心
    2020-12-02 09:59

    Yes, MySQL can use multiple index for a single query. The optimizer will determine which indexes will benefit the query. You can use EXPLAIN to obtain information about how MySQL executes a statement. You can add or ignore indexes using hints like so:

    SELECT * FROM t1 USE INDEX (i1) IGNORE INDEX FOR ORDER BY (i2) ORDER BY a;
    

    I would suggest reading up on how MySQL uses indexes.

    Just a few excerpts:

    If there is a choice between multiple indexes, MySQL normally uses the index that finds the smallest number of rows.

    If a multiple-column index exists on col1 and col2, the appropriate rows can be fetched directly. If separate single-column indexes exist on col1 and col2, the optimizer will attempt to use the Index Merge optimization (see Section 8.2.1.4, “Index Merge Optimization”), or attempt to find the most restrictive index by deciding which index finds fewer rows and using that index to fetch the rows.

提交回复
热议问题