优化联合请求

rwqw0loc  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(259)

我使用sql。我有两张table
表1:id、物品名称、物品代码、物品本地化
表2:id,thing\u id,thing\u value\u 1,thing\u value\u 2,eval\u year,eval\u month,t1\u id
表1包含一些内容。一件东西有一个名字,一个代码和一个本地化。表2包含了对事物不同评价的对应条目。每月进行的评估。我们不是每个月都评估每件事,所以我们有一些事情不是每个月都评估
我想做什么?
我想:
1) 我想在一个特定年份(y年)开始之前,对一个名为“local\u0”的特定地方的每一件事进行评估。对应的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月底所做的评估的总和(与本地的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;

我想优化这个请求,因为它太长了。有人能帮我吗?对不起,我的英语不好。。。

gorkyyrv

gorkyyrv1#

使用条件聚合:

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;

相关问题