I have two queries that I’m UNIONing 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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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 useUNION ALL. Just because your data will produce the same results right now doesn’t mean that it always will.That said,
UNION ALLwill be faster on any sane database implementation, see the articles below for examples. But typically, they are the same except thatUNIONperforms an extra step to remove identical rows (as one might expect), and it may tend to dominate execution time.