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


你可能感兴趣的:(JOIN,sql,优化,Date,BI,reference)