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优化-总结的更多相关文章

  1. SQL优化案例—— RowNumber分页

    将业务语句翻译成SQL语句不仅是一门技术,还是一门艺术. 下面拿我们程序开发工程师最常用的ROW_NUMBER()分页作为一个典型案例来说明. 先来看看我们最常见的分页的样子: WITH CTE AS ...

  2. sql 优化

    1.选择最有效率的表名顺序(只在基于规则的优化器中有效): oracle的解析器按照从右到左的顺序处理 from 子句中的表名,from子句中写在最后的表(基础表driving table)将被最先处 ...

  3. SQL 优化总结

    SQL 优化总结 (一)SQL Server 关键的内置表.视图 1. sysobjects         SELECT name as '函数名称',xtype as XType  FROM  s ...

  4. (转)SQL 优化原则

    一.问题的提出 在应用系统开发初期,由于开发数据库数据比较少,对于查询SQL语句,复杂视图的的编写等体会不出SQL语句各种写法的性能优劣,但是如果将应用 系统提交实际应用后,随着数据库中数据的增加,系 ...

  5. sql优化阶段性总结以及反思

    Sql优化思路阶段性心得: 这段时间的优化做了好几个案例,其实有很多的类似点,都是好几张大表的相互连接,然后执行长达好几个小时,甚至都跑不出来. 自己差不多的思路就是Parallel full tab ...

  6. mysql sql优化实例

    mysql sql优化实例 优化前: pt-query-degist分析结果: # Query 3: 0.00 QPS, 0.00x concurrency, ID 0xDC6E62FA021C85B ...

  7. SQL优化技巧

    我们开发的大部分软件,其基本业务流程都是:采集数据→将数据存储到数据库中→根据业务需求查询相应数据→对数据进行处理→传给前台展示.对整个流程进行分析,可以发现软件大部分的操作时间消耗都花在了数据库相关 ...

  8. ORACLE常用SQL优化hint语句

    在SQL语句优化过程中,我们经常会用到hint,现总结一下在SQL优化过程中常见Oracle HINT的用法: 1. /*+ALL_ROWS*/ 表明对语句块选择基于开销的优化方法,并获得最佳吞吐量, ...

  9. SQL优化有偿服务

    本人目前经营MySQL数据库的SQL优化服务,100块钱一条.具体操作模式 其中第一条,可以通过在微信朋友圈转发链接中的信息(http://www.yougemysqldba.com/discuz/v ...

  10. 【MySQL】SQL优化系列之 in与range 查询

    首先我们来说下in()这种方式的查询 在<高性能MySQL>里面提及用in这种方式可以有效的替代一定的range查询,提升查询效率,因为在一条索引里面,range字段后面的部分是不生效的. ...

随机推荐

  1. TCP粘包拆包问题

    阿π 专注于网络协议,系统底层,服务器软件 C++博客 | 首页 | 发新随笔 | 发新文章 | | | 管理 Socket粘包问题 这两天看csdn有一些关于socket粘包,socket缓冲区设置 ...

  2. List<int>是值类型还是引用类型

    class Program { static void Main(string[] args) { List<int> lst = new List<int>(); lst.A ...

  3. DIV+CSS规范命名

    一.命名规则说明: 1).所有的命名最好都小写2).属性的值一定要用双引号("")括起来,且一定要有值如class="divcss5",id="div ...

  4. jquery validation plugin 使用

    <!DOCTYPE html> <!-- To change this license header, choose License Headers in Project Prope ...

  5. Ubuntu之修改用户名和主机名

    记得曾几何时,想把自己电脑的“乌班兔儿”取个响亮的名字,但是问了很久度娘和谷哥,都要我把当前用户删除了(userdel -r xxx),重新建一个用户(adduser xxx),但是,我的电脑是所有环 ...

  6. AndroidUI 视图动画-移动动画效果 (TranslateAnimation)

    移动动画效果可以使用 TranslateAnimation; <Button android:id="@+id/btnTranslate1" android:layout_w ...

  7. iOS 性能测试 - FBMemoryProfiler

    FBMemoryProfiler 是Facebook开源的一款用于分析iOS内存使用和检测循环引用的工具库. 脑补:http://www.cocoachina.com/ios/20160421/159 ...

  8. android开发之Animations的使用(二)

    android开发之Animations的使用(二) 本博文主要讲述的是android开发中的animation动画效果的使用,和上一篇博文不同的是,此次四种动画效果,主要使用的是xml文件实现的,提 ...

  9. JFinal开发web项目出现故障小记

    导读 作为中国优秀的开源项目之中的一个JFinal有着极速开发的优点,是中小型应用开发的首选.在导师的建议下.我使用了JFinal来开发一个Java服务端应用,官方教程非常easy.就几十页(当然是中 ...

  10. CRM Entity 之Money转string int类型等

    Money转string 左右都是string //服务站地址 vehicleDetail["yt_servicestation_address"]=serviceStationC ...