What is the difference between UNION and UNION ALL?

后端 未结 26 2346
伪装坚强ぢ
伪装坚强ぢ 2020-11-21 11:28

What is the difference between UNION and UNION ALL?

相关标签:
26条回答
  • 2020-11-21 12:14

    UNION removes duplicate records (where all columns in the results are the same), UNION ALL does not.

    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 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)
    
    0 讨论(0)
  • 2020-11-21 12:14

    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.

    from http://zengin.wordpress.com/2007/07/31/union-vs-union-all/

    0 讨论(0)
  • 2020-11-21 12:15

    You can avoid duplicates and still run much faster than UNION DISTINCT (which is actually same as UNION) by running query like this:

    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.

    0 讨论(0)
  • 2020-11-21 12:15

    UNION and UNION ALL used to combine two or more query results.

    UNION command selects distinct and related information from two tables which will eliminates duplicate rows.

    On the other hand, UNION ALL command selects all the values from both the tables, which displays all rows.

    0 讨论(0)
  • 2020-11-21 12:16

    Not sure that it matters which database

    UNION and UNION ALL should work on all SQL Servers.

    You should avoid of unnecessary UNIONs they are huge performance leak. As a rule of thumb use UNION ALL if you are not sure which to use.

    0 讨论(0)
  • 2020-11-21 12:16

    UNION merges the contents of two structurally-compatible tables into a single combined table.

    • Difference:

    The difference between UNION and UNION ALL is that UNION will omit duplicate records whereas UNION ALL will include duplicate records.

    Union Result set is sorted in ascending order whereas UNION ALL Result set is not sorted

    UNION performs a DISTINCT on its Result set so it will eliminate any duplicate rows. Whereas UNION ALL won't remove duplicates and therefore it is faster than UNION.*

    Note: The performance of UNION ALL will typically be better than UNION, since UNION requires the server to do the additional work of removing any duplicates. So, in cases where it is certain that there will not be any duplicates, or where having duplicates is not a problem, use of UNION ALL would be recommended for performance reasons.

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