Presentation is loading. Please wait.

Presentation is loading. Please wait.

Rollups, Cubes, Grouping Sets and their inner working

Similar presentations


Presentation on theme: "Rollups, Cubes, Grouping Sets and their inner working"— Presentation transcript:

1 Rollups, Cubes, Grouping Sets and their inner working
All About Grouping November 23, 2018 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 November 23, 2018

3 All About Grouping Introduction GROUPING SETS ROLLUP CUBE
Topics Introduction GROUPING SETS ROLLUP CUBE Combining and calculating Supporting functions Inner working November 23, 2018

4 All About Grouping Introduction aog1.sql November 23, 2018

5 All About Grouping GROUP BY expr1, …, exprn ≡ GROUP BY GROUPING SETS
aog2.sql November 23, 2018

6 ( (expr11, …, expr1n), …, (exprx1, …, exprxm) )
All About Grouping GROUPING SETS (2) GROUP BY GROUPING SETS ( (expr11, …, expr1n), …, (exprx1, …, exprxm) ) GROUP BY expr11, … expr1n UNION ALL GROUP BY exprx1, …, exprxm aog3.sql November 23, 2018

7 All About Grouping GROUP BY ROLLUP ( set1, …, setn ) ≡
GROUP BY GROUPING SETS ( (set1, …, setn), (set1, …, setn-1), …, set1, () ) November 23, 2018

8 leads to N+1 GROUPING SETS
All About Grouping ROLLUP (2) ROLLUP (set1, …, setN) with N ≥ 1 leads to N+1 GROUPING SETS November 23, 2018

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 November 23, 2018

10 All About Grouping GROUP BY CUBE ( set1, …, setn ) ≡
GROUP BY GROUPING SETS (all possible combinations between () and (set1, …, setn) ) November 23, 2018

11 leads to 2N GROUPING SETS
All About Grouping CUBE (2) CUBE (set1, …, setN) with N ≥ 1 leads to 2N GROUPING SETS November 23, 2018

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 November 23, 2018

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 November 23, 2018

14 GROUP BY deptno, ROLLUP(empno)
All About Grouping Combining and calculating (1) GROUP BY deptno, ROLLUP(empno) ? November 23, 2018

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, () ) November 23, 2018

16 All About Grouping Cartesian product ! GROUP BY deptno, ROLLUP(empno)
Combining and calculating (3) Cartesian product ! GROUP BY deptno, ROLLUP(empno) GROUP BY GROUPING SETS (deptno) , GROUPING SETS ( (empno), () ) GROUP BY GROUPING SETS ( (deptno,empno), (deptno) ) aog6.sql November 23, 2018

17 All About Grouping Question:
Combining and calculating (4) Question: How many grouping sets does the clause below yield? GROUP BY ROLLUP(deptno,job) , CUBE(mgr,hiredate) Answer: 3 * 4 = 12 aog7.sql November 23, 2018

18 All About Grouping GROUPING GROUPING_ID GROUP_ID aog8.sql
Supporting functions GROUPING GROUPING_ID GROUP_ID aog8.sql November 23, 2018

19 All About Grouping SORT GROUP BY versus HASH GROUP BY Inner working
November 23, 2018

20 + + 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 November 23, 2018

21 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 November 23, 2018

22 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 November 23, 2018

23 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 November 23, 2018

24 All About Grouping Questions? November 23, 2018

25 Thanks for your attention! Blog: http://rwijk.blogspot.com
All About Grouping Thanks for your attention! Blog: November 23, 2018


Download ppt "Rollups, Cubes, Grouping Sets and their inner working"

Similar presentations


Ads by Google