Difference between Union and Union all

Source: Internet
Author: User

In the database,UnionAndUnion allThe keywords combine the two results into one, but the two are different in terms of usage and efficiency.

UnionDuplicate records are filtered out after the table link is established. Therefore, after the table link is established, the generated result set is sorted, and duplicate records are deleted before the results are returned.

In most applications, duplicate records are not generated. The most common is Process Tables and historical tables.Union. For example:

Select * From gc_dfys

Union

Select * From ls_jg_dfys

ThisSQLWhen running, the results of two tables are first taken out, and then sorted by space to delete duplicate records. Finally, the result set is returned. If the table has a large amount of data, it may be sorted by disk.

WhileUnion allSimply merge the two results and then return them. In this way, if duplicate data exists in the two returned result sets, the returned result sets will contain duplicate data.

In terms of efficiency,Union allAspect RatioUnionTherefore, if you can confirm that the merged two result sets do not contain duplicate data, you can useUnion all, As follows:

Select * From gc_dfys

Union all

Select * From ls_jg_dfys

Author's blog:Http://blog.csdn.net/sunlen/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.