What is the difference between UNION and UNION ALL?

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

What is the difference between UNION and UNION ALL?

26条回答
  •  孤独总比滥情好
    2020-11-21 12:12

    UNION removes duplicates, whereas UNION ALL does not.

    In order to remove duplicates the result set must be sorted, and this may have 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 ).

    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 must use UNION, depending on the source of your data.

    I would have commented on the first post to qualify the "is much less performant" comment, but have insufficient reputation (points) to do so.

提交回复
热议问题