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. PHP中Global和Local范围以及Static变量

    1. Local scope function update_counter() { $counter++;//此处$counter为局部变量,与函数外的$counter非同一个 } $counter ...

  2. 管道通信之无名管道---pipe()

    pipe()函数在子进程产生之前就应该存在. 父子进程之间只进行一次传递 /*============================================ > Copyright ( ...

  3. gulp api

    gulp api 简介 gulp是前端开发过程中对代码进行构建的工具,是自动化项目的构建利器:它不仅能对网站资源进行优化,而且在开发过程中很多重复的任务能够使用正确的工具自动完成 gulp是基于Nod ...

  4. 一种基于Qt的可伸缩的全异步C/S架构服务器实现(流浪小狗,六篇,附下载地址)

    本文向大家介绍一种基于Qt的伸缩TCP服务实现.该实现针对C/S客户端-服务集群应用需求而搭建.连接监听.数据传输.数据处理均在独立的线程池中进行,根据特定任务不同,可安排负责监听.传输.处理的线程数 ...

  5. POJ——字符串插入

    2:字符串插入 查看 提交 统计 提问 总时间限制:  1000ms  内存限制:  65536kB 描述 有两个字符串str和substr,str的字符个数不超过10,substr的字符个数为3.( ...

  6. 如何用C#把Doc文档转换成rtf格式

    先在项目引用里添加上对Microsoft Word 9.0 object library的引用 using System; namespace DocConvert { class DoctoRtf ...

  7. Mongo散记--聚合(aggregation)&amp; 查询(Query)

    mongo官网:http://www.mongodb.org/ 工作中使用到Mongo,可是没有系统的学习研究过Mongo,仅对工作过程中,在Mongo的使用过程中的一些知识点做一下记录,并随时补充, ...

  8. Android AsyncHttpClient

    Android Asynchronous Http Client A Callback-Based Http Client Library for Android   Tweet Downloadve ...

  9. 自学SQL语言的例子(使用MySQL实现)

    SQL语言作为一种数据库管理的标准语言有着极为广泛的应用场景,菜鸟入门选用的数据库软件是轻量级的免费(这个极为重要)的MySQL,下载链接如下:http://www.mysql.com/downloa ...

  10. oracle误删除数据的恢复方法

    学习数据库时,我们只是以学习的态度,考虑如何使用数据库命令语句,并未想过工作中,如果误操作一下,都可能导致无可挽回的损失.当我在工作中真正遇到这些问题时,我开始寻找答案. 今天主要以oracle数据库 ...