解析数仓OLAP函数:ROLLUP、CUBE、GROUPING SETS
摘要:GaussDB(DWS) ROLLUP,CUBE,GROUPING SETS等OLAP函数的原理解析。
本文分享自华为云社区《GaussDB(DWS) OLAP函数浅析》,作者: DWS_Jack_2。
在一些报表场景中,经常会对数据做分组统计(group by),例如对一级部门下辖的二级部门员工数进行统计:
create table emp(
id int, --工号
name text, --员工名
dep_1 text, --一级部门
dep_2 text --二级部门
);
gaussdb=# select count(*), dep_2 from emp group by dep_2;
count | dep_2
-------+-------
200 | SRE
100 | EI
(2 rows)
常见的统计报表业务中,通常需要进一步计算一级部门的“合计”人数,也就是二级部门各分组的累加,就可以借助于rollup,如下所示,比前面的分组计算结果多了一行合计的数据:
gaussdb=# select count(*), dep_2 from emp group by rollup(dep_2);
count | dep_2
-------+-------
200 | SRE
100 | EI
300 |
(3 rows)
如上是一种group by扩展的高级分组函数使用场景,这一类分组函数统称为OLAP函数,在GaussDB(DWS)中支持 ROLLUP,CUBE,GROUPING SETS,下面对这几种OLAP函数的原理和应用场景做一下分析。
首先我们来创建一张表,customer,用户信息表,其中包含了用户id,用户名,年龄,国家,用户级别,性别,余额等信息:
create table customer
(
c_id char(16) not null,
c_name char(20) ,
c_age integer ,
c_country varchar(20) ,
c_class char(10),
c_sex text,
c_balance numeric
);
insert into customer values(1, 'tom', '20', 'China', '1', 'male', 300);
insert into customer values(2, 'jack', '30', 'USA', '1', 'male', 100);
insert into customer values(3, 'rose', '40', 'UK', '1', 'female', 200);
insert into customer values(4, 'Frank', '60', 'GER', '1', 'male', 100);
insert into customer values(5, 'Leon', '20', 'China', '2', 'male', 200);
insert into customer values(6, 'Lucy', '20', 'China', '1', 'female', 500);
ROLLUP
本文开头的示例已经解释了,ROLLUP是在分组计算基础上增加了合计,从字面意思理解,就是从最小聚合级开始,聚合单位逐渐扩大,例如如下语句:
select c_country, c_class, sum(c_balance) from customer group by rollup(c_country, c_class) order by 1,2,3;
c_country | c_class | sum
-----------+------------+------
China | 1 | 800
China | 2 | 200
China | | 1000
GER | 1 | 100
GER | | 100
UK | 1 | 200
UK | | 200
USA | 1 | 100
USA | | 100
| | 1400
(10 rows)
该语句功能等价于如下:
select c_country, c_class, sum(c_balance) from customer group by c_country, c_class
union all
select c_country, null, sum(c_balance) from customer group by c_country
union all
select null, null, sum(c_balance) from customer order by 1,2,3;
c_country | c_class | sum
-----------+------------+------
China | 1 | 800
China | 2 | 200
China | | 1000
GER | 1 | 100
GER | | 100
UK | 1 | 200
UK | | 200
USA | 1 | 100
USA | | 100
| | 1400
(10 rows)
尝试理解一下
GROUP BY ROLLUP(A,B):
首先对(A,B)进行GROUP BY,然后对(A)进行GROUP BY,最后对全表进行GROUP BY操作
CUBE
CUBE从字面意思理解,就是各个维度的意思,也就是说全部组合,即聚合键中所有字段的组合的分组统计结果,例如如下语句:
select c_country, c_class, sum(c_balance) from customer group by cube(c_country, c_class) order by 1,2,3;
c_country | c_class | sum
-----------+------------+------
China | 1 | 800
China | 2 | 200
China | | 1000
GER | 1 | 100
GER | | 100
UK | 1 | 200
UK | | 200
USA | 1 | 100
USA | | 100
| 1 | 1200
| 2 | 200
| | 1400
(12 rows)
该语句功能等价于如下:
select c_country, c_class, sum(c_balance) from customer group by c_country, c_class
union all
select c_country, null, sum(c_balance) from customer group by c_country
union all
select null, null, sum(c_balance) from customer
union all
select NULL, c_class, sum(c_balance) from customer group by c_class order by 1,2,3;
c_country | c_class | sum
-----------+------------+------
China | 1 | 800
China | 2 | 200
China | | 1000
GER | 1 | 100
GER | | 100
UK | 1 | 200
UK | | 200
USA | 1 | 100
USA | | 100
| 1 | 1200
| 2 | 200
| | 1400
(12 rows)
理解一下
GROUP BY CUBE(A,B):
首先对(A,B)进行GROUP BY,然后依次对(A)、(B)进行GROUP BY,最后对全表进行GROUP BY操作。
GROUPING SETS
GROUPING SETS区别于ROLLUP和CUBE,并没有总体的合计功能,相当于从ROLLUP和CUBE的结果中提取出部分记录,例如如下语句:
select c_country, c_class, sum(c_balance) from customer group by grouping sets(c_country, c_class) order by 1,2,3;
c_country | c_class | sum
-----------+------------+------
China | | 1000
GER | | 100
UK | | 200
USA | | 100
| 1 | 1200
| 2 | 200
(6 rows)
该语句功能等价于如下:
select c_country, null, sum(c_balance) from customer group by c_country
union all
select null, c_class, sum(c_balance) from customer group by c_class
order by 1,2,3;
c_country | ?column? | sum
-----------+------------+------
China | | 1000
GER | | 100
UK | | 200
USA | | 100
| 1 | 1200
| 2 | 200
(6 rows)
理解一下
GROUP BY GROUPING SETS(A,B):
分别对(B)、(A)进行GROUP BY计算
目前在GaussDB(DWS)中,OLAP函数的实现,会有排序(sort)操作,相比等价的union all操作,效率并不会有提升,后续会通过mixagg的支持来提升OLAP函数的执行效率,有兴趣的同学,可以explain打印一下计划,来看一下OLAP函数的执行流程。
解析数仓OLAP函数:ROLLUP、CUBE、GROUPING SETS的更多相关文章
- [转]详解Oracle高级分组函数(ROLLUP, CUBE, GROUPING SETS)
原文地址:http://blog.csdn.net/u014558001/article/details/42387929 本文主要讲解 ROLLUP, CUBE, GROUPING SETS的主要用 ...
- 高级聚合函数rollup(),cube(),grouping sets()
rollup(),cube(),grouping sets() 上面这几个函数,是对group by分组功能做的功能扩展. a.rollup() 功能:在原结果基础上追加一行总合计记录 ...
- Oracle分析函数 — sum, rollup, cube, grouping用法
本文通过例子展示sum, rollup, cube, grouping的用法. //首先建score表 create table score( class nvarchar2(20), course ...
- GROUP BY中ROLLUP/CUBE/GROUPING/GROUPING SETS使用示例
oracle group by中rollup和cube的区别: Oracle的GROUP BY语句除了最基本的语法外,还支持ROLLUP和CUBE语句.CUBE ROLLUP 是用于统计数据的. 实验 ...
- GROUPING SETS、CUBE、ROLLUP
其实还是写一个Demo 比较好 USE tempdb IF OBJECT_ID( 'dbo.T1' , 'U' )IS NOT NULL BEGIN DROP TABLE dbo.T1; END; G ...
- hive grouping sets 等聚合函数
函数说明: grouping sets 在一个 group by 查询中,根据不同的维度组合进行聚合,等价于将不同维度的 group by 结果集进行 union allcube 根据 group b ...
- Hive高阶聚合函数 GROUPING SETS、Cube、Rollup
-- GROUPING SETS作为GROUP BY的子句,允许开发人员在GROUP BY语句后面指定多个统计选项,可以简单理解为多条group by语句通过union all把查询结果聚合起来结合起 ...
- Hive函数:GROUPING SETS,GROUPING__ID,CUBE,ROLLUP
参考:lxw大数据田地:http://lxw1234.com/archives/2015/04/193.htm 数据准备: CREATE EXTERNAL TABLE test_data ( mont ...
- Oracle的rollup、cube、grouping sets函数
转载自:https://blog.csdn.net/huang_xw/article/details/6402396 Oracle的group by除了基本用法以外,还有3种扩展用法,分别是rollu ...
随机推荐
- 关于“TypeError: Assignment to constant variable”的问题解决方案
在项目开发过程中,在使用变量声明时,如果不注意,可能会造成类型错误比如: Uncaught (in promise) TypeError: Assignment to constant variabl ...
- Dubbo 和 Spring Cloud 的区别?
根据微服务架构在各方面的要素,看看 Spring Cloud 和 Dubbo 都提供了哪些支 持. Dubbo Spring Cloud 服务注册中心 Zookeep er Spring Cloud ...
- memcached 和服务器的 local cache(比如 PHP 的 APC、 mmap 文件等)相比,有什么优缺点?
首先,local cache 有许多与上面(query cache)相同的问题.local cache 能够利 用的内存容量受到(单台)服务器空闲内存空间的限制.不过,local第 109 页 共 4 ...
- redis 使用详解
前戏: 又到了最喜欢的前戏部分,这个前戏可能有点长: Nosql和sql的区别 存储结构与mysql这一种关系型数据库完全不同,nosql存储的是KV形式 应用场景不同,sql支持关系复杂的数据查询, ...
- 学习 Haproxy (四)
一. haproxy 的安装配置 # cat /etc/redhat-release CentOS release 6.6 (Final) # uname -r 2.6.32-504.el6.i686 ...
- js技术之如何在JS中获取input的值
在JavaScript中获取input元素value的值: 方法一:var variations_number = $("#input的id名").val(); 1 <!DO ...
- 六个框架,一百多条检查项目,保证PCB设计不再出错
一.资料输入阶段1.在流程上接收到的资料是否齐全(包括:原理图.*.brd文件.料单.PCB设计说明以及PCB设计或更改要求.标准化要求说明.工艺设计说明文件)2.确认PCB模板是最新的3. 确认模板 ...
- 「入门篇」初识JVM (下下) - GC
垃圾收集主要是针对堆和方法区进行:程序计数器.虚拟机栈和本地方法栈这三个区域属于线程私有的,只存在于> 线程的生命周期内,线程结束之后也会消失,因此不需要对这三个区域进行垃圾回收. GC - J ...
- html 元素 强制不换行
html 中 nowrap是用来强制不换行的 在排版中 对包裹plain text的标签使用nowrap属性即刻实现强制不换行. 如:<p nowrap>强制不换行</p>&l ...
- ubantu14.04搜狗拼音安装
1. 先卸载fcitx: sudo apt-get purge fcitx*2. 安装fcitx和libssh2-1: sudo apt-get install fcitx 和 sudo apt-ge ...