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. mudOS配置

    # 本泥巴的名称name : new夕阳再现# 定义用来接受玩家连线的端口#单端口 port number : XXXX#双端口如下:external_port_1 : telnet 10010ext ...

  2. Best Financing(HD4833)

    每笔收入产生的收益是独立的. 计算所有点的收益率,累计. #define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<al ...

  3. System V 机制(转)

    引言 UNIX 内核管理的进程自主地操作,从而产生更稳定的系统.然而,每个开发人员最终都会遇到这样的情况,即其中一组进程需要与另一组进程通信,也许是为了交换数据或发送命令.这种通信称为进程间通信(In ...

  4. TensorFlow 深度学习笔记 逻辑回归 实践篇

    Practical Aspects of Learning 转载请注明作者:梦里风林 Github工程地址:https://github.com/ahangchen/GDLnotes 欢迎star,有 ...

  5. Emacs配置erlang开发环境(.emacs 文件)

    以前都是用sublime写erlang代码,好处不多说,主要是觉得一点不好用,不能实现函数跳转,及其不方便,尤其是代码一多,头疼.后来折腾过IntelliJ,下了个收费$0.00的版本,风格还是挺稀饭 ...

  6. 有关于/home出现100%被占用 与 vnc的关系

    我在远程服务器时,通常使用的程序是vnc.因为vnc可以图形化界面,操作效果比用putty好很多. 但是,我发现使用vnc有一个问题,就是/home占用空间会达到100%. [zy@islab62 ~ ...

  7. Buffer Cache(缓冲区缓存)篇:keep缓冲区池(保留池)

    Buffer  Cache可以有三个池 默认缓冲区池 keep缓冲区池 recycling缓冲区池 --保留池和回收池可以独立于sga中的其他缓存分配内存.创建表的时候可以在storage子句中使用b ...

  8. Hoeffding连接到机器学习

    统计学场景: 一个罐子中有红球和绿球,红球比例$v$未知,数量未知,如何得到红球比例?方法---随机抽样N个球,在其中红球占比为$u$ 由hoeffding可以知道:$P(|u-v|>\epsi ...

  9. PS快捷键大全

    一.工具箱(多种工具共用一个快捷键的可同时按[Shift]加此快捷键选取)  矩形.椭圆选框工具 [M]  移动工具 [V]  套索.多边形套索.磁性套索 [L]  魔棒工具 [W]  裁剪工具 [C ...

  10. iOS中通知传值

    NSNotification 通知中心传值,可以跨越多个页面传值, 一般也是从后面的页面传给前面的页面.   思路: 第三个界面的值传给第一个界面. 1. 在第一个界面建立一个通知中心, 通过通知中心 ...