Databases · Senior · Question 53 of 75
Why might a query using OR across different columns be slow, and what can help?
SELECT * FROM users WHERE email = $1 OR phone = $2;- 1
ORalways forces a full table scan regardless of indexes - ✓Correct answer: One index can't serve both conditions; PostgreSQL may use a bitmap OR of two indexes, or you can rewrite as
UNION - 3
ORis not allowed inWHERE
Explanation
With indexes on both columns, a BitmapOr combines them. Without one of them, the planner falls back to scanning the table.