To union or union all, that is the question

前端 未结 5 1498
有刺的猬
有刺的猬 2021-02-20 16:38

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 a

5条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-20 17:18

    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.

    • SQL Server article
    • Oracle article
    • MySQL article
    • DB2 documentation

提交回复
热议问题