Turning Learners Into Developers
Codekilla
CODEKILLA
Medium
SQL-O08sql

When `OR` Disables Index — Rewrite With UNION

Problem

`SELECT * FROM users WHERE email = ? OR phone = ?` may table-scan even when both columns are indexed. Why, and how does UNION ALL fix it?

Input
Conceptual + SQL.
Output
Rewritten query
Constraints
—
Sample input
—
Sample output
OR predicates across different indexed columns force the planner to choose one or scan; UNION ALL of two index seeks beats it.
Explanation
OR can defeat index intersection; UNION ALL is two cheap seeks.
optimisationor-vs-union@DoorDash
Visible test cases
in: explain
out: ok
Your solution — run it, use AI if stuck
sql