业务场景的问题,我们有一个刷CUBE的SQL,是Oracle环境,平时跑70多分钟,

但是最近突然不动了,这个SQL需要算累计值,比如年累计客户数量。

累计值是什么意思呢?我们使用下面的数据来说明问题。

select '201901' as c_month, 100 as c_customers from dual union all
select '201902' as c_month, 102 as c_customers from dual union all
select '201903' as c_month, 120 as c_customers from dual union all
select '201904' as c_month, 111 as c_customers from dual union all
select '201905' as c_month, 155 as c_customers from dual union all
select '201906' as c_month, 199 as c_customers from dual; C_MONT C_CUSTOMERS
------ -----------
201901 100
201902 102
201903 120
201904 111
201905 155
201906 199

  

2019年1月,客户数量是100, 2019年2月,客户数量是102 ,

那么2019年1月的客户累计值是100,

2019年2月的客户年累计值是202(2019年1月的客户数量 + 2019年2月的客户数量),

2019年3月的客户年累计值是322(2019年1月的客户数量 + 2019年2月的客户数量+ 2019年3月的客户数量),

...

我使用如下的测试SQL来说明这个场景

create or replace view tab_test1 as
select '201901' as c_month, 100 as c_customers from dual union all
select '201902' as c_month, 102 as c_customers from dual union all
select '201903' as c_month, 120 as c_customers from dual union all
select '201904' as c_month, 111 as c_customers from dual union all
select '201905' as c_month, 155 as c_customers from dual union all
select '201906' as c_month, 199 as c_customers from dual union all
select '201907' as c_month, 108 as c_customers from dual; create view tab_test2 as
select '20190131' as monthlastday from dual union all
select '20190228' as monthlastday from dual union all
select '20190331' as monthlastday from dual union all
select '20190430' as monthlastday from dual union all
select '20190531' as monthlastday from dual union all
select '20190630' as monthlastday from dual union all
select '20190731' as monthlastday from dual; select *
from tab_test1 a
join tab_test2 b
on to_date(c_month,'yyyymm') <= to_date(b.monthlastday,'yyyymmdd')
and to_date(c_month,'yyyymm') >= trunc(to_date(b.monthlastday,'yyyymmdd'),'yyyy')
order by b.monthlastday , a.c_month; C_MONT C_CUSTOMERS MONTHLAS
------ ----------- --------
201901 100 20190131
201901 100 20190228
201902 102 20190228
201901 100 20190331
201902 102 20190331
201903 120 20190331
201901 100 20190430
201902 102 20190430
201903 120 20190430
201904 111 20190430
201901 100 20190531 C_MONT C_CUSTOMERS MONTHLAS
------ ----------- --------
201902 102 20190531
201903 120 20190531
201904 111 20190531
201905 155 20190531
201901 100 20190630
201902 102 20190630
201903 120 20190630
201904 111 20190630
201905 155 20190630
201906 199 20190630
201901 100 20190731 C_MONT C_CUSTOMERS MONTHLAS
------ ----------- --------
201902 102 20190731
201903 120 20190731
201904 111 20190731
201905 155 20190731
201906 199 20190731
201907 108 20190731 已选择 28 行。

  

从上面SQL返回的数据能看出来,monthlastday 字段分组,汇总c_customers,就能很轻松算出年累计值。但是如果a表数据太大,无法走hash 关联。

所以需要通过某种方法改成等值关联。

1. 先创建一个时间维表,可以通过树形查询生成一个时间维度表,由于我的测试数据自小粒度是到月的,所以我的日期维度表也是到月的。

create or replace view tab_test3 as
select extract(year from c_date) as c_year,
extract(month from c_date) as c_month,
to_char(c_date,'yyyymm') as c_month2
from (select add_months(date'2019-01-01',level -1 ) as c_date
from dual
connect by level <= 8);

  

2. 通过时间维度表自关联出累计月份对应的日期。

下面SQL,同构过滤t1 表的c_month2 字段,就可以拿到任意月份的累计月份了。比如2019-07月的累计月份是2019年 1-7月份

select t1.c_month2 as groupcolumn,t2.c_month2 joincolumn
from tab_test3 t1
join tab_test3 t2
on t1.c_year = t2.c_year
and t1.c_month2 >= t2.c_month2
order by 1,2 GROUPC JOINCO
------ ------
201901 201901
201902 201901
201902 201902
201903 201901
201903 201902
201903 201903
201904 201901
201904 201902
201904 201903
201904 201904
201905 201901 GROUPC JOINCO
------ ------
201905 201902
201905 201903
201905 201904
201905 201905
201906 201901
201906 201902
201906 201903
201906 201904
201906 201905
201906 201906
201907 201901 GROUPC JOINCO
------ ------
201907 201902
201907 201903
201907 201904
201907 201905
201907 201906
201907 201907
201908 201901
201908 201902
201908 201903
201908 201904
201908 201905 GROUPC JOINCO
------ ------
201908 201906
201908 201907
201908 201908 已选择 36 行。

  

3. 修改原来SQL中关于日期的不等值关联,可以起到走hash的作用。

