sql 子查询优化

在select 列表中使用子查询效率低于join连接。

 

SQL1.

用时 3s

 

--结算支付查询
select b.typename,
       b.typecode,
       a.id,
       a.biz_date,
       a.abstract,
       a.code,
       a.rev_orgname,
       a.rev_accountcode,
       a.pay_orgname,
       a.pay_accountcode,
       a.applyamount,
       a.bal_type,
       a.STEP_ID,
       (select c.lablename
          from sys_reference_value c
         where c.status = '1'
           and c.flag = '1'
           and c.referenceid = '00025' --支付方式码表ID
           and c.code = a.bal_type) s_bal_type,
       a.status,
       (case
         when a.status = 0 then
          '作废'
         when a.status = 1 then
          '已保存未提交'
         when a.status = 2 then
          '已提交审批中'
         when a.status = 3 then
          '审批拒绝'
         when a.status = 4 then
          '审批完成'
         when a.status = 5 then
          '已保存未提交'
         else
          '未知'
       end) s_status,
       a.isbatch,
       (
        -- 多条指令时取最后一条发送时间
        select max(bi.sendtime) sendtime from biz_balance_setdetail sd
               join mt_transferaccount ta on ta.ID_APPLY = sd.id
               join bp_bankinstrinfo bi on bi.transactionno = ta.code
        where sd.id = a.id
        group by sd.id
      ) as DateOfPayment
  from biz_balance_setdetail a
      join mt_operationtype b on a.id_mt_operationclass = b.id and a.datasource = 'MY'
  where b.status = '1'
   and b.flag = '1'
   AND a.status != '-1'
   -- ${macro1}
order by a.biz_date


SQL2.

用时0.125s

 

--结算支付查询
select b.typename,
       b.typecode,
       a.id,
       a.biz_date,
       a.abstract,
       a.code,
       a.rev_orgname,
       a.rev_accountcode,
       a.pay_orgname,
       a.pay_accountcode,
       a.applyamount,
       a.bal_type,
       a.STEP_ID,
       (select c.lablename
          from sys_reference_value c
         where c.status = '1'
           and c.flag = '1'
           and c.referenceid = '00025' --支付方式码表ID
           and c.code = a.bal_type) s_bal_type,
       a.status,
       (case
         when a.status = 0 then
          '作废'
         when a.status = 1 then
          '已保存未提交'
         when a.status = 2 then
          '已提交审批中'
         when a.status = 3 then
          '审批拒绝'
         when a.status = 4 then
          '审批完成'
         when a.status = 5 then
          '已保存未提交'
         else
          '未知'
       end) s_status,
       a.isbatch,
       tmp.sendtime as DateOfPayment
  from biz_balance_setdetail a
      join mt_operationtype b on a.id_mt_operationclass = b.id and a.datasource = 'MY'
      join (
        -- 多条指令时取最后一条发送时间
        select sd.id, max(bi.sendtime) sendtime from biz_balance_setdetail sd
               join mt_transferaccount ta on ta.ID_APPLY = sd.id
               join bp_bankinstrinfo bi on bi.transactionno = ta.code
        group by sd.id
      ) tmp on tmp.id = a.id
  where b.status = '1'
   and b.flag = '1'
   AND a.status != '-1'
   -- ${macro1}
order by a.biz_date


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值