简体   繁体   中英

improve performance select union command mysql

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.

enter link description here

The statement:

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 >= '0001' AND tbl_c.field_1 <= '9999' AND tbl_d.field_1 = '2017' AND tbl_e.field_1 <> '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 >= '0001' AND tbl_c.field_1 <= '9999' AND tbl_d.field_1 = '2017' AND tbl_e.field_1 <> '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 >= '0001' AND tbl_c.field_1 <= '9999' AND tbl_d.field_1 = '2017' AND tbl_e.field_1 <> '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 >= '0001' AND tbl_c.field_1 <= '9999' AND tbl_d.field_1 = '2017' AND tbl_e.field_1 <> 'n.v.t.')) AS result
WHERE result.b = 108 AND result.c >= '0001' AND result.c <= '9999' AND result.d = '2017' AND result.e <> 'n.v.t.' 
ORDER BY result.a

You could try to put some indexes on the columns involved in 'where' clauses and on 'join' clauses.

https://dev.mysql.com/doc/refman/5.7/en/create-index.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM