关于SQL优化的一个小试例子

时间:2023-03-08 21:22:28
关于SQL优化的一个小试例子

原SQL:

select ta.serialno,
       ta.accepttime,
       ta.subsnumber,
       ta.subsname,
       ta.contactphone1,
       ta.servicecontent,
       ta.address,
       r.region_name,
       s.contactchannel_name,
       p.name,
       p.fullname,
       rownum
  from (select t.serialno,
               t.accepttime,
               t.subsnumber,
               t.subsname,
               t.contactphone1,
               t.servicecontent,
               t.address,
               t.contactchannel,
               t.srtypeid,
               t.partition_id_region
          FROM report_threed_problemprocess t,
               (select trim(t1.para) p1
                  FROM System_Parameters_t t1
                 WHERE t1.para_flag = 'OVER_GONGXINBYCL_TYPE') a,
               (SELECT trim(t2.para) p2
                  FROM System_Parameters_t t2
                 WHERE t2.para_flag = 'OVER_GONGXINBYCL_CHANNEL') b,
               (SELECT TRIM(t3.para) p3
                  FROM System_Parameters_t t3
                 WHERE t3.para_flag = 'OVER_96180_TYPE') c
         WHERE t.partition_id_month = uf_get_partition_month(201306)
           and t.accepttime>=to_date(201306||'01','yyyymmdd') and t.accepttime<add_months(to_date(201306||'01','yyyymmdd'),1)
           and t.partition_id_region in
               (SELECT rt.ioid_id0 region_id
                  FROM region_t rt
                 WHERE rt.region_level = 4
                   AND rt.ioid_id0 >= 1001
                   AND rt.ioid_id0 <= 1018)
           AND t.srtypeid IN (SELECT spp.srtype_id
                                FROM sdt_para_pro_com_tsnote spp
                               WHERE spp.pro_com_id =1025
                                 AND state = 1)
        AND ((substr(t.srtypeid, 1, 6) = a.p1 and t.contactchannel = b.p2) or
        (substr(t.srtypeid, 1, 6) = c.p3 and t.contactchannel = b.p2))
        ) ta --96180
 inner join sdt_para_contactchannel s
    on ta.contactchannel = s.contactchannel
 inner join sdt_servicerequesttype p
    on ta.srtypeid = p.srtypeid
 inner join region_t r
    on r.ioid_id0=ta.partition_id_region;

在数据库上的执行时间为:65.969s

改进后的SQL:

select ta.serialno,
       ta.accepttime,
       ta.subsnumber,
       ta.subsname,
       ta.contactphone1,
       ta.servicecontent,
       ta.address,
       (SELECT r.region_name FROM region_t r WHERE  r.ioid_id0=ta.partition_id_region)region_name,
       (SELECT s.contactchannel_name FROM sdt_para_contactchannel s WHERE ta.contactchannel = s.contactchannel) contactchannel_name,
       p.name,
       p.fullname,
       rownum
  from (select t.serialno,
               t.accepttime,
               t.subsnumber,
               t.subsname,
               t.contactphone1,
               t.servicecontent,
               t.address,
               t.contactchannel,
               t.srtypeid,
               t.partition_id_region
          FROM report_threed_problemprocess t
         WHERE t.partition_id_month = uf_get_partition_month(201306)
           and t.accepttime>=to_date(201306||'01','yyyymmdd') and t.accepttime<add_months(to_date(201306||'01','yyyymmdd'),1)
           AND t.srtypeid IN (SELECT spp.srtype_id FROM sdt_para_pro_com_tsnote spp
                               WHERE spp.pro_com_id =1025 AND state = 1)
        AND
        (
          (substr(t.srtypeid, 1, 6) =(select trim(t1.para) FROM System_Parameters_t t1 WHERE t1.para_flag = 'OVER_GONGXINBYCL_TYPE')
                and t.contactchannel =(SELECT trim(t2.para) p2 FROM System_Parameters_t t2 WHERE t2.para_flag = 'OVER_GONGXINBYCL_CHANNEL')
           )
           or
          (substr(t.srtypeid, 1, 6) =(SELECT TRIM(t3.para) p3 FROM System_Parameters_t t3 WHERE t3.para_flag = 'OVER_96180_TYPE')
           and t.contactchannel =(SELECT trim(t2.para) p2 FROM System_Parameters_t t2 WHERE t2.para_flag = 'OVER_GONGXINBYCL_CHANNEL')
          )
         )
  ) ta --96180
 inner join sdt_servicerequesttype p
    on ta.srtypeid = p.srtypeid;
   执行时长:9.17s

暂时记这么多。