Conceptual
Login

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?