Alles over groeperen Rollups, Cubes, Grouping Sets en hun interne werking Rob van Wijk 22 september...
-
Upload
jose-milson -
Category
Documents
-
view
215 -
download
0
Transcript of Alles over groeperen Rollups, Cubes, Grouping Sets en hun interne werking Rob van Wijk 22 september...
Alles over groeperen
Rollups, Cubes, Grouping Sets en hun interne werking
Rob van Wijk
11 april 2023
Wie ben ikRob van Wijk
11 april 2023
Oracle
ontwikkelaar
Utrecht
Alles Over Groeperen
• Inleiding• GROUPING SETS• ROLLUP• CUBE• Combineren / rekenen• Ondersteunende functies• Interne werking
Onderwerpen
11 april 2023
Alles Over GroeperenInleiding
11 april 2023
aog1.sql
Alles Over Groeperen
GROUP BY col1, …, coln
≡
GROUP BY GROUPING SETS
( (col1, …, coln) )
GROUPING SETS (1)
11 april 2023
aog2.sql
Alles Over Groeperen
GROUP BY GROUPING SETS
( (col11, …, col1n), …, (colx1, …, colxm) )
≡
GROUP BY col11, … col1n
UNION ALL
…
UNION ALL
GROUP BY colx1, …, colxm
GROUPING SETS (2)
11 april 2023
aog3.sql
Alles Over Groeperen
GROUP BY ROLLUP ( set1, …, setn )
≡
GROUP BY GROUPING SETS
( (set1, …, setn), (set1, …, setn-1), …, set1, () )
ROLLUP (1)
11 april 2023
Alles Over Groeperen
ROLLUP (set1, …, setN)
met N ≥ 1
leidt tot N+1 GROUPING SETS
ROLLUP (2)
11 april 2023
Alles Over Groeperen
Voorbeeld:
GROUP BY ROLLUP ( (deptno), (job,mgr), (empno) )
≡
GROUP BY GROUPING SETS
( (deptno,job,mgr,empno)
, (deptno,job,mgr)
, (deptno)
, () )
ROLLUP (3)
11 april 2023
aog4.sql
Alles Over Groeperen
GROUP BY CUBE ( set1, …, setn )
≡
GROUP BY GROUPING SETS
(alle mogelijke combinaties tussen () en (set1, …, setn) )
CUBE (1)
11 april 2023
Alles Over Groeperen
CUBE (set1, …, setN)
met N ≥ 1
leidt tot 2N GROUPING SETS
CUBE (2)
11 april 2023
Alles Over GroeperenCUBE (3)
11 april 2023
0 sets X
1 set
2 sets
3 sets
4 sets
Volgens de driehoek van Pascal
Alles Over Groeperen
Voorbeeld:
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)
11 april 2023
aog5.sql
Alles Over Groeperen
GROUP BY deptno, ROLLUP(empno)
?
Combineren / rekenen (1)
April 11, 2023
Alles Over Groeperen
GROUP BY deptno, ROLLUP(empno)
≡
GROUP BY GROUPING SETS (deptno)
, GROUPING SETS ( empno, () )
Combineren / rekenen (2)
April 11, 2023
Alles Over Groeperen
Cartesisch product !
GROUP BY deptno, ROLLUP(empno)
≡
GROUP BY GROUPING SETS (deptno)
, GROUPING SETS ( (empno), () )
≡
GROUP BY GROUPING SETS
( (deptno,empno), (deptno) )
Combineren / rekenen (3)
April 11, 2023
aog6.sql
Alles Over Groeperen
Vraag:
Hoeveel grouping sets levert dit op?
GROUP BY ROLLUP(deptno,job)
, CUBE(mgr,hiredate)
Antwoord: 3 * 4 = 12
Combineren / rekenen (4)
April 11, 2023
aog7.sql
Alles Over Groeperen
GROUPING
GROUPING_ID
GROUP_ID
Ondersteunende functies
April 11, 2023
aog8.sql
Alles Over Groeperen
SORT GROUP BY
versus
HASH GROUP BY
Interne werking
April 11, 2023
Alles Over GroeperenInterne werking: ROLLUP (deptno,empno)
April 11, 2023
1077822450
1078395000
1079341300
207369800
2075662975
2077883000
2078761100
2079023000
3074991600
3075211250
3076541250
3076982850
3078441500
307900950
10NULL8750
20NULL10875
30NULL9400+
+NULLNULL29025
inkomende verzameling
grouping set ( (deptno,empno) )
grouping set ( () )
grouping set ( (deptno) )
SORT GROUP BY
SORT GROUP BY
SORT GROUP BY
aog9.sql
Alles Over GroeperenInterne werking: CUBE(deptno,job)
April 11, 2023
SORT GROUP BY
GENERATE CUBE
SORT GROUP BY
inkomende verzameling
grouping set (deptno,job)
14 rijen
9 rijen
36
rijen
18 rijen
aog10.sql
Alles Over GroeperenInterne werking: GROUPING SETS (1)
11 april 2023
LOAD AS SELECT (into input table)
TABLE ACCESS FULL (EMP)
TEMP TABLE TRANSFORMATION
VIEW
TABLE ACCESS FULL (output table)
tijdelijkeinputtabel
SYS_TEMP_...
tijdelijkeoutputtabel
SYS_TEMP_...
LOAD AS SELECT (into outputtabel)
HASH GROUP BY
TABLE ACCESS FULL (input tabel)
itereer zo vaak als dat
er grouping sets zijn
aog11.sql
Alles Over Groeperen
Optimalisatie naar ROLLUP of CUBE executie
indien mogelijk ?
Interne werking: GROUPING SETS (2)
11 april 2023
aog12.sql
Alles Over GroeperenVragen?
11 april 2023