The 30 second answer
An index on one column can't answer a condition that's true when either of two columns matches. PostgreSQL can combine two indexes with a BitmapOr, but only if each side of the OR has a usable index; one unindexed side forces a sequential scan. I make sure each branch is indexed, rewrite same-column ORs as IN or ANY, or split the query into a UNION ALL with a duplicate guard.
From the basics. WHERE brand_id = 7 AND category_id = 3 narrows with each condition. WHERE brand_id = 7 OR name ILIKE '%boot%' widens: a row qualifies through either branch. An index on brand_id finds only the first group, so unless the second branch also has an index, PostgreSQL has to check every row.
Combining indexes. The docs describe how PostgreSQL "can form AND and OR conditions across several index scans", for example breaking x = 42 OR x = 47 OR x = 53 OR x = 99 into four index scans whose results "are then ORed together". In a plan this is a BitmapOr feeding a Bitmap Heap Scan.
PostgreSQL 18 change. The release notes add "Allow transforming OR-clauses to arrays for faster index processing", so several ORs on the same column can become one = ANY(...) index condition automatically. Writing IN yourself does the same in every version.
Badly written. One branch has no usable index, and the ORs on status are spelled out:
SELECT id FROM orders
WHERE customer_id = 42
OR (status = 'NEW' OR status = 'PROCESSING') AND created_at < now() - interval '1 day'
OR lower(coupon_code) = 'black-friday';Better. Each branch indexed and simplified, or split into a UNION:
-- Same-column ORs as IN
... WHERE status IN ('NEW', 'PROCESSING') ...
-- Different columns: one indexed query per branch
SELECT id FROM orders WHERE customer_id = 42
UNION
SELECT id FROM orders WHERE status IN ('NEW', 'PROCESSING') AND created_at < now() - interval '1 day'
UNION
SELECT id FROM orders WHERE lower(coupon_code) = 'black-friday'; -- needs an expression indexUNION removes duplicates (a row matching two branches). UNION ALL is cheaper if branches can't overlap, or if you add AND NOT (earlier branch) conditions.
Trade-offs.
- A bitmap scan reads matching pages in physical order, which is good for many rows but loses index order, so an
ORDER BYneeds a sort. - The
UNIONrewrite is more SQL to maintain. Use it whenEXPLAINshows the OR form going to a sequential scan. - "Search across several optional filters" endpoints often build ORs dynamically. Generating only the conditions the user actually set is better than
(:param IS NULL OR col = :param)patterns, which give the planner one generic plan for all cases.
Alternate interview scenarios
"An admin search does WHERE email = ? OR phone = ? OR name ILIKE ?. It's slow."
Index email and phone, give name a trigram index, then check that the plan uses a BitmapOr. If not, split into a UNION.
"A filter endpoint uses (:brand IS NULL OR brand_id = :brand) for ten optional filters."
Build the SQL with only the filters provided, for example with a criteria builder, so each combination gets a plan that uses its indexes.
Sources (3)
- PostgreSQL docs: Combining multiple indexespostgresql.org
- PostgreSQL 18 release notes (OR-clauses to arrays)postgresql.org
- PostgreSQL docs: Row and array comparisons (IN, ANY)postgresql.org