sql执行速度优化

1. 查找最新的主单对应的子单的sum

1. 版本一 执行十几秒没有出现结果
select sum(c.putaway_qty),a.consignment_no,c.sku_code 
from odoo_dsf_inbound_order_info a 
inner join 
(select max(id) as id from odoo_dsf_inbound_order_info group by consignment_no) b on b.id=a.id 
inner join odoo_dsf_inbound_detail_sku_info c on a.consignment_no=c.consignment_no and a.serial_number=c.serial_number 
where a.status='C' and c.putaway_qty !=0  group by a.consignment_no,c.sku_code 

2. 版本二 400万不到的数据,修改之后6秒出结果
select sum(c.putaway_qty),
a.consignment_no,c.sku_code
from odoo_dsf_inbound_order_info a
inner join
(select max(id) as id from odoo_dsf_inbound_order_info group by consignment_no) b on b.id=a.id
inner join (select * from odoo_dsf_inbound_detail_sku_info limit 4000000) c on a.consignment_no=c.consignment_no and a.serial_number=c.serial_number
where a.status='C' and c.putaway_qty !=0 group by a.consignment_no,c.sku_code

3. 版本三 因为我们是一次性任务,而且只要结果就可以,使用临时表(1秒左右)
---将最新的ID存在临时表
drop table if exists temp_m;
select max(id) as id into temp temp_m from odoo_dsf_inbound_order_info group by consignment_no;

---查询结果
select sum(c.putaway_qty),
a.consignment_no,c.sku_code -- INTO TEMP rrrr
from odoo_dsf_inbound_order_info a
inner join temp_m on temp_m.id=a.id
inner join odoo_dsf_inbound_detail_sku_info c on a.consignment_no=c.consignment_no and a.serial_number=c.serial_number
where a.status='C' and c.putaway_qty !=0 group by a.consignment_no,c.sku_code;


原文地址:https://www.cnblogs.com/JackShi/p/14151361.html