You might be using Union or Union All in your SQL code while doing Data Analysis or building Data Pipelines. Ever wondered what is the difference between them and how using one over another can be more efficient? Yes, there is a small yet significant difference between Union and Union All.
Let's look at that by understanding each of them individually.
Let's look at that by understanding each of them individually.
1. Union All
Union All basically allows you to concatenate the table that has a similar structure of tables. The important condition to have Union All of the tables is that both the tables should have the same number of columns. So when you take Union All of two tables what it does in the background is it directly joins the tables without removing duplicates or redundant records.
2. Union
Union is also similar to Union All except one difference that it removes the duplicates records before taking the Union of the tables.
There is one disadvantage of Union over Union All, that since it removes duplicated records before doing Union of the tables the performance is low as compared to the Union All as the database server do additional work of removing duplicate records.
But you need to make a trade-off between faster performance or non-redundant/duplicate data (especially when you generate reports). So depending on the use-case, we can choose Union or Union All respectively.
So Data Engineers and Data Analysts should take a decision of using Union or Union All depending on their use-cases.
Comments
Post a Comment