OR Across Two Columns Blocks One Index: Split It Into UNION ALL
A single index cannot serve WHERE email = ? OR phone = ? because the two conditions live in different orderings, so the engine falls back to a scan or a bitmap combination. Writing it as two indexed queries joined by UNION ALL lets each half use its own index. PostgreSQL 18 handles the easier case of several ORs on the same column by turning them into = ANY(array), which an index can serve directly, so this rewrite is only for ORs that span different columns.
Questions this Concept answers
- Why can a single composite index not serve an `OR` across two different columns?
Rewriting OR Predicates as UNION ALL to Keep Index Seeks in SQL Server
A slow query whose `WHERE` clause combines predicates with `OR` often loses its index seek because the SQL Server cost-based optimizer treats the disjunction as a single access path and, past a cost …