Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
    Bookmark Topic Watch Topic
 
friki data migration
Ranch Hand
Posts: 772
1
  • Mark post as helpful
  • send pies
  • Report post to moderator
Use UNION ALL instead of UNION when you can

It's a shame that UNION ALL needs an extra keyword compared to UNION. It would be much better if the SQL standard had been defined to support:


  • UNION (allowing duplicates)
  • UNION DISTINCT (removing duplicates)


  • Not only is the removal of duplicates rarely needed (or sometimes even wrong), it is also quite slow for large result sets with many columns, as the two subselects need to be ordered, and each tuple needs to be compared with its subsequent tuple.

    Note that even if the SQL standard specifies INTERSECT ALL and EXCEPT ALL, hardly any database implements these less useful set operations.

    The Cure:

    Every time you write a UNION, think if you actually wanted to write UNION ALL.

    Source


    SqlBestPractices
     
      Bookmark Topic Watch Topic
    • New Topic