SQL内多个子查询的优化

SQL内多个子查询的优化

原SQL:

select DISTINCT c.gx_name,b.sort,IFNULL(a.prd_id,0) as prd_id,d.order_id,
        (select min(bb.actual_start_date)
        from prd_plan_dispatch aa
        inner join prd_plan_dispatch_detail bb on bb.dispatch_id = aa.id and bb.del_flag = '0'
        where aa.del_flag = '0'
        and aa.plan_detail_id = a.plan_detail_id
        and aa.gx_id = a.gx_id) as actualStartTime,

        (
        select min(start_date)
        from prd_plan_dispatch
        where plan_detail_id = a.plan_detail_id
        and gx_id = a.gx_id
        and del_flag = '0'
        ) as planStartTime,

        (
        select max(end_date)
        from prd_plan_dispatch
        where plan_detail_id = a.plan_detail_id
        and gx_id = a.gx_id
        and del_flag = '0'
        ) as planEndTime,

        (select max(bb.actual_end_date)
        from prd_plan_dispatch aa
        inner join prd_plan_dispatch_detail bb on bb.dispatch_id = aa.id and bb.del_flag = '0'
        where aa.del_flag = '0'
        and aa.plan_detail_id = a.plan_detail_id
        and aa.gx_id = a.gx_id) as actualEndTime,

        ifnull((select GROUP_CONCAT(sf_start)
        from prd_plan_dispatch
        where del_flag = '0'
        and plan_detail_id = a.plan_detail_id
        and gx_id = a.gx_id) ,0)as statusStr,

        (
        select sum(in_storage_qty)
        from prd_stock
        where del_flag = '0'
        and plan_detail_id = a.plan_detail_id
        and gx_id = a.gx_id
        ) as finishQty

        from prd_plan_process a
        inner join prd_tech_gx b on a.gx_id = b.gx_id and a.tech_id = b.tech_id
        inner join prd_gx_base c on c.id = a.gx_id
         inner join prd_plan_detail d on d.id = a.plan_detail_id and d.del_flag = '0'


        where a.del_flag = '0'
        order by prd_id ,b.sort

优化后:

把多个子查询放到一个查询里,再连接这个查询

select DISTINCT c.gx_name,b.sort,IFNULL(a.prd_id,0) as prd_id,d.order_id, 
temp.actualStartTime,temp.planStartTime,temp.planEndTime,temp.actualEndTime,ifnull(temp.statusStr,0) as statusStr,
   		(
        select sum(in_storage_qty)
        from prd_stock
        where del_flag = '0'
        and plan_detail_id = a.plan_detail_id
        and gx_id = a.gx_id
        ) as finishQty

 from prd_plan_process a
        inner join prd_tech_gx b on a.gx_id = b.gx_id and a.tech_id = b.tech_id
        inner join prd_gx_base c on c.id = a.gx_id
        inner join prd_plan_detail d on d.id = a.plan_detail_id and d.del_flag = '0'
		left join (		
            select aa.plan_detail_id ,aa.gx_id,min(bb.actual_start_date) as actualStartTime,
            	max(bb.actual_end_date) as actualEndTime,GROUP_CONCAT(DISTINCT aa.sf_start) as statusStr ,
            	min(aa.start_date) as planStartTime,max(aa.end_date) as planEndTime
            from prd_plan_dispatch aa
            left join prd_plan_dispatch_detail bb on bb.dispatch_id = aa.id and bb.del_flag = '0'
            where aa.del_flag = '0'
            group by aa.plan_detail_id,aa.gx_id
            
        ) temp on temp.plan_detail_id = a.plan_detail_id and temp.gx_id = a.gx_id
				 
where a.del_flag = '0'
order by prd_id ,b.sort
你的身份是高级编程技术专家,精通各类编程语言,能对编程过程中的各类问题进行分析和解答。我的问题是【用Oracle语句提高下述代码的运行速度:SELECT a.eqp_id,a.mes_recp,prim_resource,Round(waitime,1) FS_QT,Round(runtime,1) FS_RT FROM (SELECT eqp_id,mes_recp,PERCENTILE_CONT(0.5) within group(order by rtime) over(partition by eqp_id,mes_recp) runtime FROM (SELECT eqp_id,mes_recp, Round(To_Number(lotend_time-lotstart_time)*24,1) rtime FROM sdb_tb_els_history_table_miu WHERE lot_type NOT IN ('Z','V','Y','L','LF','T') AND lotstart_time IS NOT NULL AND lotend_time IS NOT NULL --AND (step_id NOT LIKE 'SRC%' AND step_id NOT LIKE 'RRC%') GROUP BY eqp_id,mes_recp,lotstart_time,lotend_time ))a, (SELECT prim_resource,equipmentname eqp_id,recipe mes_recp, PERCENTILE_CONT(0.5) within group(order by quetime) over(partition by prim_resource,recipe)waitime FROM sdb_tb_wip_tito_hist_miu WHERE lottype NOT IN ('Z','V','Y','L','LF','T') AND recipe IS NOT NULL AND priority < 9 AND priority||'-'||internalpriority NOT IN ('8-96','8-99') AND quetime<170 --AND (step NOT LIKE 'SRC%' AND step NOT LIKE 'RRC%') GROUP BY prim_resource,equipmentname,recipe,quetime,runtime)b WHERE a.eqp_id=b.eqp_id AND a.mes_recp=b.mes_recp GROUP BY a.eqp_id,a.mes_recp,prim_resource,runtime,waitime UNION ALL SELECT eqp_id,mes_recp,prim_resource,FS_RT,FS_QT FROM (SELECT equipmentname eqp_id,recipe mes_recp,prim_resource,0 FS_RT,Round(Avg(quetime) over(partition by prim_resource,recipe),1) FS_QT FROM sdb_tb_wip_tito_hist_miu WHERE lottype NOT IN ('Z','V','Y','L','LF','T') AND recipe IS NOT NULL AND priority < 9 AND priority||'-'||internalpriority NOT IN ('8-96','8-99') AND recipe IN ('DUMMY','AE01') AND quetime<170) GROUP BY eqp_id,mes_recp,prim_resource,fs_rt,fs_qt】
09-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值