// troubleshooting guide

Index merge and BitmapAnd plans

The plan reads two single-column indexes and intersects or unites what they return. How MySQL index_merge and PostgreSQL BitmapAnd and BitmapOr show up, why an intersection usually means a composite index is missing, and why a union is usually fine.

Covers MySQL PostgreSQL

The symptom

When a query filters on two columns and each has its own index, the engine can read both indexes and combine the row lists. MySQL calls this index_merge and names the method in the Extra column: Using intersect(...) 用于 AND, Using union(...) 或者 Using sort_union(...) 用于 OR. PostgreSQL shows a BitmapAnd 或者 BitmapOr node over two Bitmap Index Scan nodes.

An intersection reads every matching entry from both indexes to keep the few that are in both. One index on both columns would go straight to those rows, which is why the query plan viewer flags it. A union is different: no single ordinary index can answer an OR across two columns, and the merge is often the best plan there is. The examples use a shop schema with 400,000 orders.

Diagnose it

MySQL: type index_merge

With separate indexes on customer_id 和 status, a filter on both columns intersects them:

CREATE INDEX orders_customer_idx ON orders (customer_id);
CREATE INDEX orders_status_idx ON orders (status);

EXPLAIN
SELECT order_id, total FROM orders WHERE customer_id = 1 AND status = 'cancelled'\G
 type: index_merge
  key: orders_status_idx,orders_customer_idx
 rows: 4460
Extra: Using intersect(orders_status_idx,orders_customer_idx); Using where

EXPLAIN ANALYZE shows what the intersection costs: how many entries each index returned against how many rows survived.

EXPLAIN ANALYZE
SELECT order_id, total FROM orders WHERE customer_id = 1 AND status = 'cancelled';
-> Filter: ((orders.`status` = 'cancelled') and (orders.customer_id = 1))  (cost=1536 rows=4460) (actual time=0.0511..11.3 rows=4000 loops=1)
    -> Intersect rows sorted by row ID  (cost=1536 rows=4461) (actual time=0.0465..11 rows=4000 loops=1)
        -> Index range scan on orders using orders_status_idx over (status = 'cancelled')  (cost=0.0105..236 rows=22534) (actual time=0.0203..1.52 rows=12000 loops=1)
        -> Index range scan on orders using orders_customer_idx over (customer_id = 1)  (cost=988e-6..78 rows=78912) (actual time=0.0151..5.05 rows=39998 loops=1)

The two index scans return about 12,000 and 40,000 entries to produce 4,000 rows. MariaDB prints the same index_merge type and Using intersect(...) text for this query.

PostgreSQL: BitmapAnd

PostgreSQL builds a bitmap of matching row locations from each index and combines the bitmaps before it visits the table:

CREATE INDEX orders_customer_idx ON orders (customer_id);
CREATE INDEX orders_status_idx ON orders (status);

EXPLAIN (ANALYZE, BUFFERS)
SELECT order_id, total FROM orders
WHERE customer_id BETWEEN 2 AND 600 AND status = 'cancelled';
Bitmap Heap Scan on orders  (cost=287.93..1305.93 rows=327 width=10) (actual time=0.725..0.937 rows=240.00 loops=1)
  Recheck Cond: (((status)::text = 'cancelled'::text) AND (customer_id >= 2) AND (customer_id <= 600))
  Heap Blocks: exact=240
  ->  BitmapAnd  (cost=287.93..287.93 rows=327 width=0) (actual time=0.690..0.690 rows=0.00 loops=1)
        ->  Bitmap Index Scan on orders_status_idx  (cost=0.00..135.32 rows=12120 width=0) (actual time=0.303..0.303 rows=12000.00 loops=1)
              Index Cond: ((status)::text = 'cancelled'::text)
        ->  Bitmap Index Scan on orders_customer_idx  (cost=0.00..152.19 rows=10777 width=0) (actual time=0.252..0.252 rows=10800.00 loops=1)
              Index Cond: ((customer_id >= 2) AND (customer_id <= 600))

阅读 rows of each Bitmap Index Scan against the rows of the Bitmap Heap Scan on top: 12,000 and 10,800 index entries for 240 rows. The BitmapAnd node itself always reports rows=0; a bitmap has no row count.

OR produces a union

The same two indexes serve an OR, and here each index contributes rows the other cannot find:

EXPLAIN
SELECT order_id, total FROM orders WHERE customer_id = 4242 OR status = 'pending'\G
 type: index_merge
  key: orders_customer_idx,orders_status_idx
 rows: 4020
Extra: Using union(orders_customer_idx,orders_status_idx); Using where
EXPLAIN (ANALYZE, BUFFERS)
SELECT order_id, total FROM orders WHERE customer_id = 4242 OR status = 'pending';
Bitmap Heap Scan on orders  (cost=53.44..4675.69 rows=4074 width=10) (actual time=0.323..1.810 rows=4020.00 loops=1)
  Recheck Cond: ((customer_id = 4242) OR ((status)::text = 'pending'::text))
  Heap Blocks: exact=2572
  ->  BitmapOr  (cost=53.44..53.44 rows=4074 width=0) (actual time=0.149..0.149 rows=0.00 loops=1)
        ->  Bitmap Index Scan on orders_customer_idx  (cost=0.00..4.58 rows=21 width=0) (actual time=0.010..0.010 rows=20.00 loops=1)
              Index Cond: (customer_id = 4242)
        ->  Bitmap Index Scan on orders_status_idx  (cost=0.00..46.82 rows=4053 width=0) (actual time=0.138..0.139 rows=4000.00 loops=1)
              Index Cond: ((status)::text = 'pending'::text)

