待优化语句如下

select a.code as code, a.m_code as m_code,a.stktype as f_stype,a.e_year as e_year,
b.sname as sname,a.c_date as c_date,to_char(sysdate,'YYYYMMDD') as createtime,
to_char(sysdate,'YYYYMMDD') as updatetime,
(select sum(valuef2) from a t where t.code=a.code and t.c_date between to_char(to_date(a.c_date,'YYYYMMDD')-180,'YYYYMMDD') and a.c_date and t.e_year=a.e_year) e70115_70011,
(select sum(valuef1) from a t where t.code=a.code and t.c_date between to_char(to_date(a.c_date,'YYYYMMDD')-180,'YYYYMMDD') and a.c_date and t.e_year=a.e_year) e70104_70011,
(select sum(valuef6) from a t where t.code=a.code and t.c_date between to_char(to_date(a.c_date,'YYYYMMDD')-180,'YYYYMMDD') and a.c_date and t.e_year=a.e_year) e70126_70011,
(select sum(valuef2) from a t where t.code=a.code and t.c_date between to_char(to_date(a.c_date,'YYYYMMDD')-180,'YYYYMMDD') and a.c_date and t.e_year=a.e_year) e70131_70011,
'-' as f_unit
from a,b@link b
where a.code = b.code
and b.stype=2 and b.status=1 and c_date>to_char(sysdate-3,'YYYYMMDD')

首先分析下标量子查询中的过滤条件:

t.c_date between to_char(to_date(a.c_date,'YYYYMMDD')-180,'YYYYMMDD') and a.c_date

该语句的目标实现c_date 180天内的数据汇总,因此可以分析函数表示为

order by to_date(c_date,'YYYYMMDD') range between 180 preceding current row

标量子查询的语句可改写为

sum(valuef2) over(partition by a.code,a.year order by to_date(a.c_date,'YYYYMMDD') range between 180 preceding current row)

而我们只需要三天内的数据,所以加上case判断

case
when a.c_date>to_char(sysdate-3,'YYYYMMDD')
then
sum(valuef2) over(partition by a.code,a.year order by to_date(a.c_date,'YYYYMMDD') range between 180 preceding current row)
end

最终整体语句可改写为

select A.*,b.sname as sname,to_char(sysdate,'YYYYMMDD') as createtime,
to_char(sysdate,'YYYYMMDD') as updatetime from
(select a.code as code,a.m_code as m_code, a.stktype as f_stype,a.e_year as e_year, a.c_date as c_date,
case
when a.c_date>to_char(sysdate-3,'YYYYMMDD')
then
sum(valuef2) over(partition by a.code,a.year order by to_date(a.c_date,'YYYYMMDD') range between 180 preceding current row)
end as f70115_70011,
case
when a.c_date>to_char(sysdate-3,'YYYYMMDD')
then
sum(valuef1) over(partition by a.code,a.year order by to_date(a.c_date,'YYYYMMDD') range between 180 preceding current row)
end as f70104_70011,
case
when a.c_date>to_char(sysdate-3,'YYYYMMDD')
then
sum(valuef6) over(partition by a.code,a.year order by to_date(a.c_date,'YYYYMMDD') range between 180 preceding current row)
end as f70126_70011,
case
when a.c_date>to_char(sysdate-3,'YYYYMMDD')
then
sum(valuef5) over(partition by a.code,a.year order by to_date(a.c_date,'YYYYMMDD') range between 180 preceding current row)
end as f70131_70011,
'-' as f_unit
from a where a.c_date>= to_char(sysdate-3-180,'YYYYMMDD') ---缩小数据区间
) A inner join b@link B on(A.code=B.code)
where B.stype=2 and B.status=1 and A.c_date>=to_char(sysdate-3,'YYYYMMDD')

随着数据量的增加该优化的效率越明显

