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与子 ...
随机推荐
- runtime官方文档
OC是一种面向对象的动态语言,作为初学者可能大多数人对面向对象这个概念理解的比较深,而对OC是动态语言这一特性了解的比较少.那么什么是动态语言?动态语言就是在运行时来执行静态语言的编译链接的工作.这就 ...
- SVN通过域名连不上服务器地址(svn: E175002: OPTIONS request failed on '/svn/yx-SVN-Server' Connection refused: connect)
用域名直连就连不上,如果换成了ip直连就可以连接上去了 https://yx-server01/svn/yx-SVN-Server 换为了 https://192.168.188.208/svn/yx ...
- yii2图片验证码
控制器LoginController.php <?php namespace backend\controllers; use Yii; use yii\debug\models\search\ ...
- for计算100以内的奇数和
#include "stdio.h" void main() { //for计算100以内的奇数和 步长为1,continue实现 ; ;i<=;i++) { ==) { c ...
- 一个有用的shell脚本
#!/bin/bash #if [ $1 -eq null ]; then # echo "please input params1!" # exit #fi #if [ $2 - ...
- caffe+NVIDIA安装+CUDA-7.5+ubuntu14.04(显卡GTX1080)
首先强调,我们实验室的机器是3.3w的机器,老板专门买来给我们搞深度学习,其中显卡是NVIDIA GeForce GTX1080(最近新出的,装了两块),cpu是intel i7处理器3.3Ghz, ...
- POJ 3416 Crossing
树状数组+离线操作 #include<stdio.h> #include<string.h> #include<math.h> #include<algori ...
- 编辑器phpstrom的快捷键修改
file->setting-->查找 keymap -->查找 format 格式化代码 ctrl+alt +L appearance-->外观-->显示行号
- Infix to postfix conversion 中缀表达式转换为后缀表达式
Conversion Algorithm 1.操作符栈压入"#": 2.依次读入表达式的每个单词: 3.如果是操作数则压入操作数栈: 4.如果是操作符,则将操作符栈顶元素与要读入的 ...
- 找最大重复次数的数和重复次数(C++ Pair)
Problem A: 第一集 你好,世界冠军 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 265 Solved: 50[Submit][Statu ...