all about grouping

25
All About Grouping Rollups, Cubes, Grouping Sets and their inner working Rob van Wijk July 3, 2022

Upload: sarah-owens

Post on 04-Jan-2016

31 views

Category:

Documents


0 download

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 Presentation

TRANSCRIPT

Page 1: All About Grouping

All About Grouping

Rollups, Cubes, Grouping Sets and their inner working

Rob van Wijk

April 20, 2023

Page 2: All About Grouping

Who am IRob van Wijk

April 20, 2023

• 14 years with Oracle products

Utrecht

• Database application developer

Page 3: All About Grouping

All About Grouping

• Introduction• GROUPING SETS• ROLLUP• CUBE• Combining and calculating• Supporting functions• Inner working

Topics

April 20, 2023

Page 4: All About Grouping

All About GroupingIntroduction

April 20, 2023

aog1.sql

Page 5: All About Grouping

All About Grouping

GROUP BY expr1, …, exprn

GROUP BY GROUPING SETS

( (expr1, …, exprn) )

GROUPING SETS (1)

April 20, 2023

aog2.sql

Page 6: All About Grouping

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

Page 7: All About Grouping

All About Grouping

GROUP BY ROLLUP ( set1, …, setn )

GROUP BY GROUPING SETS

( (set1, …, setn), (set1, …, setn-1), …, set1, () )

ROLLUP (1)

April 20, 2023

Page 8: All About Grouping

All About Grouping

ROLLUP (set1, …, setN)

with N ≥ 1

leads to N+1 GROUPING SETS

ROLLUP (2)

April 20, 2023

Page 9: All About Grouping

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

Page 10: All About Grouping

All About Grouping

GROUP BY CUBE ( set1, …, setn )

GROUP BY GROUPING SETS

(all possible combinations between () and (set1, …, setn) )

CUBE (1)

April 20, 2023

Page 11: All About Grouping

All About Grouping

CUBE (set1, …, setN)

with N ≥ 1

leads to 2N GROUPING SETS

CUBE (2)

April 20, 2023

Page 12: All About Grouping

All About GroupingCUBE (3)

April 20, 2023

0 sets X

1 set

2 sets

3 sets

4 sets

Follows Pascal’s triangle

Page 13: All About Grouping

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

Page 14: All About Grouping

All About Grouping

GROUP BY deptno, ROLLUP(empno)

?

Combining and calculating (1)

April 20, 2023

Page 15: All About Grouping

All About Grouping

GROUP BY deptno, ROLLUP(empno)

GROUP BY GROUPING SETS (deptno)

, GROUPING SETS ( empno, () )

Combining and calculating (2)

April 20, 2023

Page 16: All About Grouping

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

Page 17: All About Grouping

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

Page 18: All About Grouping

All About Grouping

GROUPING

GROUPING_ID

GROUP_ID

Supporting functions

April 20, 2023

aog8.sql

Page 19: All About Grouping

All About Grouping

SORT GROUP BY

versus

HASH GROUP BY

Inner working

April 20, 2023

Page 20: All About Grouping

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

Page 21: All About Grouping

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

Page 22: All About Grouping

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

Page 23: All About Grouping

All About Grouping

Optimize towards a ROLLUP or CUBE execution,

if possible?

Inner working: GROUPING SETS (2)

April 20, 2023

aog12.sql

Page 24: All About Grouping

All About GroupingQuestions?

April 20, 2023

Page 25: All About Grouping

All About Grouping

April 20, 2023

Thanks for your attention!

Email: [email protected]

Blog: http://rwijk.blogspot.com