title: PostgreSQL union two tables and join with a third table
body: <p>I want to union to tables and join them with a third metadata table and I would like to know which approach is the best/fastest?<br />
  The database is a PostgreSQL.<br />
  Below is my two suggestions, but other approaches are welcome.</p>
  
  <p>To do the join before the union on both tables:</p>
  
  <pre><code>SELECT a.id, a.feature_type, b.datetime, b.file_path
  FROM table1 a, metadata b WHERE a.metadata_id = b.id
  UNION ALL
  SELECT a.id, a.feature_type, b.datetime, b.file_path
  FROM table2 a, metadata b WHERE a.metadata_id = b.id
  </code></pre>
  
  <p>Or to do the union first and then do the join:</p>
  
  <pre><code>SELECT a.id, a.feature_type, b.datetime, b.file_path
  FROM
  (
      SELECT id, feature_type, metadata_id FROM table1
      UNION ALL
      SELECT id, feature_type, metadata_id FROM table2
  )a, metadata b
  WHERE a.metadata_id = b.id
  </code></pre>