这似乎在大型数据集上花费了很长时间。将第一个和最后3个查询合并为1会更快吗?有没有人有什么可以加快速度的建议?我很感激。可以优化此SQL语句吗?
update "detail" set bal = (units * amount) where code_type = 'AB';
update "detail" set bal = (units * amount) where code_type = 'CD';
update "detail" set bal = (units * amount) where code_type = 'EF';
update "detail" set bal = (units * amount * -1) where code_type = 'GH';
update "detail" set bal = (units * amount * -1) where code_type = 'IK';
update "detail" set bal = (units * amount * -1) where code_type = 'LM';
update "detail" set bal = 0 where code_type = 'NO';
另外 -
update bill set pay =
(select round(sum(bd1.bal),2) from "detail" bd1 where
bd1.inv = bill.inv and
(bd1.code_type = 'AB' or bd1.code_type = 'CD'));
update bill set pay = 0 where pay is null;
update bill set cost =
(select round(sum(bd2.bal),2) from "detail" bd2 where
bd2.inv = bill.inv and
(not (bd2.code_type = 'AB' or bd2.code_type = 'CD')));
update bill set cost = 0 where cost is null;
update bill set balance = round(cost + pay,2);
感谢
OP的语句更新了两个不同的表“'detail”'和'bill'。但你只是更新'账单'。它必须是2个frankqueries,而不是1,是吗? – 2012-04-26 17:28:27
呃,我错过了 - 没有意识到那里真的有两个问题。我会尽力将它分成两部分。 – 2012-04-26 17:47:16
感谢您的帮助,第一部分效果很好!我很抱歉,因为在我的第二个陈述中,我错误地标记了一些字段。我不知道这会造成多大的差异。但如果你不介意再看一遍。以下是我所做的更改。 1. bd2.inv = bill.inv和2.更新账单设置成本= 0,其中成本为空; 3.更新账单设置余额= round(cost + pay,2); – Trevor 2012-04-27 18:06:07