Problem Statement
When can rewriting a wide OR predicate as UNION ALL improve performance?
Explanation
Wide ORs often disable index seeks. Splitting into separate queries lets each branch use the best index, then UNION ALL combines results.
Add DISTINCT only if needed, since it adds sort or hash work. Ensure predicates are mutually exclusive or dedupe explicitly.
Code Solution
SolutionRead Only
(SELECT * FROM users WHERE email = $1) UNION ALL (SELECT * FROM users WHERE phone = $2)
Practice Sets
This question appears in the following practice sets:
