优化这条sql: select distinct (select product_name from t_product from where id = #{productId} and mark = 1 and status = 1) as productName, (select count(0) from t_clue a where a.distribution_status != 4 and a.mark = 1 and a.product_id = #{productId} and a.status in(1,2,3,31,32,33)) as clueCount, (select count(0) from t_clue a left join t_clue_appendix b on a.clue_code = b.clue_code where a.distribution_status != 4 and a.mark = 1 and b.file_url is not null and a.product_id = #{productId} and a.status in (3,31,32,33)) as intentionCount, (select count(0) from t_clue a where a.status in (4,5,7,8) and a.distribution_status != 4 and a.mark = 1 and a.product_id = #{productId} and a.status = 4) as incomingCount, (select count(0) from t_clue a where a.status in (5,7,8) and a.distribution_status != 4 and a.mark = 1 and a.product_id = #{productId} and a.status in (5,7,8)) as approvedCount, (select count(0) from t_clue a where a.status = 6 and a.distribution_status != 4 and a.mark = 1 and a.product_id = #{productId} and a.status = 6) as rejectionCount, (select count(0) from t_clue a where a.status in (7,8) and a.distribution_status != 4 and a.mark = 1 and a.product_id = #{productId}) as loanCount, (select count(0) from t_clue a where a.status = 8 and a.distribution_status != 4 and a.mark = 1 and a.product_id = #{productId}) as swipeCount, (select sum(a.loan_amount) from t_clue a where a.distribution_status != 4 and a.mark = 1 and a.product_id = #{productId}) as loanMoney, (select sum(a.use_amount) from t_clue a where a.distribution_status != 4 and a.mark = 1 and a.product_id = #{productId}) as swipeMoney
时间: 2024-03-05 19:54:43 浏览: 18
可以考虑使用 JOIN 语句替换子查询,同时对查询中的字段添加索引以提高查询效率。修改后的 SQL 可能如下所示:
```
SELECT
t1.product_name AS productName,
COUNT(CASE WHEN t2.status IN (1,2,3,31,32,33) THEN 1 END) AS clueCount,
COUNT(CASE WHEN t2.status IN (3,31,32,33) AND t3.file_url IS NOT NULL THEN 1 END) AS intentionCount,
COUNT(CASE WHEN t2.status = 4 THEN 1 END) AS incomingCount,
COUNT(CASE WHEN t2.status IN (5,7,8) THEN 1 END) AS approvedCount,
COUNT(CASE WHEN t2.status = 6 THEN 1 END) AS rejectionCount,
COUNT(CASE WHEN t2.status IN (7,8) THEN 1 END) AS loanCount,
COUNT(CASE WHEN t2.status = 8 THEN 1 END) AS swipeCount,
SUM(t2.loan_amount) AS loanMoney,
SUM(t2.use_amount) AS swipeMoney
FROM
t_product t1
JOIN t_clue t2 ON t1.id = t2.product_id
LEFT JOIN t_clue_appendix t3 ON t2.clue_code = t3.clue_code
WHERE
t1.id = #{productId} AND t1.mark = 1 AND t1.status = 1 AND t2.distribution_status != 4 AND t2.mark = 1
GROUP BY
t1.product_name;
```