To union or union all, that is the question

前端 未结 5 1326
有刺的猬
有刺的猬 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

    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.

提交回复
热议问题