SQL UNION 和 UNION ALL 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between UNION and UNION ALL?
提问by Brian G
What is the difference between UNION
and UNION ALL
?
UNION
和 和有UNION ALL
什么区别?
回答by Jim Harte
UNION
removes duplicate records (where all columns in the results are the same), UNION ALL
does not.
UNION
删除重复记录(结果中的所有列都相同),UNION ALL
不会。
There is a performance hit when using UNION
instead of UNION ALL
, since the database server must do additional work to remove the duplicate rows, but usually you do not want the duplicates (especially when developing reports).
使用UNION
代替 会降低性能UNION ALL
,因为数据库服务器必须做额外的工作来删除重复的行,但通常您不想要重复的行(尤其是在开发报告时)。
UNION Example:
联合示例:
SELECT 'foo' AS bar UNION SELECT 'foo' AS bar
Result:
结果:
+-----+
| bar |
+-----+
| foo |
+-----+
1 row in set (0.00 sec)
UNION ALL example:
联合所有示例:
SELECT 'foo' AS bar UNION ALL SELECT 'foo' AS bar
Result:
结果:
+-----+
| bar |
+-----+
| foo |
| foo |
+-----+
2 rows in set (0.00 sec)
回答by Bhaumik Patel
Both UNION and UNION ALL concatenate the result of two different SQLs. They differ in the way they handle duplicates.
UNION 和 UNION ALL 都连接了两个不同 SQL 的结果。它们在处理重复项的方式上有所不同。
UNION performs a DISTINCT on the result set, eliminating any duplicate rows.
UNION ALL does not remove duplicates, and it therefore faster than UNION.
UNION 对结果集执行 DISTINCT,消除任何重复的行。
UNION ALL 不会删除重复项,因此它比 UNION 更快。
Note:While using this commands all selected columns need to be of the same data type.
注意:使用此命令时,所有选定的列都需要具有相同的数据类型。
Example: If we have two tables, 1) Employee and 2) Customer
示例:如果我们有两个表,1) Employee 和 2) Customer
- Employee table data:
- 员工表数据:
- Customer table data:
- 客户表数据:
- UNION Example (It removes all duplicate records):
- UNION 示例(它删除所有重复记录):
- UNION ALL Example (It just concatenate records, not eliminate duplicates, so it is faster than UNION):
- UNION ALL 示例(它只是连接记录,不消除重复,所以它比 UNION 快):
回答by mathewbutler
UNION
removes duplicates, whereas UNION ALL
does not.
UNION
删除重复项,而UNION ALL
不会。
In order to remove duplicates the result set must be sorted, and this mayhave an impact on the performance of the UNION, depending on the volume of data being sorted, and the settings of various RDBMS parameters ( For Oracle PGA_AGGREGATE_TARGET
with WORKAREA_SIZE_POLICY=AUTO
or SORT_AREA_SIZE
and SOR_AREA_RETAINED_SIZE
if WORKAREA_SIZE_POLICY=MANUAL
).
为了删除重复项,必须对结果集进行排序,这可能会对 UNION 的性能产生影响,具体取决于排序的数据量以及各种 RDBMS 参数的设置(对于 Oracle PGA_AGGREGATE_TARGET
with WORKAREA_SIZE_POLICY=AUTO
or SORT_AREA_SIZE
and SOR_AREA_RETAINED_SIZE
if WORKAREA_SIZE_POLICY=MANUAL
)。
Basically, the sort is faster if it can be carried out in memory, but the same caveat about the volume of data applies.
基本上,如果可以在内存中进行排序,则排序会更快,但同样适用于有关数据量的警告。
Of course, if you need data returned without duplicates then you mustuse UNION, depending on the source of your data.
当然,如果您需要返回没有重复的数据,那么您必须使用 UNION,具体取决于您的数据来源。
I would have commented on the first post to qualify the "is much less performant" comment, but have insufficient reputation (points) to do so.
我会评论第一篇文章来限定“性能低得多”的评论,但没有足够的声誉(点数)来这样做。
回答by Michiel Overeem
In ORACLE: UNION does not support BLOB (or CLOB) column types, UNION ALL does.
在 ORACLE 中:UNION 不支持 BLOB(或 CLOB)列类型,但 UNION ALL 支持。
回答by George Mauer
The basic difference between UNION and UNION ALL is union operation eliminates the duplicated rows from the result set but union all returns all rows after joining.
UNION 和 UNION ALL 之间的基本区别是联合操作从结果集中消除了重复的行,而联合所有在加入后返回所有行。
from http://zengin.wordpress.com/2007/07/31/union-vs-union-all/
来自http://zengin.wordpress.com/2007/07/31/union-vs-union-all/
回答by Ihor Vorotnov
You can avoid duplicates and still run much faster than UNION DISTINCT (which is actually same as UNION) by running query like this:
通过运行这样的查询,您可以避免重复,并且仍然比 UNION DISTINCT(实际上与 UNION 相同)运行得更快:
SELECT * FROM mytable WHERE a=X UNION ALL SELECT * FROM mytable WHERE b=Y AND a!=X
SELECT * FROM mytable WHERE a=X UNION ALL SELECT * FROM mytable WHERE b=Y AND a!=X
Notice the AND a!=X
part. This is much faster then UNION.
注意AND a!=X
部分。这比 UNION 快得多。
回答by Peter Perhá?
Just to add my two cents to the discussion here: one could understand the UNION
operator as a pure, SET-oriented UNION - e.g. set A={2,4,6,8}, set B={1,2,3,4}, A UNION B = {1,2,3,4,6,8}
只是在这里的讨论中加上我的两分钱:人们可以将UNION
运算符理解为纯粹的面向 SET 的 UNION - 例如 set A={2,4,6,8}, set B={1,2,3,4 }, A UNION B = {1,2,3,4,6,8}
When dealing with sets, you would not want numbers 2 and 4 appearing twice, as an element either isor is notin a set.
当套打交道,你不希望号2和4出现两次,作为一个元素或者是或不是一组。
In the world of SQL, though, you might want to see all the elements from the two sets together in one "bag" {2,4,6,8,1,2,3,4}. And for this purpose T-SQL offers the operator UNION ALL
.
但是,在 SQL 世界中,您可能希望将两个集合中的所有元素放在一个“袋子”{2,4,6,8,1,2,3,4} 中。为此,T-SQL 提供了运算符UNION ALL
.
回答by DotNetGuy
UNION
The UNION
command is used to select related information from two tables, much like the JOIN
command. However, when using the UNION
command all selected columns need to be of the same data type. With UNION
, only distinct values are selected.
UNION
的UNION
命令用于从两个表中选择相关的信息,很像JOIN
命令。但是,使用该UNION
命令时,所有选定的列都需要具有相同的数据类型。使用UNION
,仅选择不同的值。
UNION ALL
The UNION ALL
command is equal to the UNION
command, except that UNION ALL
selects all values.
UNION ALL
的UNION ALL
命令等于UNION
命令,不同之处在于UNION ALL
选择的所有值。
The difference between Union
and Union all
is that Union all
will not eliminate duplicate rows, instead it just pulls all rows from all tables fitting your query specifics and combines them into a table.
Union
和之间的区别在于Union all
它Union all
不会消除重复的行,而只是从所有符合查询细节的表中提取所有行并将它们组合到一个表中。
A UNION
statement effectively does a SELECT DISTINCT
on the results set. If you know that all the records returned are unique from your union, use UNION ALL
instead, it gives faster results.
一个UNION
语句有效地做一个SELECT DISTINCT
对结果集。如果您知道返回的所有记录在您的联合中都是唯一的,请UNION ALL
改用,它会提供更快的结果。
回答by Jakub ?turc
Not sure that it matters which database
不确定哪个数据库很重要
UNION
and UNION ALL
should work on all SQL Servers.
UNION
并且UNION ALL
应该适用于所有 SQL Server。
You should avoid of unnecessary UNION
s they are huge performance leak. As a rule of thumb use UNION ALL
if you are not sure which to use.
你应该避免不必要的UNION
s 它们是巨大的性能泄漏。根据经验,UNION ALL
如果您不确定要使用哪个,请使用。
回答by DBA
UNION - results in distinctrecords
while
UNION ALL - results in all the records including duplicates.
UNION - 产生不同的记录,
而
UNION ALL - 产生包括重复在内的所有记录。
Both are blocking operators and hence I personally prefer using JOINS over Blocking Operators(UNION, INTERSECT, UNION ALL etc. ) anytime.
两者都是阻塞运算符,因此我个人更喜欢在任何时候使用 JOINS 而不是阻塞运算符(UNION、INTERSECT、UNION ALL 等)。
To illustrate why Union operation performs poorly in comparison to Union All checkout the following example.
为了说明为什么 Union 操作与 Union All 相比表现不佳,请查看以下示例。
CREATE TABLE #T1 (data VARCHAR(10))
INSERT INTO #T1
SELECT 'abc'
UNION ALL
SELECT 'bcd'
UNION ALL
SELECT 'cde'
UNION ALL
SELECT 'def'
UNION ALL
SELECT 'efg'
CREATE TABLE #T2 (data VARCHAR(10))
INSERT INTO #T2
SELECT 'abc'
UNION ALL
SELECT 'cde'
UNION ALL
SELECT 'efg'
Following are results of UNION ALL and UNION operations.
以下是 UNION ALL 和 UNION 操作的结果。
A UNION statement effectively does a SELECT DISTINCT on the results set. If you know that all the records returned are unique from your union, use UNION ALL instead, it gives faster results.
UNION 语句有效地对结果集执行 SELECT DISTINCT。如果您知道返回的所有记录在您的联合中都是唯一的,请改用 UNION ALL,它会提供更快的结果。
Using UNION results in Distinct Sortoperations in the Execution Plan. Proof to prove this statement is shown below:
使用 UNION 会导致执行计划中的不同排序操作。证明此陈述的证明如下所示: