暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

SQL优化案例7-利用分析函数优化自连接

Oracle微学堂 2017-11-08
1101
优化前:

select distinct decode(length(a.category_id), 
                       5, 
                       decode(a.origin_type, 801, 888888, 999999), 
                       a.category_id) category_id, 
                a.notice_code, 
                a.treat_status, 
                lr.real_name as receiver_name, 
                f.send_code, 
                f.policy_code, 
                g.real_name agent_name, 
                f.organ_id, 
                f.dept_id, 
                a.policy_id, 
                a.change_id, 
                a.case_id, 
                a.group_policy_id, 
                a.fee_id, 
                a.auth_id, 
                a.pay_id, 
                cancel_appoint.appoint_time cancel_appoint_time, 
                a.insert_time, 
                a.send_time, 
                a.end_time, 
                f.agency_code, 
                a.REPLY_TIME, 
                a.REPLY_EMP_ID, 
                a.FIRST_DUTY, 
                a.NEED_SEND_PRINT, 
                11 source 
  from t_policy_problem        a, 
       t_policy                f, 
       t_agent                 g, 
       t_letter_receiver       lr, 
       t_problem_category      pc, 
       t_policy_cancel_appoint cancel_appoint 
 where f.agent_id = g.agent_id(+) 
   and a.policy_id = f.policy_id(+) 
   and lr.main_receiver = 'Y' 
   and a.category_id = pc.category_id 
   and a.item_id = lr.item_id 
   and a.policy_id = cancel_appoint.policy_id(+) 
   And a.Item_Id = (Select Max(item_id) 
                      From t_Policy_Problem 
                     Where notice_code = a.notice_code

   and a.policy_id is not null 
   and a.notice_code is not null 
   and a.change_id is null 
   and a.case_id is null 
   and a.group_policy_id is null 
   and a.origin_type not in (801, 802) 
   and a.pay_id is null 
   and a.category_id not in 
       (130103, 130104, 130102, 140102, 140101) 
   and f.policy_type = 1 
   and (a.fee_id is null or 
       (a.fee_id is not null and a.origin_type = 701)) 
   and f.organ_id in 
       (select distinct organ_id 
          from T_COMPANY_ORGAN 
         start with organ_id = '107' 
        connect by parent_id = prior organ_id) 
   and pc.NEED_PRITN = 'Y'

优化后:

WITH t_Policy_Problem_w AS
 (SELECT tp.*, max(item_id) OVER(PARTITION BY notice_code) max_item_id
    FROM t_Policy_Problem tp)
select distinct decode(length(a.category_id),
                       5,
                       decode(a.origin_type, 801, 888888, 999999),
                       a.category_id) category_id,
                a.notice_code,
                a.treat_status,
                lr.real_name as receiver_name,
                f.send_code,
                f.policy_code,
                g.real_name agent_name,
                f.organ_id,
                f.dept_id,
                a.policy_id,
                a.change_id,
                a.case_id,
                a.group_policy_id,
                a.fee_id,
                a.auth_id,
                a.pay_id,
                cancel_appoint.appoint_time cancel_appoint_time,
                a.insert_time,
                a.send_time,
                a.end_time,
                f.agency_code,
                a.REPLY_TIME,
                a.REPLY_EMP_ID,
                a.FIRST_DUTY,
                a.NEED_SEND_PRINT,
                11 source
  from 
t_Policy_Problem_w      a,
       t_policy                f,
       t_agent                 g,
       t_letter_receiver       lr,
       t_problem_category      pc,
       t_policy_cancel_appoint cancel_appoint
 where a.item_id = a.max_item_id
   and f.agent_id = g.agent_id(+)
   and a.policy_id = f.policy_id(+)
   and lr.main_receiver = 'Y'
   and a.category_id = pc.category_id
   and a.item_id = lr.item_id
   and a.policy_id = cancel_appoint.policy_id(+)
   and a.policy_id is not null
   and a.notice_code is not null
   and a.change_id is null
   and a.case_id is null
   and a.group_policy_id is null
   and a.origin_type not in (801, 802)
   and a.pay_id is null
   and a.category_id not in (130103, 130104, 130102, 140102, 140101)
   and f.policy_type = 1
   and (a.fee_id is null or (a.fee_id is not null and a.origin_type = 701))
   and f.organ_id in (select distinct organ_id
                        from T_COMPANY_ORGAN
                       start with organ_id = '107'
                      connect by parent_id = prior organ_id)
   and pc.NEED_PRITN = 'Y'
 
利用分析函数max() over(partition by )优化自连接。

扫描二维码关注我的微学堂

搜索刘老师微信号:Rman-2014,备注“Oracle学习与咨询”,即可添加好友;或者扫描下面二维码,关注我的“微学堂”公众号,了解最新OCP认证动态、题库及答案解析、培训机构及讲师介绍、课堂授课内容等。每天还有一篇技术文章发布哦!





最后修改时间:2019-12-20 16:40:16
文章转载自Oracle微学堂,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论