Download presentation
Presentation is loading. Please wait.
Published byramaic Αργυριάδης Modified over 6 years ago
1
Rollups, Cubes, Grouping Sets and their inner working
All About Grouping January 2, 2019 Rollups, Cubes, Grouping Sets and their inner working Rob van Wijk
2
Who am I Database application developer 14 years with Oracle products
Rob van Wijk Database application developer 14 years with Oracle products Utrecht January 2, 2019
3
All About Grouping Introduction GROUPING SETS ROLLUP CUBE
Topics Introduction GROUPING SETS ROLLUP CUBE Combining and calculating Supporting functions Inner working January 2, 2019
4
All About Grouping Introduction aog1.sql January 2, 2019
5
All About Grouping GROUP BY expr1, …, exprn ≡ GROUP BY GROUPING SETS
aog2.sql January 2, 2019
6
All About Grouping GROUP BY GROUPING SETS ( set1, …, setx ) ≡
GROUP BY set1 UNION ALL … GROUP BY setx aog3.sql January 2, 2019
7
All About Grouping GROUP BY ROLLUP ( set1, …, setn ) ≡
GROUP BY GROUPING SETS ( (set1 U … U setn), (set1 U … U setn-1), …, set1, () ) January 2, 2019
8
leads to N+1 GROUPING SETS
All About Grouping ROLLUP (2) ROLLUP (set1, …, setN) with N ≥ 1 leads to N+1 GROUPING SETS January 2, 2019
9
All About Grouping Example:
ROLLUP (3) Example: GROUP BY ROLLUP ( (deptno), (job,mgr), (empno) ) ≡ GROUP BY GROUPING SETS ( (deptno,job,mgr,empno) , (deptno,job,mgr) , (deptno) , () ) aog4.sql January 2, 2019
10
All About Grouping GROUP BY CUBE ( set1, …, setn ) ≡
GROUP BY GROUPING SETS (all possible combinations between (set1 U … U setn) and () ) January 2, 2019
11
leads to 2N GROUPING SETS
All About Grouping CUBE (2) CUBE (set1, …, setN) with N ≥ 1 leads to 2N GROUPING SETS January 2, 2019
12
Follows Pascal’s triangle
All About Grouping CUBE (3) Follows Pascal’s triangle 0 sets X 1 set 2 sets 3 sets 4 sets January 2, 2019
13
All About Grouping Example:
CUBE (4) Example: GROUP BY CUBE ( (deptno), (job,mgr), (empno) ) ≡ GROUP BY GROUPING SETS ( (deptno,job,mgr,empno) , (deptno,job,mgr), (deptno,empno), (job,mgr,empno) , (deptno), (job,mgr), (empno) , () ) aog5.sql January 2, 2019
14
GROUP BY deptno, ROLLUP(empno)
All About Grouping Combining and calculating (1) GROUP BY deptno, ROLLUP(empno) ? January 2, 2019
15
All About Grouping GROUP BY deptno, ROLLUP(empno) ≡
Combining and calculating (2) GROUP BY deptno, ROLLUP(empno) ≡ GROUP BY GROUPING SETS (deptno) , GROUPING SETS ( empno, () ) January 2, 2019
16
All About Grouping Cartesian product !
Combining and calculating (3) Cartesian product ! GROUP BY GROUPING SETS ( set1 , …, setX ) , GROUPING SETS ( setX+1 , …, setX+Y ) ≡ GROUP BY GROUPING SETS (set1 U setX+1 , …,set1 U setX+Y , … , setX U setX+1 , …,setX U setX+Y ) January 2, 2019
17
All About Grouping GROUP BY deptno, ROLLUP(empno) ≡
Combining and calculating (4) GROUP BY deptno, ROLLUP(empno) ≡ GROUP BY GROUPING SETS (deptno) , GROUPING SETS ( (empno), () ) GROUP BY GROUPING SETS ( (deptno,empno), (deptno) ) aog6.sql January 2, 2019
18
All About Grouping Question:
Combining and calculating (5) Question: How many grouping sets does the clause below yield? GROUP BY ROLLUP(deptno,job) , CUBE(mgr,hiredate) Answer: 3 * 4 = 12 aog7.sql January 2, 2019
19
GROUP BY deptno, ename, CUBE(empno,job,mgr)
All About Grouping Combining and calculating (6) GROUP BY deptno , ROLLUP(empno) , CUBE(job,mgr) , GROUPING SETS (ename) ≡ GROUP BY deptno, ename, CUBE(empno,job,mgr) aog7a.sql January 2, 2019
20
All About Grouping GROUPING GROUPING_ID GROUP_ID aog8.sql
Supporting functions GROUPING GROUPING_ID GROUP_ID aog8.sql January 2, 2019
21
All About Grouping SORT GROUP BY versus HASH GROUP BY Inner working
January 2, 2019
22
+ + All About Grouping aog9.sql Inner working: ROLLUP (deptno,empno)
incoming set grouping set ( (deptno,empno) ) SORT GROUP BY 10 7782 2450 7839 5000 7934 1300 20 7369 800 7566 2975 7788 3000 7876 1100 7902 30 7499 1600 7521 1250 7654 7698 2850 7844 1500 7900 950 grouping set ( (deptno) ) + 10 NULL 8750 20 NULL 10875 30 NULL 9400 SORT GROUP BY grouping set ( () ) + NULL 29025 SORT GROUP BY aog9.sql January 2, 2019
23
All About Grouping aog10.sql Inner working: CUBE(deptno,job)
incoming set 14 rows grouping set (deptno,job) SORT GROUP BY 9 rows 36 rows GENERATE CUBE 18 rows SORT GROUP BY aog10.sql January 2, 2019
24
All About Grouping temporary input table SYS_TEMP_... temporary
Inner working: GROUPING SETS (1) LOAD AS SELECT (into input table) TABLE ACCESS FULL (EMP) temporary input table SYS_TEMP_... LOAD AS SELECT (into outputtable) HASH GROUP BY TABLE ACCESS FULL (input table) iterate as much times as there are grouping sets temporary output table SYS_TEMP_... TEMP TABLE TRANSFORMATION VIEW TABLE ACCESS FULL (output table) aog11.sql January 2, 2019
25
Optimize towards a ROLLUP or CUBE execution,
All About Grouping Inner working: GROUPING SETS (2) Optimize towards a ROLLUP or CUBE execution, if possible? aog12.sql January 2, 2019
26
All About Grouping Questions? January 2, 2019
27
Thanks for your attention! Blog: http://rwijk.blogspot.com
All About Grouping Thanks for your attention! Blog: January 2, 2019
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.