I have two queries plus its own EXPLAIN
's results:
One:
SELECT *
FROM notifications
WHERE id = 5204 OR seen = 3
Benchmark (for 10,000 rows): 0.861
Two:
SELECT h.* FROM ((SELECT n.* from notifications n WHERE id = 5204)
UNION ALL
(SELECT n.* from notifications n WHERE seen = 3)) h
Benchmark (for 10,000 rows): 2.064
The result of two queries above is identical. Also I have these two indexes on notifications
table:
notifications(id) -- this is PK
notification(seen)
As you know, OR
usually prevents effective use of indexes, That's why I wrote second query (by UNION
). But after some tests I figured it out which still using OR
is much faster that using UNION
. So I'm confused and I really cannot choose the best option in my case.
Based on some logical and reasonable explanations, using union
is better, but the result of benchmark says using OR
is better. May you please help me should I use which approach?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…