Sharon Ben-Moshe is the founder of sqlfmt, a browser-based SQL formatter and validator with misspelled-keyword suggestions across PostgreSQL, MySQL, SQLite, SQL Server, and BigQuery.
Learn how the MySQL Index Merge access method works, why combining single-column indexes causes CPU spikes, and how composite indexes resolve the issue.
Quick answer
MySQL Index Merge occurs when the optimizer scans multiple single-column indexes on the same table and merges their row pointers in memory using union, intersection, or sort-union algorithms. While designed as a fallback, Index Merge consumes heavy CPU and memory, and is almost always slower than a single composite index.
FAQs
Why does MySQL choose Index Merge instead of a single index?
The optimizer estimates that reading and intersecting two indexes will access fewer total rows than scanning a single index and evaluating the second condition on the table heap.
Is Index Merge ever faster than a composite index?
No. A composite index allows MySQL to evaluate both conditions within a single B-tree scan, which is always faster than scanning two B-trees and merging pointers.
What does "Using union" mean in MySQL EXPLAIN Extra?
It means the query contains an OR condition between two indexed columns, and MySQL is merging the row IDs from both index scans while removing duplicates.
Can Index Merge use indexes from different tables?
No. Index Merge operates exclusively on multiple indexes defined on a single table within a query block.
Build fast text search directly in PostgreSQL without external search engines. Master to_tsvector, to_tsquery, relevance ranking with ts_rank, and GIN indexes.
When querying a table with multiple filter conditions, developers often expect MySQL to use multiple single-column indexes simultaneously. For example, if you have an index on status and another index on created_at, you might assume MySQL scans both and combines the results effortlessly.
MySQL does have an optimizer feature called Index Merge designed for this scenario. However, seeing Using intersect or Using union in your EXPLAIN output is frequently a warning sign rather than a performance victory. Index Merge introduces high memory and CPU overhead that a composite index solves with a single lookup.
How the MySQL Index Merge access method works
In standard query execution, MySQL chooses the single best index for a table, scans its B-tree, and retrieves matching row pointers. If your query filters on two columns with separate indexes, the optimizer must either pick one index and filter the remaining conditions on the heap, or trigger Index Merge.
Under Index Merge, the storage engine scans both indexes independently, retrieves row IDs (primary key values in InnoDB), sorts them, and merges them in memory using set operations. According to the MySQL index merge optimization documentation, MySQL employs three distinct merge algorithms:
Intersection: Used with AND conditions; reads pointers from both indexes and keeps only row IDs present in both sets.
Union: Used with OR conditions; reads pointers from both indexes and combines them, removing duplicate row IDs.
Sort-Union: Used when index scans produce unsorted row IDs; sorts row IDs in temporary memory buffers before executing the union.
Why Index Merge causes production CPU spikes
The primary issue with Index Merge is the overhead of in-memory set operations. If your table has 5 million rows, an index scan on status = 'active' might return 2 million row IDs. An index scan on category_id = 12 might return 500,000 row IDs.
MySQL must allocate memory to hold 2.5 million row pointers, sort them, and perform millions of equality comparisons to compute the intersection. This causes severe CPU spikes and memory consumption. If the row list exceeds sort_buffer_size, MySQL spills intermediate data to temporary disk tables, slowing query execution drastically.
Identifying Index Merge in MySQL EXPLAIN
You can identify Index Merge by inspecting the type and Extra columns in your execution plan. Read our guide to SQL indexing strategies for developers to learn how to interpret index access types.
Detecting Index Merge in EXPLAIN output
-- Query filtering on two independent indexed columns:EXPLAIN SELECT id, order_number, total_amountFROM customer_ordersWHERE status = 'pending' AND customer_id = 4120;-- Typical Index Merge execution plan:-- *************************** 1. row ***************************-- id: 1-- select_type: SIMPLE-- table: customer_orders-- partitions: NULL-- type: index_merge-- possible_keys: idx_orders_status,idx_orders_customer-- key: idx_orders_status,idx_orders_customer-- key_len: 2,4-- ref: NULL-- rows: 324-- filtered: 100.00-- Extra: Using intersect(idx_orders_status,idx_orders_customer); Using where
The type: index_merge and Extra: Using intersect(...) indicate that MySQL is scanning two separate B-trees and merging row pointers in memory. Consult the MySQL EXPLAIN documentation for detailed output flag specifications.
The solution: replace single-column indexes with a composite index
A composite (multi-column) index resolves the problem by organizing both columns into a single B-tree index structure. MySQL navigates directly to the exact leaf page where both conditions are satisfied simultaneously, requiring zero in-memory merging or sorting.
Replacing single indexes with a composite index
-- Drop individual indexes or keep them only if queried separately:-- DROP INDEX idx_orders_status ON customer_orders;-- DROP INDEX idx_orders_customer ON customer_orders;-- Create composite index covering both predicates:CREATE INDEX idx_orders_customer_statusON customer_orders (customer_id, status);-- Re-run EXPLAIN with composite index:EXPLAIN SELECT id, order_number, total_amountFROM customer_ordersWHERE status = 'pending' AND customer_id = 4120;-- Optimized execution plan:-- type: ref-- key: idx_orders_customer_status-- key_len: 6-- Extra: Using index condition
Notice the difference: type shifts from index_merge to ref. The database reads a single index, cuts disk page reads to a minimum, and eliminates CPU overhead entirely.
Disabling Index Merge when the optimizer makes bad choices
In some edge cases, the MySQL cost optimizer chooses Index Merge when a single index scan with a heap filter would have been significantly faster. You can disable Index Merge globally or at the session level using optimizer_switch.
Disabling index merge via optimizer switch
-- Disable index merge intersection for current sessionSET SESSION optimizer_switch = 'index_merge_intersection=off';-- Query now picks the single best index:SELECT id, order_numberFROM customer_ordersWHERE status = 'pending' AND customer_id = 4120;
Actionable checklist for fixing Index Merge bottlenecks
Run EXPLAIN: search slow query logs for queries reporting type: index_merge in EXPLAIN output.
Create composite indexes: add multi-column indexes matching the combined filter conditions in the query.
Order composite keys wisely: place the highest-cardinality equality column first in the composite index definition.
Check sort buffer memory: ensure sort_buffer_size is sufficient to avoid disk spillage during necessary merges.
Test optimizer switches: test index_merge_intersection=off in staging to see if single index scans perform better.
Drop redundant single indexes: remove single-column indexes rendered obsolete by the new composite index.
Wrapping up
While the MySQL Index Merge optimization serves as a useful fallback when appropriate indexes are missing, it introduces significant CPU and memory overhead on high-concurrency production systems. Replacing multiple single-column indexes with a properly ordered composite index provides predictable, sub-millisecond execution. Keep your index creation scripts and queries clean and readable by formatting your SQL with sqlfmt.