select
wdj.wip_entity_id,
wdj.organization_id,
wdj.primary_item_id,
bso.operation_code
,sum(prl.quantity_delivered*pla.unit_price) ,sum(prl.quantity*pla.unit_price)
from
po.po_requisition_lines_all prl,
po.po_distributions_all pda,
po.po_lines_all pla,
wip.WIP_OPERATION_RESOURCES wor ,
wip.wip_discrete_jobs wdj,
wip.wip_operations wo,
bom.bom_standard_operations bso
where
prl.line_location_id=pda.line_location_id
and pda.po_line_id=pla.po_line_id
and wo.operation_seq_num=wor.operation_seq_num
and wo.wip_entity_id=wor.wip_entity_id
and wo.organization_id=wor.organization_id
and wo.department_id=bso.department_id
and wo.description=bso.operation_description
and wo.organization_id=bso.organization_id
and wor.wip_entity_id=prl.wip_entity_id
and pda.wip_operation_seq_num=wor.operation_seq_num
and wor.wip_entity_id=wdj.wip_entity_id
and wor.organization_id=wdj.organization_id
and wdj.status_type in(3,6)
and wor.autocharge_type=4
and wdj.class_code in ('TJ1補料','TJ1重工','TJ2補料','TJ2重工')
group by wdj.wip_entity_id,wdj.primary_item_id,wdj.organization_id ,bso.operation_code
現在跑出來要一分鐘左右, 高手給看一下如何優化一下。
------解决方案-------------------- 笛卡尔积是很“扯淡”的。
尽管sql server数据库会自动对笛卡尔积做优化,但是有点基础的程序员,应该写 inner join, left join 等关联语句,而不是这种。