title: improve performance select union command mysql
body: <p>In my application I have a SELECT UNION statement to fetch data from a MySQL database and display the data on a webpage. This is working fine but it takes almost 4 seconds before the result is displayed on the page. I have searched the web for methods to improve the performance of a SELECT UNION statement and found two possible solutions. 1 UNION ALL instead of UNION. 2 move the WHERE inside each subquery. I have tried both but this does not improve performance. It still takes about 4 seconds. Are there any other possible solutions? With kind regards, Rutger.</p>
  
  <p><a href="http://www.iheavy.com/2013/06/13/how-to-optimize-mysql-union-for-high-speed/" rel="nofollow noreferrer">enter link description here</a></p>
  
  <h2>The statement:</h2>
  
  <pre><code>SELECT a, b, c, d, e FROM (
  (SELECT 
  tbl_a.field_1 AS a,
  tbl_b.field_1 AS b,
  tbl_c.field_1 AS c,
  tbl_d.field_1 AS d,
  tbl_e.field_1 AS e
  FROM (((tbl_a INNER JOIN tbl_b ON tbl_a.bkst_id = tbl_b.bkst_id) INNER JOIN tbl_c 
  ON tbl_a.rel_id = tbl_c.rel_id) INNER JOIN tbl_d ON tbl_b.rekA_id = tbl_d.rekB_id) 
  INNER JOIN tbl_e ON tbl_a.dgbk_id = tbl_e.dgbk_id
  WHERE tbl_b.field_1 = 108 AND tbl_c.field_1 &gt;= '0001' AND tbl_c.field_1 &lt;= '9999' AND tbl_d.field_1 = '2017' AND tbl_e.field_1 &lt;&gt; 'n.v.t.') 
  UNION ALL
  (SELECT 
  tbl_a.field_1 AS a,
  tbl_b.field_2 AS b,
  tbl_c.field_1 AS c,
  tbl_d.field_1 AS d,
  tbl_e.field_1 AS e
  FROM (((tbl_a INNER JOIN tbl_b ON tbl_a.bkst_id = tbl_b.bkst_id) INNER JOIN tbl_c 
  ON tbl_a.rel_id = tbl_c.rel_id) INNER JOIN tbl_d ON tbl_b.rekC_id = tbl_d.rekB_id) 
  INNER JOIN tbl_e ON tbl_a.dgbk_id = tbl_e.dgbk_id
  WHERE tbl_b.field_1 = 108 AND tbl_c.field_1 &gt;= '0001' AND tbl_c.field_1 &lt;= '9999' AND tbl_d.field_1 = '2017' AND tbl_e.field_1 &lt;&gt; 'n.v.t.') 
  UNION ALL
  (SELECT 
  tbl_a.field_1 AS a,
  tbl_b.field_3 AS b,
  tbl_c.field_1 AS c,
  tbl_d.field_1 AS d,
  tbl_e.field_1 AS e
  FROM (((tbl_a INNER JOIN tbl_b ON tbl_a.bkst_id = tbl_b.bkst_id) INNER JOIN tbl_c 
  ON tbl_a.rel_id = tbl_c.rel_id) INNER JOIN tbl_d ON tbl_b.rekB_id = tbl_d.rekB_id)
  INNER JOIN tbl_e ON tbl_a.dgbk_id = tbl_e.dgbk_id
  WHERE tbl_b.field_1 = 108 AND tbl_c.field_1 &gt;= '0001' AND tbl_c.field_1 &lt;= '9999' AND tbl_d.field_1 = '2017' AND tbl_e.field_1 &lt;&gt; 'n.v.t.')
  UNION ALL
  (SELECT 
  tbl_a.field_1 AS a,
  tbl_b.field_4 AS b,
  tbl_c.field_1 AS c,
  tbl_d.field_1 AS d,
  tbl_e.field_1 AS e
  FROM (((tbl_a INNER JOIN tbl_b ON tbl_a.bkst_id = tbl_b.bkst_id) INNER JOIN tbl_c 
  ON tbl_a.rel_id = tbl_c.rel_id) INNER JOIN tbl_d ON tbl_b.rekD_id = tbl_d.rekB_id)
  INNER JOIN tbl_e ON tbl_a.dgbk_id = tbl_e.dgbk_id
  WHERE tbl_b.field_1 = 108 AND tbl_c.field_1 &gt;= '0001' AND tbl_c.field_1 &lt;= '9999' AND tbl_d.field_1 = '2017' AND tbl_e.field_1 &lt;&gt; 'n.v.t.')) AS result
  WHERE result.b = 108 AND result.c &gt;= '0001' AND result.c &lt;= '9999' AND result.d = '2017' AND result.e &lt;&gt; 'n.v.t.' 
  ORDER BY result.a
  </code></pre>