select t3.sku from (SELECT sku, SUM(daily_sell_price), SUM(profit) FROM t_sku_analyze2023 where 1=1 -- and statistics_time >= '2023-04-01 00:00:00' -- and statistics_time <= '2023-04-02 01:01:03' AND statistics_time >= '2023-04-01 00:00:00' AND statistics_time <= '2023-04-30 00:00:00' GROUP BY sku) t3 LEFT JOIN (select sku,SUM(refund_monry) AS refund_monry from t_sku_refund_analysis2023 where refund_month = '2023-04' GROUP BY sku) t4 ON t3.sku = t4.sku优化
时间: 2024-03-17 14:41:19 浏览: 118
这个查询可以进行优化,可以尝试以下两个方案:
1. 索引优化:可以在 t_sku_analyze2023 表上建立 (statistics_time, sku) 的联合索引,以及在 t_sku_refund_analysis2023 表上建立 (refund_month, sku) 的联合索引,这样可以加速查询速度。
2. 子查询优化:可以将子查询的结果缓存到临时表中,然后再进行 join 操作,这样可以避免重复计算。例如:
```
CREATE TEMPORARY TABLE t_sku_analyze_temp AS (
SELECT sku, SUM(daily_sell_price) AS sell_price, SUM(profit) AS profit
FROM t_sku_analyze2023
WHERE statistics_time >= '2023-04-01 00:00:00'
AND statistics_time <= '2023-04-30 00:00:00'
GROUP BY sku
);
CREATE TEMPORARY TABLE t_sku_refund_temp AS (
SELECT sku, SUM(refund_monry) AS refund_monry
FROM t_sku_refund_analysis2023
WHERE refund_month = '2023-04'
GROUP BY sku
);
SELECT t3.sku
FROM t_sku_analyze_temp t3
LEFT JOIN t_sku_refund_temp t4 ON t3.sku = t4.sku;
```
这样可以避免多次扫描原表,提高查询效率。
阅读全文