关于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 ...
随机推荐
- 初探 MySQL 的 Binlog
https://xcoder.in/2015/08/10/mysql-binlog-try/
- 运维知识体系v0.5
http://www.90qj.com/?post=318http://ixdba.blog.51cto.com/2895551/1751377 运维知识体系v0.5-(运维社区-赵班长出品,欢迎 ...
- C++ stringstream介绍,使用方法与例子
From: http://www.usidcbbs.com/read-htm-tid-1898.html C++引入了ostringstream.istringstream.stringstream这 ...
- tcl/tk
http://blog.csdn.net/dulixin/article/category/365058 http://blog.csdn.net/dulixin/article/category/3 ...
- jqgrid项目使用记录/注释版/未整理/
jqgrid项目使用笔记function loadActData(){ $("#actGridTable").clearGridData();//首先清空表格内容 var url ...
- sharepoint 删除list里的所有内容
[System.reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") $siteUrl = " ...
- JQuery 之 Ajax 异步和同步浅谈
Ajax 同步和异步的区别 同步是当 JS 代码加载到当前 Ajax 的时候会把页面里所有的代码停止加载,页面出现假死状态:当这个 Ajax 执行完毕后才会继续运行其他代码此时页面假死状态才会解除.反 ...
- Razor引擎学习:RenderBody,RenderPage和RenderSection
ASP.NET MVC 3 已经正式发布了,现在估计许多人都在拼命学,我也不能例外,刚刚看到了一篇文章,介绍了三个非常有用的方法:RenderBody,RenderPage和RenderSection ...
- bat里如何用相对路径
在bat中直接使用绝对路径没有问题,但是文件传到其他地方时,绝对路径会发生改变,因此想通过使用相对路径来解决. 可以通过在bat获取当前bat所在的目录,然后cd 该目录来解决该问题 在bat前面增加 ...
- "const wchar_t is incompatible with parameter of type "LPCSTR"
MessageBox(NULL, L"TEST", L"TEST", MB_OK); You may get this error if you "U ...