Distinct

The DISTINCT keyword is used to remove duplicate values from a result of a SQL or SPARQL query.

Union

Use this tag only for questions about UNION, a keyword of the SQL language for combining the results of multiple SQL queries

Slower de-dupping tables

Quality Example
Slower

"Union distinct is slower than union all but you may need it for de-dupping."

from question "How to optimize select sum() on mysql?"

"Keep in mind that union will return a distinct list - duplicates will be removed but it will perform slower than using union all which will not remove duplicates"

from question "Checking if a string is found in one of multiple columns in mySQL"

Faster

"However the tables are huge and the union all i read its faster than distinct takes forever to execute even with just two tables let alone 6"

from question "MySQL: Optimizing SELECT from 6 huge identical tables with different data split up by timestamp"

"Union all is generally faster than using distinct or grouping"

from question "SQL Server : shortest syntax using distinct?"

Note rows outer

Quality Example
Faster

"A side note since you want all rows using union all is faster since it does not need to perform a distinct to eliminate duplicates"

from question "SQL select from table and get table name"

"I am currently using union all and a distinct in the outer query as this proved much faster than using union s for the uniqueness of data"

from question "Should I use a T-SQL function, view, or stored proc?"

Back to Home
Data comes from Stack Exchange with CC-BY-SA-3.0