我在这里遇到一个主要的问题:
select
`produsereceptie`.`receptie_id` AS `document_id`,`receptie` AS `receptie`,
`produsereceptie`.`produs_id` AS `produs_id`,
`produse`.`cod` AS `cod`,
`produse`.`denumire` AS `denumire`,
`produsereceptie`.`gestiune` AS `gestiune`,
sum(`produsereceptie`.`qty`) AS `qtyIn`,
ifnull((select sum(`productie`.`qtyConsumat`) FROM `productie` where (`productie`.`produs_id` = `produsereceptie`.`produs_id`) group by `produsereceptie`.`produs_id`),0) AS `qtyOut`
from (`produsereceptie` join `produse` on((`produse`.`id` = `produsereceptie`.`produs_id`)))
group by `produsereceptie`.`produs_id`
union select `comenzimonitorizari`.`idx` AS `document_id`,
'productie' AS `productie`,`produse`.`id` AS `id`,
`produse`.`cod` AS `cod`,`produse`.`denumire` AS `denumire`,
'productie' AS `productie`,sum(`comenzimonitorizari`.`produse_conforme`) AS `qtyIn`,
ifnull((select sum(`produseiesire`.`qty`) from `produseiesire`
where (`produseiesire`.`produs_id` = `produse`.`id`)
group by `comenzimonitorizari`.`produs_cod`),0) AS `qtyOut`
from (`comenzimonitorizari` join `produse` on ((`produse`.`cod` = `comenzimonitorizari`.`produs_cod`)))
group by `comenzimonitorizari`.`produs_cod`;
sql说明是附图(http://postimg.org/image/vysy9i6ab/)
任何帮助都是最受欢迎的,因为我不知道如何优化它。
平均时间大约是9-10秒......而我在这里面临的是地狱。
答案 0 :(得分:1)
尝试UNION ALL而不是UNION。希望它有所帮助
<强>更新强>
我无法测试数据,但我会说问题在于这一行:
ifnull((select sum(`productie`.`qtyConsumat`) FROM `productie` where (`productie`.`produs_id` = `produsereceptie`.`produs_id`) group by `produsereceptie`.`produs_id`),0) AS `qtyOut`
首先尝试使用
之类的内容删除此查询替换它0 AS `qtyOut`
你现在几次?