To union or union all, that is the question

前端 未结 5 1318
有刺的猬
有刺的猬 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
    0 讨论(0)
  • 2021-02-20 17:18

    I would use UNION ALL anyway. Even though you know that there are not going to be duplicates, depending on your database server engine, it might not know that.

    So, just to provide extra information to DB server, in order for its query planner a better choice (probably), use UNION ALL.

    Having said that, if your DB server's query planner is smart enough to infer that information from the UNION clause and table indexes, then results (performance and semantic wise) should be the same.

    Either case, it strongly depends on the DB server you are using.

    0 讨论(0)
  • 2021-02-20 17:25

    Since there will be no duplicates from the two use UNION ALL. You don't need to check for duplicates and UNION ALL will preform the task more efficiently.

    0 讨论(0)
  • 2021-02-20 17:32

    According to http://blog.sqlauthority.com/2007/03/10/sql-server-union-vs-union-all-which-is-better-for-performance/ at least for performance it is better to use UNION ALL, since it does not actively distinct duplicates and as such is faster

    0 讨论(0)
  • 2021-02-20 17:36

    I see that you've tagged this question PERFORMANCE, so I assume that's your primary consideration.

    UNION ALL will absolutely outperform UNION since SQL doesn't have to check the two sets for dups.

    Unless you need SQL to perform the duplicate checking for you, always use UNION ALL.

    0 讨论(0)
提交回复
热议问题