问题描述
我有两个查询,我将它们 UNION
结合在一起,这样我就知道这两个查询之间不会有重复的元素.因此,UNION
和 UNION ALL
将产生相同的结果.
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.
我应该使用哪个?
推荐答案
您应该使用与您正在寻找的意图相匹配的方案.如果你想确保没有重复使用UNION
,否则使用UNION ALL
.仅仅因为您的数据会现在产生相同的结果并不意味着它总是会.
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.
也就是说,UNION ALL
在任何健全的数据库实现上都会更快,请参阅下面的文章以获取示例.但通常情况下,它们是相同的,除了 UNION
执行一个额外的步骤来删除相同的行(正如人们所期望的那样),并且它可能倾向于支配执行时间.
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.
这篇关于联合还是联合所有人,这是个问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!