Fix it

Replace an intersection with a composite index

Create one index with both columns. Put the column compared with = first and the range column after it:

CREATE INDEX orders_customer_status_idx ON orders (customer_id, status);

EXPLAIN ANALYZE
SELECT order_id, total FROM orders WHERE customer_id = 1 AND status = 'cancelled';
-> Index lookup on orders using orders_customer_status_idx (customer_id=1, status='cancelled')  (cost=1400 rows=4000) (actual time=0.295..3.41 rows=4000 loops=1)
CREATE INDEX orders_status_customer_idx ON orders (status, customer_id);

EXPLAIN (ANALYZE, BUFFERS)
SELECT order_id, total FROM orders
WHERE customer_id BETWEEN 2 AND 600 AND status = 'cancelled';
Index Scan using orders_status_customer_idx on orders  (cost=0.42..644.05 rows=327 width=10) (actual time=0.016..0.231 rows=240.00 loops=1)
  Index Cond: (((status)::text = 'cancelled'::text) AND (customer_id >= 2) AND (customer_id <= 600))
  Buffers: shared hit=240 read=3

The index now returns exactly the rows the query keeps. Once the composite index exists, a single-column index on its leading column is redundant, because the composite index serves the same lookups. The redundant prefix index snippet finds those.

Leave a union alone, or split the query

对于 OR across two columns the merge is usually right. If the plan falls back to a full scan instead, write the two halves as separate queries so each uses its own index. UNION removes the rows that match both conditions:

EXPLAIN FORMAT=TREE
SELECT order_id, total FROM orders WHERE customer_id = 4242
UNION
SELECT order_id, total FROM orders WHERE status = 'pending';
-> Table scan on <union temporary>  (cost=1809..1862 rows=4020)
    -> Union materialize with deduplication  (cost=1809..1809 rows=4020)
        -> Index lookup on orders using orders_customer_idx (customer_id=4242)  (cost=7 rows=20)
        -> Index lookup on orders using orders_status_idx (status='pending')  (cost=1400 rows=4000)

MySQL: turn the intersection off for one session

To compare the plan without the merge before you build an index, switch the method off for your session:

DROP INDEX orders_customer_status_idx ON orders;
SET SESSION optimizer_switch = 'index_merge_intersection=off';

EXPLAIN
SELECT order_id, total FROM orders WHERE customer_id = 1 AND status = 'cancelled'\G

SET SESSION optimizer_switch = 'index_merge_intersection=default';
 type: ref
  key: orders_status_idx
 rows: 22534
Extra: Using where

When to leave it alone

Leave an intersection when the query is rare, or when the two columns are filtered in many different combinations and one composite index per combination would be too many indexes on a table that takes writes. Leave a union when it reads few rows. Build the composite index when the same pair of columns is filtered together all day and each single index returns many times more entries than the query keeps.

The checklist

Work through it in order. Each step gives you the evidence for the next one.

  1. Find index_merge in the type column (MySQL) or BitmapAnd / BitmapOr (PostgreSQL).
  2. Note whether it is an intersection (AND) or a union (OR).
  3. For an intersection, compare the entries each index returns with the rows the query keeps.
  4. Create a composite index: equality columns first, the range column last.
  5. Re-capture the plan and confirm a single index read.
  6. Drop a single-column index that the composite index now makes redundant.

Questions that come up

Is index merge bad?

Not by itself. An intersection means the engine read two indexes to do the work one composite index would do, so it is a hint that an index is missing. A union for an OR condition is often the best available plan.

What does Using intersect mean in MySQL EXPLAIN?

MySQL scanned the indexes named in the brackets, kept the row ids that appear in all of them, and then read those rows. It appears for AND conditions when each column has its own index and no index contains them together.

Why does BitmapAnd show rows=0 in PostgreSQL?

A BitmapAnd or BitmapOr node produces a bitmap of row locations, not rows, so EXPLAIN ANALYZE reports zero for it. Read the row counts on the Bitmap Index Scan nodes below it and on the Bitmap Heap Scan above it.

Which column goes first in the composite index?

Columns compared with = go first, and a column filtered by a range goes after them. With two equality columns either order serves this query, so lead with the column that other queries filter on alone.

Can I keep the single-column indexes?

Keep the one that is not the leading column of the new composite index if other queries filter on that column alone. The single-column index on the leading column duplicates the composite index and only adds write cost.

// fix it where the query lives

Run these queries in SQLly

SQLly is a native SQL workbench for macOS, Windows and Linux. The query plan viewer and formatter these guides link to are built into its editor.