title: Performance of UNION vs UNION ALL
body: <p>I am selecting a single column of foreign keys from multiple tables through either UNION or UNION ALL.</p>
  
  <p>It is generally recommended to use UNION ALL instead of UNION for performance issues when duplicates do not matter. However, in my calling PHP script it would be more efficient to loop through and manipulate the data without duplicates. </p>
  
  <p>So, I can use either of the following options:</p>
  
  <p><strong>Option 1:</strong></p>
  
  <p>Use UNION in the database to eliminate duplicates</p>
  
  <p><strong>Option 2:</strong></p>
  
  <p>use UNION ALL in the database and eliminate the data in my PHP script using array_unique() or other similar functions. </p>
  
  <p>My assumption is that Option 1 would be the preferred and more efficient method in the majority of cases, however I have nothing to back up that assumption, and not sure the best way to test it especially since it would likely depend a lot on what the data was.</p>
  
  <p>Is my assumption correct in most cases? If so, why? If not, why not?</p>