Oracle sql优化之分析函数优化标量子查询的更多相关文章

  1. SQL Server的优化器会缓存标量子查询结果集吗

    在这篇博客"ORACLE当中自定义函数性优化浅析"中,我们介绍了通过标量子查询缓存来优化函数性能: 标量子查询缓存(scalar subquery caching)会通过缓存结果减 ...

  2. 优化有标量子查询的SQL

    数据库环境:SQL SERVER 2008R2 今天在数据库中抓出一条比较耗费资源的SQL,只返回904条数据,居然跑了40多分钟.SQL及对应的数据量如下图: SELECT saft04.cur_y ...

  3. SQL优化-标量子查询(数据仓库设计的隐患-标量子查询)

    项目数据库集群出现了大规模节点宕机问题.经查询,问题在于几张表被锁.主要问题在于近期得几个项目在数据库SQL编写时大量使用了标量子查询. 为确定为题确实是由于数据表访问量超过单节点限制,做了一些测试. ...

  4. Oracle SQL高级编程——分析函数(窗口函数)全面讲解

    Oracle SQL高级编程--分析函数(窗口函数)全面讲解 注:本文来源于:<Oracle SQL高级编程--分析函数(窗口函数)全面讲解> 概述 分析函数是以一定的方法在一个与当前行相 ...

  5. 彻底搞懂oracle的标量子查询

    oracle标量子查询和自己定义函数有时用起来比較方便,并且开发者也常常使用.数据量小还无所谓.数据量大,往往存在性能问题. 下面測试帮助大家彻底搞懂标量子查询. SQL> create tab ...

  6. 标量子查询SQL改写

    一网友说下面sql跑的好慢,让我看看 sql代码: select er, cid, pid, tbl, zs, sy, (select count(sr.mobile_tele_no) from tb ...

  7. 标量子查询调优SQL

    fxnjbmhkk4pp4 select /*+ leading (wb,sb,qw) */ 'blocker('||wb.holding_session||':'||sb.username||')- ...

  8. [20180626]函数与标量子查询14.txt

    [20180626]函数与标量子查询14.txt --//前面看http://www.cnblogs.com/kerrycode/p/9099507.html链接,里面提到: 通俗来将,当使用标量子查 ...

  9. SQL Server中INNER JOIN与子查询IN的性能测试

    这个月碰到几个人问我关于"SQL SERVER中INNER JOIN 与 IN两种写法的性能孰优孰劣?"这个问题.其实这个概括起来就是SQL Server中INNER JOIN与子 ...

随机推荐

  1. 高精度运算专题2-减法运算(The subtraction operation)

    这个专题呢,我就来讲讲高精度的减法,下面是三个计算减法的函数,第一个函数是char类型的,要对字符串进行数字转换,而第二个是两个int类型的数组,不用转换成数字,第三个则更为优化,用a数组-b数组放回 ...

  2. .net大型分布式电子商务架构说明(转载来自<头条>)

    背景 构建具备高可用,高扩展性,高性能,能承载高并发,大流量的分布式电子商务平台,支持用户,订单,采购,物流,配送,财务等多个项目的协作,便于后续运营报表,分析,便于运维及监控. 架构演变 基础框架剥 ...

  3. java innerclass

    ---恢复内容开始--- 内部类: public class Inner{ public class Inner2{} } 创建内部类对象 .new public class Test {    in ...

  4. D - 小晴天老师系列——晴天的后花园

    D - 小晴天老师系列——晴天的后花园 Time Limit: 10000/5000MS (Java/Others)    Memory Limit: 128000/64000KB (Java/Oth ...

  5. SQL Server删除表信息的三种方法

    1.使用DELETE实现SQL Server删除表信息 (1)删除表中的全部信息 USE student GO DELETE student      --不加where条件,删除表中的所有记录 go ...

  6. C语言指针强制转化的应用

    指针类型强制转化在kernel设计中非常常见,这里记录两个非常有意思的用法: 1.对地址进行运算.任何虚拟地址都表示成void *va  = (void *) 100, *(va + 1) ==101 ...

  7. eclipse中maven工程的创建javaweb项目

    第一步.new一个新的工程 ,选中maven project,点击next 第2步.不做选择,点击next 第3步.选择maven-archetype-webapp,点击next 第四步.填写项目名称 ...

  8. poll机制分析[转]

    所有的系统调用,基于都可以在它的名字前加上"sys_"前缀,这就是它在内核中对应的函数.比如系统调用open.read.write.poll,与之对应的内核函数为:sys_open ...

  9. H5加载优化

  10. 使用monit搭建一个监控系统

    上周用monit搭建或者说定制了一个监控系统,来监控服务器发生事情.当然了主要是监控异常,因为我们的产品属于服务器类型,很多进程都daemon,要不停的运行.我们搭建监控目的不过是出现问题能够及时的知 ...