这个SQL语句可以再优化一下吗?老是超时已过期
select a.order_id,a.order_number,a.order_date,a.style_number,a.shipment_date, (select count(order_id) from carton where order_id=a.order_id) packed_count, (select sum(packed_qty) from carton where order_id=a.order_id) packed_qty,
(select sum(gross_weight) from carton where order_id=a.order_id) gross_weight,
(select sum(net_weight) from carton where order_id=a.order_id) net_weight,
(select sum(vol_cbm) from carton where order_id=a.order_id) total_vol,
b.country_name,a.shipment_method_desc,a.terms_desc
from order_head a
inner join order_address b on a.order_id=b.order_id and b.address_purpose='DE'
where (1=1) +其他条件
里面有个统计结果,不知道可以再优化一下不,查询时老是超时已过期
[解决办法]
SELECT a.order_id ,
a.order_number ,
a.order_date ,
a.style_number ,
a.shipment_date ,
X.*,
b.country_name ,
a.shipment_method_desc ,
a.terms_desc
FROM order_head a
INNER JOIN order_address b ON a.order_id = b.order_id
AND b.address_purpose = 'DE'
OUTER APPLY
(
SELECT packed_count = COUNT(order_id), packed_qty=SUM(packed_qty), gross_weight=SUM(gross_weight),
net_weight = SUM(net_weight), total_vol=SUM(total_vol)
FROM carton WHERE order_id = a.order_id
) x
WHERE ( 1 = 1 )
--#1.把子查询改成OUTER APPLY,如果上所示
--#2.建立索引1.carton(order_id),2.order_address(order_id)
--#3.剩下的优化就要看你WHERE中的东东了,楼主没写出来,期待中……