关于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
暂时记这么多。
关于SQL优化的一个小试例子的更多相关文章
- 简单聊聊TiDB中sql优化的一个规则---左连接消除(Left Out Join Elimination)
我们看看 TiDB 一段代码的实现 --- 左外连接(Left Out Join)的消除; select 的优化一般是这样的过程: 在逻辑执行计划的优化阶段, 会有很多关系代数的规则, 需要将逻辑执行 ...
- 关于sql优化的一个小总结
1.数据量大的时候,可以分多次查询2.有些数据的存储可以分主次表,此表存一些不常用的数据3.union all 比union效率要高4.尽量不要用distinct5.不返回不需要的行和列6.根据条件加 ...
- SQL优化 MySQL版 - 避免索引失效原则(二)
避免索引失效原则(二) 注:继上一篇文章继续讲解: 避免索引失效原则(一)https://www.cnblogs.com/StanleyBlogs/p/10482048.html#4195062 作者 ...
- 聊聊sql优化的15个小技巧
前言 sql优化是一个大家都比较关注的热门话题,无论你在面试,还是工作中,都很有可能会遇到. 如果某天你负责的某个线上接口,出现了性能问题,需要做优化.那么你首先想到的很有可能是优化sql语句,因为它 ...
- 关于SQL优化的辟谣
列举一些关于 SQL 语句的谣言,避免一些生瓜蛋子面试的时候被另外一群生瓜蛋子的 SQL 优化宝典给坑了. 以下所有内容都是 SQL Server 中的,其他数据库只能参考和借鉴 一.全表扫描 全表扫 ...
- 基于MySQL 的 SQL 优化总结
文章首发于我的个人博客,欢迎访问.https://blog.itzhouq.cn/mysql1 基于MySQL 的 SQL 优化总结 在数据库运维过程中,优化 SQL 是 DBA 团队的日常任务.例行 ...
- BATJ解决千万级别数据之MySQL 的 SQL 优化大总结
引用 在数据库运维过程中,优化 SQL 是 DBA 团队的日常任务.例行 SQL 优化,不仅可以提高程序性能,还能减低线上故障的概率. 目前常用的 SQL 优化方式包括但不限于:业务层优化.SQL 逻 ...
- SQL优化基础 使用索引(一个小例子)
按照本文操作和体会,会对sql优化有个基本最简单的了解,其他深入还需要更多资料和实践的学习: 1. 建表: 复制代码代码如下: create table site_user ( id int IDEN ...
- sql优化经典例子
场景 我用的数据库是mysql5.6,下面简单的介绍下场景 课程表 create table Course( c_id int PRIMARY KEY, name varchar(10) ) 数据10 ...
随机推荐
- 信号之abort函数
abort函数的功能是使异常程序终止. #include <stdlib.h> void abort(void); 此函数不返回 此函数将SIGABRT信号发送给调用进程(进程不应忽略此信 ...
- java_Cookie添加和删除
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, ...
- Android NDK STL
相信Android开发者都喜欢用C++编写一些高效的应用,有关Android NDK的C++开发相关知识总结如下: 从Android NDK r5开始支持了STL Port,在这个版本开始 ...
- 修改UIBarButtonItem字体大小、颜色等相关属性
在ios中如果想修改UIBarButtonItem里面的内容有很多种方法,常见的就是自定义contentView 但是有时候因为懒不想自定义只想在原来的文字上进行修改 如果只是修改UIBarButt ...
- Tomcat启动报错 Failed to start component [StandardServer[8005]]解决
SEVERE: The required Server component failed to start so Tomcat is unable to start. org.apache.catal ...
- nfs文件系统挂载失败解决方法
nfs文件系统挂载失败解决方法 */--> nfs文件系统挂载失败解决方法 Table of Contents 1. 错误提示 2. 我的配置 1 错误提示 bootserver=255.255 ...
- 实例化的两种方法(new和函数法)
// 定义类 类名字是 classA function classA(){ this.b=1; } classA.prototype.b=44; classA.prototype.s ...
- windows共享文件夹如何让CentOS 6.5读取
http://www.111cn.net/sys/CentOS/74104.htm 工作需要,需要把本地win7共享的文件夹让CenotOS 6.5服务器临时使用一下,以下是CentOS 6.5系统挂 ...
- SharePoint2013 SharePoint-Hosted 模式 分页方法
/**分页js插件 var ListPager = new listPaging(); 先调用start方法加载上下文 然后调用dataLoad方法查询第一页数据 需要设置几个属性值 ListPage ...
- C#学习笔记3:提示“截断字符串或二进制数据”错误解决方法
1.调试程序如出现“截断字符串或二进制数据”的关于数据库的错误,可以先试一试修改数据库中字符定义的长度. 2.使用ManualResetEvent前需导入 命名空间System.Threading; ...