解析数仓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 ...
随机推荐
- 如何在网上找MySQL数据库的JDBC驱动jar包?
当我们在开发程序,涉及数据库时,总是需要用到相应的jar包,这不小编就给大家介绍一下如何下载相应的jar包 方法/步骤 1 在百度搜索栏上搜索MySQL 2 选择Downloads 3 选择 Co ...
- 如何获取 topic 主题的列表?
bin/kafka-topics.sh --list --zookeeper localhost:2181
- 用 wait-notify 写一段代码来解决生产者-消费者问题?(答案)
请参考答案中的示例代码.只要记住在同步块中调用 wait() 和 notify()方法,如果阻塞,通过循环来测试等待条件.
- java程序如何确保多线程的运行安全?
线程的安全问题体现在: 原子性:一个或多个操作在CPU执行过程中不被中断的特性 可见性:一个线程对共享变量的修改,另一个线程能立刻看到 有序性:程序执行的顺序按照代码的先后顺序执行 导致线程存在安全问 ...
- Zookeeper 的典型应用场景?
Zookeeper 是一个典型的发布/订阅模式的分布式数据管理与协调框架,开发人员 可以使用它来进行分布式数据的发布和订阅. 通过对 Zookeeper 中丰富的数据节点进行交叉使用,配合 Watch ...
- centos安装服务参考博客,亲测可用
centos 安装nginx参考 日志log报错 nginx -c /etc/nginx/nginx.conf https://blog.csdn.net/weixin_41004350/articl ...
- 汽车中的V流程开发
各步骤的简介各步骤的简介 (1)Control Design and offline Simulation:算法模型构建和离线仿真(基于模型的设计).算法工程师用Matlab模型实现算法:并实施离线仿 ...
- 12_非线性理论基础_Lyapunov直接方法
- python-使用函数求特殊a串数列和
给定两个均不超过9的正整数a和n,要求编写函数fn(a,n) 求a+aa+aaa++⋯+aa⋯aa(n个a)之和,fn须返回的是数列和 函数接口定义: 1 fn(a,n) 2 其中 a 和 n 都是用 ...
- rabitmq 登录报错:User can only log in via localhost
安装教程参考:https://blog.csdn.net/qq_43672652/article/details/107349063 修改了配置文件仍然报错,无法登录.解决办法:新建一个用户登录: 查 ...