Can MySQL use multiple indexes for a single query?

后端 未结 4 1999
[愿得一人]
[愿得一人] 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:33

    Mysql can use index merge to merge the results of two indexes. But this is not really the preferred way of mysql. It will use two indexes if that optimizes the query execution. but this is also a hint for the query developer to create a composite index.

    An index merge is by no means equivalent to a composite index. here is an excerpt from Baron Schwartz book -

    The index merge strategy sometimes works very well, but it’s more common for it to actually be an indication of a poorly indexed table:

    • When the server intersects indexes (usually for AND conditions), it usually means that you need a single index with all the relevant columns, not multiple indexes that have to be combined.
    • When the server unions indexes (usually for OR conditions), sometimes the algorithm’s buffering, sorting, and merging operations use lots of CPU and memory resources. This is especially true if not all of the indexes are very selective, so the scans return lots of rows to the merge operation.

提交回复
热议问题