Thursday, 5 September 2013

Union VS Union all in terms of performance

Many times  unskilled or novice computer programmers when are under pressure and the time is running fast towards the end, usually  they copy the code from a place and paste it into another place, without the full understanding of the implications.

Let's take a look at the differences between UNION and UNION ALL operators.

Both of them are able to combine multiple queries but UNION operator makes a loop of the combined rows and in addition looks for duplicated records in order to remove them, on the contrary UNION ALL simply combines the multiple queries result.

If your purpose is combine two or more queries without duplicated rows and you are super sure that there are not, never ever use UNION because performance will be negatively affected because of the loops and the discard operations, UNION ALL is definitely the best solution for achieving your purpose.


Find out the difference running the explain plan of your queries with both of the operators.

No comments:

Post a Comment