== Why is this an issue?

``++UNION++`` is significantly less performant compared to ``++UNION ALL++`` because it removes duplicated entries and run an internal ``++DISTINCT++`` to achieve this.


``++UNION ALL++`` is not removing duplicates and returns all the results from the queries. It performs faster in most of the cases compared to ``++UNION++``. Nevertheless, the quantity of data returned by ``++UNION ALL++`` can be significantly more important than with ``++UNION++``. On slow network, the performance gain to use ``++UNION ALL++`` instead of ``++UNION++`` can be challenged by the time lost to transfer more data than with ``++UNION++``. 


=== Noncompliant code example

[source,sql]
----
-- if you care about not having duplicated entries, then UNION is the good choice
SELECT EMAIL FROM EMPLOYEES UNION SELECT EMAIL FROM CUSTOMERS
----


=== Compliant solution

[source,sql]
----
-- if you don't care about duplicated entries in the results of this UNION, then UNION ALL should be preferred
SELECT EMAIL FROM EMPLOYEES UNION ALL SELECT EMAIL FROM CUSTOMERS
----


ifdef::env-github,rspecator-view[]
'''
== Comments And Links
(visible only on this page)

=== on 23 Oct 2018, 10:36:07 Alexandre Gigleux wrote:
Merged with RSPEC-1571 which was better described to cover the case of ``++UNION ALL++``.

endif::env-github,rspecator-view[]
