Oracle sql优化之分析函数优化标量子查询
待优化语句如下
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优化之分析函数优化标量子查询的更多相关文章
- SQL Server的优化器会缓存标量子查询结果集吗
在这篇博客"ORACLE当中自定义函数性优化浅析"中,我们介绍了通过标量子查询缓存来优化函数性能: 标量子查询缓存(scalar subquery caching)会通过缓存结果减 ...
- 优化有标量子查询的SQL
数据库环境:SQL SERVER 2008R2 今天在数据库中抓出一条比较耗费资源的SQL,只返回904条数据,居然跑了40多分钟.SQL及对应的数据量如下图: SELECT saft04.cur_y ...
- SQL优化-标量子查询(数据仓库设计的隐患-标量子查询)
项目数据库集群出现了大规模节点宕机问题.经查询,问题在于几张表被锁.主要问题在于近期得几个项目在数据库SQL编写时大量使用了标量子查询. 为确定为题确实是由于数据表访问量超过单节点限制,做了一些测试. ...
- Oracle SQL高级编程——分析函数(窗口函数)全面讲解
Oracle SQL高级编程--分析函数(窗口函数)全面讲解 注:本文来源于:<Oracle SQL高级编程--分析函数(窗口函数)全面讲解> 概述 分析函数是以一定的方法在一个与当前行相 ...
- 彻底搞懂oracle的标量子查询
oracle标量子查询和自己定义函数有时用起来比較方便,并且开发者也常常使用.数据量小还无所谓.数据量大,往往存在性能问题. 下面測试帮助大家彻底搞懂标量子查询. SQL> create tab ...
- 标量子查询SQL改写
一网友说下面sql跑的好慢,让我看看 sql代码: select er, cid, pid, tbl, zs, sy, (select count(sr.mobile_tele_no) from tb ...
- 标量子查询调优SQL
fxnjbmhkk4pp4 select /*+ leading (wb,sb,qw) */ 'blocker('||wb.holding_session||':'||sb.username||')- ...
- [20180626]函数与标量子查询14.txt
[20180626]函数与标量子查询14.txt --//前面看http://www.cnblogs.com/kerrycode/p/9099507.html链接,里面提到: 通俗来将,当使用标量子查 ...
- SQL Server中INNER JOIN与子查询IN的性能测试
这个月碰到几个人问我关于"SQL SERVER中INNER JOIN 与 IN两种写法的性能孰优孰劣?"这个问题.其实这个概括起来就是SQL Server中INNER JOIN与子 ...
随机推荐
- 第二次冲刺spring会议(第五次会议)
[例会时间]2014/5/8 21:15 [例会地点]9#446 [例会形式]轮流发言 [例会主持]马翔 [例会记录]兰梦 小组成员:兰梦 ,马翔,李金吉,赵天,胡佳奇
- SqlParameter关于Like的传参数无效问题
正确的写法(简洁版) private void GetHandleData(string strKeyWord1, string strKeyWord2, string strKeyWord3) { ...
- NOIP2005-普及组复赛-第二题-校门外的树
题目描述 Description 某校大门外长度为L的马路上有一排树,每两棵相邻的树之间的间隔都是1米.我们可以把马路看成一个数轴,马路的一端在数轴0的位置,另一端在L的位置:数轴上的每个整数点,即0 ...
- Openjudge-计算概论(A)-求平均年龄
描述: 班上有学生若干名,给出每名学生的年龄(整数),求班上所有学生的平均年龄,保留到小数点后两位. 输入第一行有一个整数n(1<= n <= 100),表示学生的人数.其后n行每行有1个 ...
- java抽象类的特点
抽象类 抽象类的特点: 1.抽象类的方法可以是抽象的,也可以是不抽象的. 2.抽象类的方法可以全是抽象的,也可以全是不抽象的. 3.有抽象方法的类必须是抽象类,或者是接口(但是接口的方法定义必须是pu ...
- ural 1698. Square Country 5(记忆化搜索)
1698. Square Country 5 Time limit: 2.0 secondMemory limit: 64 MB The first arithmetical operation ta ...
- hudson
来源: hudson入门与实战 http://www.360doc.com/content/15/0304/22/12144668_452603921.shtml Hudson安装配置.部署应用及分析 ...
- 艰辛五天:Ubuntu14.04+显卡驱动+cuda+Theano环境安装过程
题记:从一开始不知道显卡就是GPU(虽然是学计算机的,但是我真的不知道…脑残如我也是醉了),到搞好所有这些环境前后弄了5天时间,前面的买显卡.装显卡和装双系统见另一篇博客装显卡.双系统,这篇主要记录我 ...
- C#中泛型默认关键字(default)详解
我们在泛型类和泛型方法中产生的一个问题是,在预先未知以下情况时,如何将默认值分配给参数化类型 T:(T 是引用类型还是值类型?)对此我们将如何处理? C#代码实例: /// <summary&g ...
- wmts调用路径手工合成
wmts调用路径手工合成 一般OGC WMTS地图只提供了xml描述,地图应用常常要合成WMTS完整的调用URL.我们需要获知以下参数: BaseURL:例如 "http://10.36.5 ...