待优化语句如下

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. NOIP2011-普及组复赛-第二题-统计单词数

    题目描述 Description 一般的文本编辑器都有查找单词的功能,该功能可以快速定位特定单词在文章中的位置,有的还能统计出特定单词在文章中出现的次数.  现在,请你编程实现这一功能,具体要求是:给 ...

  2. 工具类 util.img

        /**     * @description transform emotion image url between code     * @author x.radish     * @pa ...

  3. 【strtok()】——分割字符串

    对字符串进行分割: 在使用前需要先初始化例如: char * p=strtok(Str," ");/*初始化以" "(以空格字符来分割字符串),即把" ...

  4. html5+css3学习笔记音频和视频

    格式 IE Firefox Opera Chrome Safari Ogg No 3.5+ 10.5+ 5.0+ No MPEG 4 9.0+ No No 5.0+ 3.0+ WebM No 4.0+ ...

  5. windows服务-log4net的使用

    本文转自http://www.cnblogs.com/puzi0315/archive/2012/08/08/2628966.html Log4net监控服务状态 对于比较复杂的逻辑,可以使用log4 ...

  6. Session和Cookie的使用总结

    Session和Cookie的使用总结: Session和cookie都是asp.Net中的内置对象,至于他们有什么区别,在这里就不在多说,现在来说说一些比较实用点的东西: 我们知道网站都有一个后台管 ...

  7. 关于perl闭包(个人理解)

    我个人理解,就是当一个变量超出作用域时,应是消失了,不见了的,但你还能访问它,这就是闭包. # #看下面的例子. #!/usr/bin/env perl -w use strict; { my $va ...

  8. WebDriver获取table的内容(通过动态获取Table单元格的TagName对其innerHTML值进行获取)

    import java.util.ArrayList;import java.util.Iterator;import java.util.LinkedHashMap;import java.util ...

  9. U盘做svn版本控制

    svn提供的访问方式有: file:///本地路径/to/svnrepo/ //访问本地磁盘 http://host/to/svnrepo/ //通过配置subversion的apache服务器的we ...

  10. 三、oracle 用户管理(user)

    一.创建用户概述:在oracle中要创建一个新的用户使用create user语句,一般是具有dba(数据库管理员)的权限才能使用.create user 用户名 identified by 密码;  ...