记一次SQL写法调优 with as order by多列

老业务员开发离职,留下一个SQL ,运行70多秒。
表为7-8亿行。

主键是3列的复合主键:jrn_no ,jrn_seq ,ac_dt,
组合索引: sep_cd,ac_dt

SELECT *
  from CSDACM.T_ACM_CDDT
 where sep_cd = '8080030000119045001'
   and ac_dt <= '20201223'
   and ac_dt >= '20201213'
   and ord_seq = (select max(ord_seq)
                    from CSDACM.T_ACM_CDDT
                   where sep_cd = '8080030000119045001'
                     and ac_dt <= '20201223'
                     and ac_dt >= '20201213')
   and jrn_no = (select max(jrn_no)
                   from CSDACM.T_ACM_CDDT
                  where sep_cd = '8080030000119045001'
                    and ord_seq != '0'
                    and ac_dt <= '20201223'
                    and ac_dt >= '20201213')
   and jrn_seq = (select max(jrn_seq)
                    from CSDACM.T_ACM_CDDT
                   where sep_cd = '8080030000119045001'
                     and ac_dt <= '20201223'
                     and ac_dt >= '20201213'
                     and ord_seq = (select max(ord_seq)
                                      from CSDACM.T_ACM_CDDT
                                     where sep_cd = '8080030000119045001'
                                       and ac_dt <= '20201223'
                                       and ac_dt >= '20201213')
                     and jrn_no = (select max(jrn_no)
                                     from CSDACM.T_ACM_CDDT
                                    where sep_cd = '8080030000119045001'
                                      and ord_seq != '0'
                                      and ac_dt <= '20201223'
                                      and ac_dt >= '20201213'));

分析发现就是一个查询结果集来回循环,想到 with as。但开始时候没想,直接把结果集放到查询子集里试试。

select c.* from (SELECT a.* from CSDACM.T_ACM_CDDT a,
  (select max(ord_seq) os,max(jrn_no) jn 
  from CSDACM.T_ACM_CDDT where sep_cd = '8080030000119045001' and ac_dt <= '20201223' and ac_dt >= '20201213') b 
 where a.sep_cd = '8080030000119045001'
   and a.ac_dt <= '20201223'
   and a.ac_dt >= '20201213'
   and a.ord_seq=b.os
   and a.jrn_no=b.jn
   order by a.jrn_seq desc) c where rownum=1 

查询减少到30-40秒,看着还是有点乱,但是长度降低下来了。

继续用with as 试试:

with a as (select * from CSDACM.T_ACM_CDDT  where sep_cd = '8080030000119045001' and ac_dt <= '20201223' and ac_dt >= '20201213')
select c.* from (select * from a where (ord_seq,jrn_no) in (select max(ord_seq),max(jrn_no) from a)  order by jrn_seq desc) c where rownum=1;

结果9-12秒内出来结果。

记录下: order by多列取最大值所在的整行数据;

select * from A where (b,c) in (select max(a),max(c) from A)

with max_bc as (select * from A where (b,c) in (select max(a),max(c) from A))
select * from   max_bc where e in (select max(e) from  max_bc)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东方-phantom

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值