优化联盟要求

时间:2018-08-27 09:00:56

标签: mysql join optimization union

我使用SQL。 我有两个桌子

表1: ID,thing_name,thing_code,thing_localisation

表2: ID,thing_id,thing_value_1,thing_value_2,eval_year,eval_month,T1_ID

表1包含一些内容。事物具有名称,代码和本地化。表2包含对应于事物的不同评估的条目。每月进行一次评估。 我们不会每个月评估所有事情,因此有些事情每个月都不会评估

我想做什么?

我要:

1) 我想在特定年份(y年)开始之前,对与特定本地“ LOCAL_0”相关的所有事情进行评估。 SQL查询对应的外观如下:

SELECT T.thing_code, SUM(E.thing_eval_1), SUM(E.thing_eval_2)
FROM table1 T, table2 E
WHERE T.ID = E.T1_ID AND T.thing_localisation = "LOCAL_0" AND E.eval_year < y

2)我想对从y年初到y个月m结束的事情(与LOCAL_0相关)进行评估。 SQL查询如下所示:

SELECT T.thing_code, SUM(E.thing_eval_1), SUM(E.thing_eval_2)
FROM table1 T, table2 E
WHERE T.ID = E.T1_ID AND T.thing_localisation = "LOCAL_0"
AND E.eval_year = y and E.eval_month <= m

第一个查询的结果如下

+------------------------------------------------------+
| thing_code    SUM_EVAL_1_BEFORE    SUM_EVAL 2_BEFORE |
+------------------------------------------------------+
| 111           1                    2                 |
| 112           3                    4                 |
| 113           5                    6                 |
+------------------------------------------------------+

第二个查询的结果看起来像

+-----------------------------------------------+
| thing_code    SUM_EVAL_1_NOW   SUM_EVAL_2_NOW |
+-----------------------------------------------+
| 110           0.5              0.3            |
| 111           0.1              0.1            |
| 112           1                0.9            |
+-----------------------------------------------+

3)我最终希望建立一个可以给我这个结果的请求

+--------------------------------------------------------------------------+
| thing_code EVAL_1_BEF EVAL_2_BEF EVAL_1_NOW EVAL_2_NOW EVAL_1_NOW E2_NOW |
+--------------------------------------------------------------------------+
| 110        0          0          0.5        0.3        0.5        0.3    |
| 111        1          2          0.1        0.1        1.1        2.1    |
| 112        3          4          1          0.9        4          4.9    |
| 113        5          6          0          0          5          6      |
+--------------------------------------------------------------------------+

为此,我进行了搜索,发现一个请求可以正常工作,但是我花了太长时间去给出结果(因为数据库真的很大,有很多条目,数百万行)

SELECT UN.code,
       UN.Eval1_BEF,
       UN.Eval2_BEF,
       UN.Eval1_NOW,
       UN.Eval2_NOW,
       (UN.Eval1_BEF + UN.Eval1_NOW) AS Eval1_AFTER,
       (UN.Eval2_BEF + UN.Eval2_NOW) AS Eval2_AFTER

FROM
(
SELECT R1.thing_code AS code, R1.Eval1 AS Eval1_BEF, R1.Eval2 AS Eval2_BEF, R2.Eval1 AS Eval1_NOW, R2.Eval2 AS Eval2_NOW
FROM Result_Request_1 R1 LEFT JOIN Result_Request_2 R2 ON R1.thing_code = R2.thing code

UNION

SELECT R2.thing_code AS code, R1.Eval1 AS Eval1_BEF, R1.Eval2 AS Eval2_BEF, R2.Eval1 AS Eval1_NOW, R2.Eval2 AS Eval2_NOW
FROM Result_Request_1 R1 RIGHT JOIN Result_Request_2 R2 ON R1.thing_code = R2.thing code

) UN

ORDER BY UN.code;

我想优化此请求,因为它花费的时间太长。谁能帮我?对不起,我的英语不好...

1 个答案:

答案 0 :(得分:0)

使用条件聚合:

SELECT
    T.thing_code,
    SUM(CASE WHEN E.eval_year < y THEN E.thing_eval_1 ELSE 0 END) AS SUM_EVAL_1_BEFORE,
    SUM(CASE WHEN E.eval_year < y THEN E.thing_eval_2 ELSE 0 END) AS SUM_EVAL_2_BEFORE,
    SUM(CASE WHEN E.eval_year = y and E.eval_month <= m
             THEN E.thing_eval_1 ELSE 0 END) AS SUM_EVAL_1_NOW,
    SUM(CASE WHEN E.eval_year = y and E.eval_month <= m
             THEN E.thing_eval_2 ELSE 0 END) AS SUM_EVAL_2_NOW
FROM table1 T
INNER JOIN table2 E
    ON T.ID = E.T1_ID
WHERE
    T.thing_localisation = 'LOCAL_0'
GROUP BY
    T.thing_code;