提高性能选择union命令mysql

时间:2017-07-30 11:05:07

标签: php mysql

在我的应用程序中,我有一个SELECT UNION语句来从MySQL数据库中获取数据并在网页上显示数据。这工作正常,但在页面上显示结果之前需要将近4秒钟。我在网上搜索了提高SELECT UNION语句性能的方法,并找到了两种可能的解决方案。 1 UNION ALL而不是UNION。 2在每个子查询中移动WHERE。我尝试了两种方法,但这并没有提高性能。它仍然需要大约4秒钟。还有其他可能的解决方案吗?亲切的问候,Rutger。

enter link description here

声明:

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

1 个答案:

答案 0 :(得分:0)

您可以尝试在&#39;其中&#39;所涉及的列上放置一些索引。条款和加入&#39;条款。

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