I have two queries that I'm UNION
ing together such that I already know there will be no duplicate elements between the two queries. Therefore, UNION
and UNION ALL
will produce the same results.
Which one should I use?
You should use the one that matches the intent of what you are looking for. If you want to ensure that there are no duplicates use UNION
, otherwise use UNION ALL
. Just because your data will produce the same results right now doesn't mean that it always will.
That said, UNION ALL
will be faster on any sane database implementation, see the articles below for examples. But typically, they are the same except that UNION
performs an extra step to remove identical rows (as one might expect), and it may tend to dominate execution time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With