all about grouping
DESCRIPTION
All About Grouping. October 29, 2014. Rollups, Cubes, Grouping Sets and their inner working. Rob van Wijk. Utrecht. Who am I. Rob van Wijk. Database application developer. 14 years with Oracle products. All About Grouping. Introduction GROUPING SETS ROLLUP CUBE - PowerPoint PPT PresentationTRANSCRIPT
All About Grouping
Rollups, Cubes, Grouping Sets and their inner working
Rob van Wijk
April 20, 2023
Who am IRob van Wijk
April 20, 2023
• 14 years with Oracle products
Utrecht
• Database application developer
All About Grouping
• Introduction• GROUPING SETS• ROLLUP• CUBE• Combining and calculating• Supporting functions• Inner working
Topics
April 20, 2023
All About GroupingIntroduction
April 20, 2023
aog1.sql
All About Grouping
GROUP BY expr1, …, exprn
≡
GROUP BY GROUPING SETS
( (expr1, …, exprn) )
GROUPING SETS (1)
April 20, 2023
aog2.sql
All About Grouping
GROUP BY GROUPING SETS
( (expr11, …, expr1n), …, (exprx1, …, exprxm) )
≡
GROUP BY expr11, … expr1n
UNION ALL
…
UNION ALL
GROUP BY exprx1, …, exprxm
GROUPING SETS (2)
April 20, 2023
aog3.sql
All About Grouping
GROUP BY ROLLUP ( set1, …, setn )
≡
GROUP BY GROUPING SETS
( (set1, …, setn), (set1, …, setn-1), …, set1, () )
ROLLUP (1)
April 20, 2023
All About Grouping
ROLLUP (set1, …, setN)
with N ≥ 1
leads to N+1 GROUPING SETS
ROLLUP (2)
April 20, 2023
All About Grouping
Example:
GROUP BY ROLLUP ( (deptno), (job,mgr), (empno) )
≡
GROUP BY GROUPING SETS
( (deptno,job,mgr,empno)
, (deptno,job,mgr)
, (deptno)
, () )
ROLLUP (3)
April 20, 2023
aog4.sql
All About Grouping
GROUP BY CUBE ( set1, …, setn )
≡
GROUP BY GROUPING SETS
(all possible combinations between () and (set1, …, setn) )
CUBE (1)
April 20, 2023
All About Grouping
CUBE (set1, …, setN)
with N ≥ 1
leads to 2N GROUPING SETS
CUBE (2)
April 20, 2023
All About GroupingCUBE (3)
April 20, 2023
0 sets X
1 set
2 sets
3 sets
4 sets
Follows Pascal’s triangle
All About Grouping
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)
, () )
CUBE (4)
April 20, 2023
aog5.sql
All About Grouping
GROUP BY deptno, ROLLUP(empno)
?
Combining and calculating (1)
April 20, 2023
All About Grouping
GROUP BY deptno, ROLLUP(empno)
≡
GROUP BY GROUPING SETS (deptno)
, GROUPING SETS ( empno, () )
Combining and calculating (2)
April 20, 2023
All About Grouping
Cartesian product !
GROUP BY deptno, ROLLUP(empno)
≡
GROUP BY GROUPING SETS (deptno)
, GROUPING SETS ( (empno), () )
≡
GROUP BY GROUPING SETS
( (deptno,empno), (deptno) )
Combining and calculating (3)
April 20, 2023
aog6.sql
All About Grouping
Question:
How many grouping sets does the clause below yield?
GROUP BY ROLLUP(deptno,job)
, CUBE(mgr,hiredate)
Answer: 3 * 4 = 12
Combining and calculating (4)
April 20, 2023
aog7.sql
All About Grouping
GROUPING
GROUPING_ID
GROUP_ID
Supporting functions
April 20, 2023
aog8.sql
All About Grouping
SORT GROUP BY
versus
HASH GROUP BY
Inner working
April 20, 2023
All About GroupingInner working: ROLLUP (deptno,empno)
April 20, 2023
1077822450
1078395000
1079341300
207369800
2075662975
2077883000
2078761100
2079023000
3074991600
3075211250
3076541250
3076982850
3078441500
307900950
10NULL8750
20NULL10875
30NULL9400+
+NULLNULL29025
incoming set
grouping set ( (deptno,empno) )
grouping set ( () )
grouping set ( (deptno) )
SORT GROUP BY
SORT GROUP BY
SORT GROUP BY
aog9.sql
All About GroupingInner working: CUBE(deptno,job)
April 20, 2023
SORT GROUP BY
GENERATE CUBE
SORT GROUP BY
incoming set
grouping set (deptno,job)
14 rows
9 rows
36
rows
18 rows
aog10.sql
All About GroupingInner working: GROUPING SETS (1)
April 20, 2023
LOAD AS SELECT (into input table)
TABLE ACCESS FULL (EMP)
TEMP TABLE TRANSFORMATION
VIEW
TABLE ACCESS FULL (output table)
temporaryinput table
SYS_TEMP_...
temporaryoutput 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
aog11.sql
All About Grouping
Optimize towards a ROLLUP or CUBE execution,
if possible?
Inner working: GROUPING SETS (2)
April 20, 2023
aog12.sql
All About GroupingQuestions?
April 20, 2023
All About Grouping
April 20, 2023
Thanks for your attention!
Email: [email protected]
Blog: http://rwijk.blogspot.com