What is the difference between UNION and UNION ALL?

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

What is the difference between UNION and UNION ALL?

26条回答
  •  不要未来只要你来
    2020-11-21 12:09

    UNION removes duplicate records in other hand UNION ALL does not. But one need to check the bulk of data that is going to be processed and the column and data type must be same.

    since union internally uses "distinct" behavior to select the rows hence it is more costly in terms of time and performance. like

    select project_id from t_project
    union
    select project_id from t_project_contact  
    

    this gives me 2020 records

    on other hand

    select project_id from t_project
    union all
    select project_id from t_project_contact
    

    gives me more than 17402 rows

    on precedence perspective both has same precedence.

提交回复
热议问题