原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

暂时记这么多。

关于SQL优化的一个小试例子的更多相关文章

  1. 简单聊聊TiDB中sql优化的一个规则---左连接消除(Left Out Join Elimination)

    我们看看 TiDB 一段代码的实现 --- 左外连接(Left Out Join)的消除; select 的优化一般是这样的过程: 在逻辑执行计划的优化阶段, 会有很多关系代数的规则, 需要将逻辑执行 ...

  2. 关于sql优化的一个小总结

    1.数据量大的时候,可以分多次查询2.有些数据的存储可以分主次表,此表存一些不常用的数据3.union all 比union效率要高4.尽量不要用distinct5.不返回不需要的行和列6.根据条件加 ...

  3. SQL优化 MySQL版 - 避免索引失效原则(二)

    避免索引失效原则(二) 注:继上一篇文章继续讲解: 避免索引失效原则(一)https://www.cnblogs.com/StanleyBlogs/p/10482048.html#4195062 作者 ...

  4. 聊聊sql优化的15个小技巧

    前言 sql优化是一个大家都比较关注的热门话题,无论你在面试,还是工作中,都很有可能会遇到. 如果某天你负责的某个线上接口,出现了性能问题,需要做优化.那么你首先想到的很有可能是优化sql语句,因为它 ...

  5. 关于SQL优化的辟谣

    列举一些关于 SQL 语句的谣言,避免一些生瓜蛋子面试的时候被另外一群生瓜蛋子的 SQL 优化宝典给坑了. 以下所有内容都是 SQL Server 中的,其他数据库只能参考和借鉴 一.全表扫描 全表扫 ...

  6. 基于MySQL 的 SQL 优化总结

    文章首发于我的个人博客,欢迎访问.https://blog.itzhouq.cn/mysql1 基于MySQL 的 SQL 优化总结 在数据库运维过程中,优化 SQL 是 DBA 团队的日常任务.例行 ...

  7. BATJ解决千万级别数据之MySQL 的 SQL 优化大总结

    引用 在数据库运维过程中,优化 SQL 是 DBA 团队的日常任务.例行 SQL 优化,不仅可以提高程序性能,还能减低线上故障的概率. 目前常用的 SQL 优化方式包括但不限于:业务层优化.SQL 逻 ...

  8. SQL优化基础 使用索引(一个小例子)

    按照本文操作和体会,会对sql优化有个基本最简单的了解,其他深入还需要更多资料和实践的学习: 1. 建表: 复制代码代码如下: create table site_user ( id int IDEN ...

  9. sql优化经典例子

    场景 我用的数据库是mysql5.6,下面简单的介绍下场景 课程表 create table Course( c_id int PRIMARY KEY, name varchar(10) ) 数据10 ...

随机推荐

  1. max_user_connections 与 max_connections,max_connect_errors, nr_open, file-max

    LINUX文件设置: ulimit -n <num>  ----> [/etc/profile,/.bashrc] ---->/etc/security/limits.conf ...

  2. C#_LINQ(LINQ to Entities)

    LINQ to Entities 是 LINQ 中最吸引人的部分.它让你可以使用标准的 C# 对象与数据库的结构和数据打交道.使用 LINQ to Entities 时,LINQ 查询在后台转换为 S ...

  3. 【面试题】如何让C语言自动发现泄漏的内存

    1. 题目 改造malloc和free函数,使C语言能自动发现泄漏的内存,在程序退出时打印中遗漏的内存地址和大小. 2. 思路 用一个链表来记录已经分配的内存地址.在malloc时,把分配的内存地址和 ...

  4. Mantis 安装与配置

    1. 适用范围 a. 本文介绍基于 Windows 下的缺陷管理平台 Mantis. 2. 软件准备 a. 下载 EasyPHP:http://www.easyphp.org/easyphp-devs ...

  5. eclipse创建项目时出现appcompat_v7包及解决办法

    Android开发学习总结(三)--appcompat_v7项目说明 一.appcompat_v7项目说明 今天来说一下appcompat_v7项目的问题,使用eclipse创建Android项目时, ...

  6. Elasticsearch template(待续...)

    动态模板 Dynamic templates allow you to define custom mappings that can be applied to dynamically added ...

  7. 基于 JQUERY 网页 banner

    demo.html <html> <head> <title>demo</title> <link href="css/PaPaBann ...

  8. Unity3D 之连按移动加速

    上代码: 效果是连续按W后,加速移动 为物体添加个拖拽效果,方便看运动轨迹. 将下面的脚本绑定到移动的物体上. 不过这里有一点很重要的需要去注意就是该方法不能放在 void FixedUpdate() ...

  9. ASP.NET MVC中的统一化自定义异常处理

    当ASP.NET MVC程序出现了异常,怎么处理更加规范? 1. 最简单的方法是设置<customErrors/>节点 <customErrors>节点用于定义一些自定义错误信 ...

  10. ASP.Net Core 运行在Linux(Ubuntu)

    这段时间一直在研究asp.net core部署到linux,今天终于成功了,这里分享一下我的部署过程. Linux Disibutaion:Ubuntu 14.04 Web Server:nginx. ...