sql优化-总结
1、尽量缩小数据范围。
2、能一个sql解决的,坚决不用两条sql。利用case when或decode。
select month_id,
corppkno,
sum(exportSum_new) exportSum_new,
sum(exportSum_newLy) exportSum_newLy,
sum(exportSum_Support) exportSum_Support,
sum(exportSum_SupportLy) exportSum_SupportLy
from ( /*当年累计出口*/
select a.month_id,
c.corppkno,
decode(a.isnewinsurant, null, 0, b.exportdollar) exportSum_new,/*此处为了用一条sql实现*/
0 exportSum_newLy,
b.exportdollar exportSum_Support,
0 exportSum_SupportLy
from (select trunc(t1.month_id / 100) yearid,
t1.month_id,
t3.cocode,
max(t.newinsurantpkno_sm) isnewinsurant /*当月新增的客户*/
from stdw.F_Sum_SupportInsurant_SM t,
stdw.lu_month_cumulate t1,
stdw.d_t_customer t2,
stdw.d_t_Customsenterprisemapping t3
where t.monthid = t1.month_cumul_id
and t.supportinsuantpkno_sm = t2.pkno
and t2.crmno = t3.customno
and t3.state = '1'
and t1.month_id <= to_char(sysdate - 1, 'YYYYMM')
group by t1.month_id, t3.cocode) A,
stdw.f_custom_company_composite B,
stdw.d_custom_branch_province C,
stdw.lu_month_cumulate D /*此sql先用子查询A限定范围,再通过A去关联B。因为B的范围大,如果对B进行汇总后再和A关联,效率较低*/
where b.monthid = d.month_cumul_id
and b.corpid = c.corpid
and a.yearid = b.yearid /*跨区访问*/
and a.month_id = d.month_id
and a.cocode = b.cocode
union all
/*上年总出口额*/
select a.month_id,
b.corppkno,
0 exportSum_new,
decode(a.isnewinsurant, null, 0, b.exportdollar) exportSum_newLy,
0 exportSum_Support,
b.exportdollar exportSum_SupportLy
from (select trunc(t1.month_id / 100) - 1 yearid_ly,
t1.month_id,
t3.cocode,
max(t.newinsurantpkno_sm) isnewinsurant /*当月新增的客户*/
from stdw.F_Sum_SupportInsurant_SM t,
stdw.lu_month_cumulate t1,
stdw.d_t_customer t2,
stdw.d_t_Customsenterprisemapping t3
where t.monthid = t1.month_cumul_id
and t.supportinsuantpkno_sm = t2.pkno
and t2.crmno = t3.customno
and t3.state = '1'
and t1.month_id <= to_char(sysdate - 1, 'YYYYMM')
group by t1.month_id, t3.cocode) A,
(select t1.outputyear yearid,
t1.cocode,
t4.corppkno,
t1.totaldollar exportdollar
from stdw.f_custom_company_total t1,
stdw.d_custom_company t2,
stdw.d_custom_province_zone t3,
stdw.d_custom_branch_province t4
where t1.cocode = t2.cocode
and t2.zonecode = t3.zone
and t3.province_no = t4.proviceid) B
where a.yearid_ly = B.yearid
and a.cocode = B.cocode)
group by month_id, corppkno
sql优化-总结的更多相关文章
- SQL优化案例—— RowNumber分页
将业务语句翻译成SQL语句不仅是一门技术,还是一门艺术. 下面拿我们程序开发工程师最常用的ROW_NUMBER()分页作为一个典型案例来说明. 先来看看我们最常见的分页的样子: WITH CTE AS ...
- sql 优化
1.选择最有效率的表名顺序(只在基于规则的优化器中有效): oracle的解析器按照从右到左的顺序处理 from 子句中的表名,from子句中写在最后的表(基础表driving table)将被最先处 ...
- SQL 优化总结
SQL 优化总结 (一)SQL Server 关键的内置表.视图 1. sysobjects SELECT name as '函数名称',xtype as XType FROM s ...
- (转)SQL 优化原则
一.问题的提出 在应用系统开发初期,由于开发数据库数据比较少,对于查询SQL语句,复杂视图的的编写等体会不出SQL语句各种写法的性能优劣,但是如果将应用 系统提交实际应用后,随着数据库中数据的增加,系 ...
- sql优化阶段性总结以及反思
Sql优化思路阶段性心得: 这段时间的优化做了好几个案例,其实有很多的类似点,都是好几张大表的相互连接,然后执行长达好几个小时,甚至都跑不出来. 自己差不多的思路就是Parallel full tab ...
- mysql sql优化实例
mysql sql优化实例 优化前: pt-query-degist分析结果: # Query 3: 0.00 QPS, 0.00x concurrency, ID 0xDC6E62FA021C85B ...
- SQL优化技巧
我们开发的大部分软件,其基本业务流程都是:采集数据→将数据存储到数据库中→根据业务需求查询相应数据→对数据进行处理→传给前台展示.对整个流程进行分析,可以发现软件大部分的操作时间消耗都花在了数据库相关 ...
- ORACLE常用SQL优化hint语句
在SQL语句优化过程中,我们经常会用到hint,现总结一下在SQL优化过程中常见Oracle HINT的用法: 1. /*+ALL_ROWS*/ 表明对语句块选择基于开销的优化方法,并获得最佳吞吐量, ...
- SQL优化有偿服务
本人目前经营MySQL数据库的SQL优化服务,100块钱一条.具体操作模式 其中第一条,可以通过在微信朋友圈转发链接中的信息(http://www.yougemysqldba.com/discuz/v ...
- 【MySQL】SQL优化系列之 in与range 查询
首先我们来说下in()这种方式的查询 在<高性能MySQL>里面提及用in这种方式可以有效的替代一定的range查询,提升查询效率,因为在一条索引里面,range字段后面的部分是不生效的. ...
随机推荐
- PHP面试题之文件目录操作
获取文件后缀,遍历目录层次 /** * 5种方式获取文件后缀名 * 这道题其实就是考函数substr() strrchr() array_pop() strrpos() strpos() strrev ...
- JDK常见问题 环境变量配置
"javac不是内部命令或外部命令" Windows7 安装"jdk-6u26-windows-x64.exe"后,常提示"javac不是内部命令或外 ...
- WPF学习:绑定
原文 http://www.cnblogs.com/SouthAurora/archive/2010/06/30/1768464.html 一.绑定到元素对象 1.元素和元素(XAML.代码) 1.1 ...
- php快递查询
http://www.oschina.net/code/snippet_60100_25087 <?php class Express { private $expressname =array ...
- Div布局案例
通常看到这个页面,会想到它是有几块组成的. 第一块,分销佣金. 第二块,包括代言.商品.二维码 其中代言又是左右结构. 于是乎基本的div结构就出来了. <div class="row ...
- c语言结构体排序示例
设计性实验编程实现对学生成绩表的相关信息排序.实验要求:⑴ 建立一个由n个学生的考试成绩表,每条信息由学号.姓名和分数组成.⑵ 按学号排序,并输出排序结果.⑶ 按分数排序,分数相同的则按学号有序,并输 ...
- 解密yii中CModule::_components和CModule::_componentConfig
array CModule::_components 所有组件对象(CComponent的子类)将作为键值存在该数组中, 键名是定义该组件时使用的键名.例如: protected function r ...
- IOS学习笔记07---C语言函数-printf函数
IOS学习笔记07---C语言函数-printf函数 0 7.C语言5-printf函数 ------------------------- ----------------------------- ...
- Android上下左右滑动,显示底层布局
转载博客地址:http://www.cnblogs.com/flyme2012/p/4106308.html 闲着没事做了一个小东西.Android的上下左右滑动,显示底层布局.可以做类似于QQ消息列 ...
- java中文乱码解决之道(三)—–编码详情:伟大的创想—Unicode编码
原文出处:http://cmsblogs.com/?p=1458 随着计算机的发展.普及,世界各国为了适应本国的语言和字符都会自己设计一套自己的编码风格,正是由于这种乱,导致存在很多种编码方式,以至于 ...