关于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 ...
随机推荐
- Model
using System.Collections.Generic; using System.ComponentModel.DataAnnotations; namespace iFlytekDemo ...
- 2、netlink简介
Netlink 是一种特殊的 socket,它是 Linux 所特有的,类似于 BSD 中的AF_ROUTE 但又远比它的功能强大,目前在最新的 Linux 内核(2.6.14)中使用netlink ...
- vb.net中常用键值
可在代码中的任何地方用下列常数代替实际值: 常数 值 描述 vbKeyLButton 0x1 鼠标左键 vbKeyRButton 0x2 鼠标右键 vbKeyCancel 0x3 CANCEL 键 v ...
- Android_CntextMenu_example_textSize
menu.xml <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item ...
- Objec类和final关键字的用法
一.在继承中,子类继承父类,那父类继承谁呢,java中如果类没有指定继承谁,那么就默认继承Object类,object类是所有对象的直接或者间接的父类,根类,或者叫做基类,它里面定义的功能所有的对象都 ...
- HashMap、HashSet源代码分析其 Hash 存储机制
集合和引用 就像引用类型的数组一样,当我们把 Java 对象放入数组之时,并不是真正的把 Java 对象放入数组中,只是把对象的引用放入数组中,每个数组元素都是一个引用变量. 实际上,HashSet ...
- 【高级JEE技术】JMX
JMX即Java Manager Extentin(java 管理扩展)一种动态改变javabean属性值的技术,具体应用场景可以有很多.比如使用JMX作为线上应用的开关,在做一些新老系统改造的时候 ...
- scala学习笔记:理解并行集合par
scala> (1 to 5).foreach(println(_)) 1 2 3 4 5 scala> (1 to 5).par.foreach(println(_)) 3 1 4 2 ...
- arguments 函数内部属性
1.arguments 是在function方法里面的,是实参数组,用法是挺多的,下面来记录一下 2.利用arguments实现方法的重载 //01.使用argument模拟方法重载 function ...
- xxx is not in the sudoers file.This incident will be reported.的解决方法
1.切换到root用户下,怎么切换就不用说了吧,不会的自己百度去. 2.添加sudo文件的写权限,命令是:chmod u+w /etc/sudoers 3.编辑sudoers文件vi /etc/sud ...