关于Oracle中Sort Merge Join的改写的更多相关文章

  1. Oracle 三种连接方式 NESTED LOOP HASH JOIN SORT MERGE JOIN

    NESTED LOOP: 对于被连接的数据子集较小的情况,嵌套循环连接是个较好的选择.在嵌套循环中,内表被外表驱动,外表返回的每一行都要在内表中检索找到与它匹配的行,因此整个查询返回的结果集不能太大( ...

  2. Oracle 表的连接方式(1)-----Nested loop join和 Sort merge join

    关系数据库技术的精髓就是通过关系表进行规范化的数据存储,并通过各种表连接技术和各种类型的索引技术来进行信息的检索和处理. 表的三种关联方式: nested loop:从A表抽一条记录,遍历B表查找匹配 ...

  3. Nested loops、Hash join、Sort merge join(三种连接类型原理、使用要点)

    nested loop 嵌套循环(原理):oracle从较小结果集(驱动表.也可以被称为outer)中读取一行,然后和较大结果集(被侦查表,也可以叫做inner)中的所有数据逐条进行比较(也是等值连接 ...

  4. Sort merge join、Nested loops、Hash join(三种连接类型)

    目前为止,典型的连接类型有3种: Sort merge join(SMJ排序-合并连接):首先生产driving table需要的数据,然后对这些数据按照连接操作关联列进行排序:然后生产probed ...

  5. Nested Loop,Sort Merge Join,Hash Join

    三种连接工作方式比较: Nested loops 工作方式是从一张表中读取数据,访问另一张表(通常是索引)来做匹配,nested loops适用的场合是当一个关联表比较小的时候,效率会更高. Merg ...

  6. Nested Loops,Hash Join 和 Sort Merge Join. 三种不同连接的不同:

    原文:https://blog.csdn.net/tianlesoftware/article/details/5826546 Nested Loops,Hash Join 和 Sort Merge ...

  7. NESTED LOOPS & HASH JOIN & SORT MERGE JOIN

    表连接方式及使用场合 NESTED LOOP 对于被连接的数据子集较小的情况,nested loop连接是个较好的选择.nested loop就是扫描一个表,每读到一条记录,就根据索引去另一个表里面查 ...

  8. 三大表连接方式详解之Nested loop join和 Sort merge join

    在早期版本,Oracle提供的是nested-loop join,两表连接就相当于二重循环,假定两表分别有m行和n行       如果内循环是全表扫描,时间复杂度就是O(m*n)       如果内循 ...

  9. 排序合并连接(sort merge join)的原理

    排序合并连接(sort merge join)的原理 排序合并连接(sort merge join)的原理     排序合并连接(sort merge join)       访问次数:两张表都只会访 ...

随机推荐

  1. 超強的Linux指令解釋網站《explainshell.com》,學Linux必備!

    ExplainShell 官方網站:http://explainshell.com/ 原始碼下載:https://github.com/idank/explainshell 用瀏覽器打該explain ...

  2. Mybatis+Springmvc+Spring整合常用的配置文件

    1.创建web项目 2.导入mabatis spring springnvc 需要的jar包 3.创建mybatis,spring,springmvc的配置文件 (1)web.xml配置文件 < ...

  3. find和grep技巧

    1. find ./ -name "*streaming*"  查找文件 2.  grep -r KUBE_LOGTOSTDERR /etc/kubernetes/*  查找内容

  4. shell脚本编程测试类型上

    一bash的条件测试 判断某需求是否满足,需要由测试机制来实现.专用的测试表达式需要由测试命令辅助完成测试过程. 评估布尔声明,以便用在条件性执行中.若真,则返回0:若假,则返回1. 测试命令:• t ...

  5. 阿里云入选Gartner 2019 WAF魔力象限,唯一亚太厂商!

    近期,在全球权威咨询机构Gartner发布的2019 Web应用防火墙魔力象限中,阿里云Web应用防火墙成功入围,是亚太地区唯一一家进入该魔力象限的厂商! Web应用防火墙,简称WAF.在保护Web应 ...

  6. .net从服务端下载文件(可以断点续传)

    public void DownFile(string guid) { var fileTransfer = new FileTransfer(); var directoryPath = Path. ...

  7. Qt事件学习

    一.创建Qt gui应用对应的源码: 点击(此处)折叠或打开 //mylineedit.h #ifndef MYLINEEDIT_H #define MYLINEEDIT_H #include < ...

  8. mybatis中一对多查询collection关联不执行

    今天遇到的原因是因为下面红底id没有,导致关联查询没有条件(id字段没传),所以一直没有执行. <?xml version="1.0" encoding="UTF- ...

  9. SCP-Py-001

    项目编号:Py-001 项目等级:Euclid 特殊收容措施: Py-001必须被存储于基金会主站的网络硬盘中,并切断一切与互联网的连接. Py-001突破收容在网络上传播后,一旦在一台计算机上被下载 ...

  10. electron-vue中点击按钮,实现打开程序目录里面的某个文件

    设计到的知识点: explorer.exe /select 打开文件夹并把焦点放到指定文件 nodejs中的process模块--child_process.exec 我这里是根据需求,点击按钮后打开 ...