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-mysql 问题笔记一——在命令行中可以执行的sql语句,无法从php页面页面执行!

    我的情况: 1.由于外键较多,插入数据时,提前关闭外键(SET FOREIGN_KEY_CHECKS=0). 2.所使用的sql语句中,有外键绑定到其他表中,所以无法从php页面插入. 原因分析: S ...

  2. C语言笔记——简介与编译过程初探

    序言 从今天起,详细说说C语言.这一年多,在大多数语言和技术之间转了一大圈,终于看清楚了事实,决心静下心来好好学学C语言.初学者会认为C语言是个入门用的东西,没有必要深入研究.但对计算机领域再稍加了解 ...

  3. Solaris-[ODBC-ORACLE WP Driver]遇到的几个问题

    确保之前已装好ORACLE和ODBC,ODBC连接数据库时会出现几个问题 一.登陆oracle并启动 [root@bunsol:/export]$su - oracle Oracle Corporat ...

  4. Java WebService把Date类型转换成XMLGregorianCalendar

    JavaEE 的WebService中的Date类型在Web应用中调set方法的时候,默认情况下,JAXB将xsd:date, xsd:time, 和xsd:dateTime映射为XMLGregori ...

  5. C# System.Guid.NewGuid() 【转】

    概念 GUID: 即Globally Unique Identifier(全球唯一标识符) 也称作 UUID(Universally Unique IDentifier) . GUID是一个通过特定算 ...

  6. auto_ptr 要点解析

    今天看了auto_ptr类的用法,又仔细看了看C++标准库中的符合标准的auto_ptr类别的实作,觉得自己基本上理解了auto_ptr的原理,下面就我的心得写几句,有不正确的地方,希望多多指教. 1 ...

  7. 使用Vitamio打造自己的Android万能播放器(5)——在线播放(播放优酷视频)

    前言 为了保证每周一篇的进度,又由于Vitamio新版本没有发布, 决定推迟本地播放的一些功能(截图.视频时间.尺寸等),跳过直接写在线播放部分的章节.从Vitamio的介绍可以看得出,其支持http ...

  8. Java基础学习笔记2

    运算符: 重点:++和--运算符; a++ (a--):表示先将a的原值带入计算,计算完毕后,再将a的值进行+1(-1); ++a (--a):先将a的值进行+1(-1)运算,然后将+1(-1)以后的 ...

  9. 利用VS自带的命令行工具查看和生产PublicKeyToken (转)

    使用VS2013(或其他版本)命令行工具,键入:SN -T C:\*****.dll 就会显示出该dll具体的PublicKeyToken数值. 如果该程序集没有强命 名,则不会有PublicKeyT ...

  10. 蓝桥杯算法训练<一>

    一.图形显示 此题虽然简单,但是需啊哟注意的是,每个“*”后边有一个空格] 问题描述 编写一个程序,首先输入一个整数,例如5,然后在屏幕上显示如下的图形(5表示行数): * * * * * * * * ...