mysql 子查询计算太慢什么优化?
SELECTe.*,(SELECTNAMEFROMdictWHEREid=e.areaId)asareaName,(SELECTSUM(money)FROMenginee...
SELECT
e.*,
(SELECT NAME FROM dict WHERE id = e.areaId) as areaName,
(SELECT SUM(money) FROM engineering_project WHERE engineeringId =e.id) AS sumProjectMoney,
(IFNULL((SELECT SUM(actualMoney) FROM income WHERE engineeringId=e.id),0) +IFNULL((SELECT SUM(discount) FROM income WHERE engineeringId=e.id),0) +IFNULL((SELECT SUM(procedureMoney) FROM income WHERE engineeringId=e.id),0) ) AS accountPaid, ##已付款金额
IFNULL((SELECT SUM(procedureMoney) FROM income WHERE engineeringId=e.id),0) AS procedureMoneyS, ##手续费
IFNULL((SELECT SUM(discount) FROM income WHERE engineeringId=e.id),0) AS discountS, ##折扣
IFNULL((SELECT SUM(actualMoney) FROM income WHERE engineeringId=e.id),0) AS actualMoneyS, ##实收
(SELECT SUM(money) FROM engineering_project WHERE engineeringId =e.id)-(IFNULL((SELECT SUM(actualMoney) FROM income WHERE engineeringId=e.id),0) +IFNULL((SELECT SUM(discount) FROM income WHERE engineeringId=e.id),0) +IFNULL((SELECT SUM(procedureMoney) FROM income WHERE engineeringId=e.id),0) ) AS debtMoney, ##欠付款金额
IFNULL((SELECT SUM(actualMoney) FROM income WHERE engineeringId=e.id),0) AS paymentAll ##所有付款
FROM
engineering e 展开
e.*,
(SELECT NAME FROM dict WHERE id = e.areaId) as areaName,
(SELECT SUM(money) FROM engineering_project WHERE engineeringId =e.id) AS sumProjectMoney,
(IFNULL((SELECT SUM(actualMoney) FROM income WHERE engineeringId=e.id),0) +IFNULL((SELECT SUM(discount) FROM income WHERE engineeringId=e.id),0) +IFNULL((SELECT SUM(procedureMoney) FROM income WHERE engineeringId=e.id),0) ) AS accountPaid, ##已付款金额
IFNULL((SELECT SUM(procedureMoney) FROM income WHERE engineeringId=e.id),0) AS procedureMoneyS, ##手续费
IFNULL((SELECT SUM(discount) FROM income WHERE engineeringId=e.id),0) AS discountS, ##折扣
IFNULL((SELECT SUM(actualMoney) FROM income WHERE engineeringId=e.id),0) AS actualMoneyS, ##实收
(SELECT SUM(money) FROM engineering_project WHERE engineeringId =e.id)-(IFNULL((SELECT SUM(actualMoney) FROM income WHERE engineeringId=e.id),0) +IFNULL((SELECT SUM(discount) FROM income WHERE engineeringId=e.id),0) +IFNULL((SELECT SUM(procedureMoney) FROM income WHERE engineeringId=e.id),0) ) AS debtMoney, ##欠付款金额
IFNULL((SELECT SUM(actualMoney) FROM income WHERE engineeringId=e.id),0) AS paymentAll ##所有付款
FROM
engineering e 展开
3个回答
展开全部
建议建立临时表!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2020-11-13 · MySQL开源数据库领先者
关注
展开全部
子查询优化策略
对于不同类型的子查询,优化器会选择不同的策略。
1. 对于 IN、=ANY 子查询,优化器有如下策略选择:
semijoin
Materialization
exists
Materialization
exists
derived_merge,将派生表合并到外部查询中(5.7 引入 );
将派生表物化为内部临时表,再用于外部查询。
2. 对于 NOT IN、<>ALL 子查询,优化器有如下策略选择:
3. 对于 derived 派生表,优化器有如下策略选择:
注意:update 和 delete 语句中子查询不能使用 semijoin、materialization 优化策略
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询