MySQL Index Merge Optimization Explained

This article explores how the MySQL query optimizer evaluates and applies index merge optimizations to improve query performance. It covers the different types of index merges—intersection, union, and sort-union—how the cost-based optimizer decides to use them, and the practical limitations of this execution strategy.

What is MySQL Index Merge?

The Index Merge optimization method allows MySQL to retrieve rows from a single table using multiple index scans. The optimizer merges the row IDs (or primary keys) retrieved from these different index scans into a single result set before fetching the actual row data from the table.

Historically, MySQL could only use one index per table access in a query. Index Merge relaxes this limitation, allowing the database to resolve complex AND and OR queries more efficiently without relying solely on composite indexes.

How the Optimizer Evaluates Index Merge

MySQL uses a Cost-Based Optimizer (CBO) to determine the most efficient execution plan for a query. To evaluate whether to use an Index Merge, the optimizer performs the following steps:

  1. Candidate Index Identification: The optimizer analyzes the WHERE clause to identify which columns have indexes and can be used to filter the query.
  2. Cost Estimation of Single Indexes: The optimizer calculates the estimated cost (in terms of disk I/O and CPU cycles) of using the single best-performing index.
  3. Cost Estimation of Index Merge: The optimizer calculates the cost of scanning multiple indexes, sorting the retrieved row IDs (if necessary), and merging them. The cost formula factors in:
    • The number of index rows to be scanned.
    • The CPU cost of intersecting or unionizing the keys.
    • The memory buffer requirements for the merge operation.
  4. Comparison and Selection: If the calculated cost of the Index Merge plan is lower than a single index scan, a full table scan, or a range scan, the optimizer selects the Index Merge strategy.

Index Merge Algorithms

Depending on the structure of the query and the logical operators used, the optimizer applies one of three index merge algorithms:

1. Intersection Algorithm

This algorithm is applied when the query filters data using AND conditions on different indexed columns.

2. Union Algorithm

This algorithm is used when the query contains OR conditions on different indexed columns.

3. Sort-Union Algorithm

This algorithm is similar to the Union algorithm but is utilized when the retrieved row IDs are not guaranteed to be in sorted order.

Limitations of Index Merge

While Index Merge can improve performance, it is not always the optimal choice. The optimizer may bypass it or suffer performance penalties due to the following factors: