关于Oracle中Sort Merge Join的改写
业务场景的问题,我们有一个刷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的改写的更多相关文章
- Oracle 三种连接方式 NESTED LOOP HASH JOIN SORT MERGE JOIN
NESTED LOOP: 对于被连接的数据子集较小的情况,嵌套循环连接是个较好的选择.在嵌套循环中,内表被外表驱动,外表返回的每一行都要在内表中检索找到与它匹配的行,因此整个查询返回的结果集不能太大( ...
- Oracle 表的连接方式(1)-----Nested loop join和 Sort merge join
关系数据库技术的精髓就是通过关系表进行规范化的数据存储,并通过各种表连接技术和各种类型的索引技术来进行信息的检索和处理. 表的三种关联方式: nested loop:从A表抽一条记录,遍历B表查找匹配 ...
- Nested loops、Hash join、Sort merge join(三种连接类型原理、使用要点)
nested loop 嵌套循环(原理):oracle从较小结果集(驱动表.也可以被称为outer)中读取一行,然后和较大结果集(被侦查表,也可以叫做inner)中的所有数据逐条进行比较(也是等值连接 ...
- Sort merge join、Nested loops、Hash join(三种连接类型)
目前为止,典型的连接类型有3种: Sort merge join(SMJ排序-合并连接):首先生产driving table需要的数据,然后对这些数据按照连接操作关联列进行排序:然后生产probed ...
- Nested Loop,Sort Merge Join,Hash Join
三种连接工作方式比较: Nested loops 工作方式是从一张表中读取数据,访问另一张表(通常是索引)来做匹配,nested loops适用的场合是当一个关联表比较小的时候,效率会更高. Merg ...
- Nested Loops,Hash Join 和 Sort Merge Join. 三种不同连接的不同:
原文:https://blog.csdn.net/tianlesoftware/article/details/5826546 Nested Loops,Hash Join 和 Sort Merge ...
- NESTED LOOPS & HASH JOIN & SORT MERGE JOIN
表连接方式及使用场合 NESTED LOOP 对于被连接的数据子集较小的情况,nested loop连接是个较好的选择.nested loop就是扫描一个表,每读到一条记录,就根据索引去另一个表里面查 ...
- 三大表连接方式详解之Nested loop join和 Sort merge join
在早期版本,Oracle提供的是nested-loop join,两表连接就相当于二重循环,假定两表分别有m行和n行 如果内循环是全表扫描,时间复杂度就是O(m*n) 如果内循 ...
- 排序合并连接(sort merge join)的原理
排序合并连接(sort merge join)的原理 排序合并连接(sort merge join)的原理 排序合并连接(sort merge join) 访问次数:两张表都只会访 ...
随机推荐
- 这是什么b
用table表格标签渲染总排名和总分数据 <!DOCTYPE html> <html lang="zh"> <head> <meta ch ...
- Java内存模型之happens-before原则
我们无法就所有场景来规定某个线程修改的变量何时对其他线程可见,但是我们可以指定某些规则,这规则就是happens-before,从JDK 5 开始,JMM就使用happens-before的概念来阐述 ...
- 修改bug 提交出错:操作失败: 无法更改关系,因为一个或多个外键属性不可以为 null
提交出错:操作失败: 无法更改关系,因为一个或多个外键属性不可以为 null.对关系作出更改后,会将相关的外键属性设置为 null 值.如果外键不支持 null 值,则必须定义新的关系,必须向外键属性 ...
- elasticsearch Java Client用户指南
这里使用的Java客户端版本是5.1.2,Elasticsearch的版本号也要是5.1.2,否则一些功能可能不支持. 之前介绍过Spring Data Elasticsearch,那里也是使用了本文 ...
- 深入学习Redis持久化
一.Redis高可用概述 在介绍Redis高可用之前,先说明一下在Redis的语境中高可用的含义. 我们知道,在web服务器中,高可用是指服务器可以正常访问的时间,衡量的标准是在多长时间内可以提供正常 ...
- showfont - 展示当前"显示屏-字体 映射"中的所有字符.
总览 showfont 描述 showfont 利用8-bit控制台模式的 Application Charset Map(应用字符集映射) 中一些连续的开关, 以当前字体在屏幕上展示所有的256个或 ...
- python 读取文件时,精确的保留数据
import numpy as npimport pandas as pd point = pd.read_csv('waypoint21.csv') with pd.option_context(' ...
- #2560异或和问题 jdfz集训—秦岳
题目描述 N个数字,要求选择M次,每次从N个数中选出两个数(Ai,Aj)(但不能和之前某次选择相同),此次选择的得分为Ai xor Aj. 求最大得分. 输入格式 第一行包含两个整数N,M 接下来一行 ...
- Python3.5-20190516-廖老师-自我笔记-匿名函数-装饰器
当函数很简单的时候采用匿名函数很方便.
- java基础学习笔记四(异常)
Java中的异常 Exception 如图可以看出所有的异常跟错误都继承与Throwable类,也就是说所有的异常都是一个对象. 从大体来分异常为两块: 1.error---错误 : 是指程序无法处理 ...