Sorting & Aggregations Lecture # 11 Database Systems Andy - - PowerPoint PPT Presentation

sorting aggregations
SMART_READER_LITE
LIVE PREVIEW

Sorting & Aggregations Lecture # 11 Database Systems Andy - - PowerPoint PPT Presentation

Sorting & Aggregations Lecture # 11 Database Systems Andy Pavlo AP AP Computer Science 15-445/15-645 Carnegie Mellon Univ. Fall 2018 2 TO DAY'S AGEN DA Sorting Algorithms Aggregations CMU 15-445/645 (Fall 2018) 3 WH Y DO WE N


slide-1
SLIDE 1

Database Systems 15-445/15-645 Fall 2018 Andy Pavlo Computer Science Carnegie Mellon Univ.

AP AP

Lecture # 11

Sorting & Aggregations

slide-2
SLIDE 2

CMU 15-445/645 (Fall 2018)

TO DAY'S AGEN DA

Sorting Algorithms Aggregations

2

slide-3
SLIDE 3

CMU 15-445/645 (Fall 2018)

WH Y DO WE N EED TO SO RT?

Tuples in a table have no specific order But users often want to retrieve tuples in a specific

  • rder.

→ Trivial to support duplicate elimination (DISTINCT) → Bulk loading sorted tuples into a B+ tree index is faster → Aggregations (GROUP BY)

3

slide-4
SLIDE 4

CMU 15-445/645 (Fall 2018)

SO RTIN G ALGO RITH M S

If data fits in memory, then we can use a standard sorting algorithm like quick-sort. If data does not fit in memory, then we need to use a technique that is aware of the cost of writing data

  • ut to disk.

4

slide-5
SLIDE 5

CMU 15-445/645 (Fall 2018)

EXTERN AL M ERGE SO RT

Sorting Phase

→ Sort small chunks of data that fit in main-memory, and then write back the sorted data to a file on disk.

Merge Phase

→ Combine sorted sub-files into a single larger file.

5

slide-6
SLIDE 6

CMU 15-445/645 (Fall 2018)

OVERVIEW

We will start with a simple example of a 2-way external merge sort. Files are broken up into N pages. The DBMS has a finite number of B fixed-size buffers.

6

slide-7
SLIDE 7

CMU 15-445/645 (Fall 2018)

2- WAY EXTERN AL M ERGE SO RT

Pass #0

→ Reads every B pages of the table into memory → Sorts them, and writes them back to disk. → Each sorted set of pages is called a run.

Pass #1,2,3,…

→ Recursively merges pairs of runs into runs twice as long → Uses three buffer pages (2 for input pages, 1 for output)

7

Memory Disk

slide-8
SLIDE 8

CMU 15-445/645 (Fall 2018)

2- WAY EXTERN AL M ERGE SO RT

Pass #0

→ Reads every B pages of the table into memory → Sorts them, and writes them back to disk. → Each sorted set of pages is called a run.

Pass #1,2,3,…

→ Recursively merges pairs of runs into runs twice as long → Uses three buffer pages (2 for input pages, 1 for output)

7

Memory Memory Disk

slide-9
SLIDE 9

CMU 15-445/645 (Fall 2018)

2- WAY EXTERN AL M ERGE SO RT

Pass #0

→ Reads every B pages of the table into memory → Sorts them, and writes them back to disk. → Each sorted set of pages is called a run.

Pass #1,2,3,…

→ Recursively merges pairs of runs into runs twice as long → Uses three buffer pages (2 for input pages, 1 for output)

7

Memory Memory Disk

slide-10
SLIDE 10

CMU 15-445/645 (Fall 2018)

2- WAY EXTERN AL M ERGE SO RT

Pass #0

→ Reads every B pages of the table into memory → Sorts them, and writes them back to disk. → Each sorted set of pages is called a run.

Pass #1,2,3,…

→ Recursively merges pairs of runs into runs twice as long → Uses three buffer pages (2 for input pages, 1 for output)

7

Memory Memory Memory Disk

slide-11
SLIDE 11

CMU 15-445/645 (Fall 2018)

2- WAY EXTERN AL M ERGE SO RT

Pass #0

→ Reads every B pages of the table into memory → Sorts them, and writes them back to disk. → Each sorted set of pages is called a run.

Pass #1,2,3,…

→ Recursively merges pairs of runs into runs twice as long → Uses three buffer pages (2 for input pages, 1 for output)

7

Memory Memory Memory Disk

slide-12
SLIDE 12

CMU 15-445/645 (Fall 2018)

2- WAY EXTERN AL M ERGE SO RT

In each pass, we read and write each page in file. Number of passes = 1 + ⌈ log2 N ⌉ Total I/O cost = 2N ∙ (# of passes)

8

6,2 9,4 8,7 5,6 3,1 2 ∅ 3,4

EOF

slide-13
SLIDE 13

CMU 15-445/645 (Fall 2018)

2- WAY EXTERN AL M ERGE SO RT

In each pass, we read and write each page in file. Number of passes = 1 + ⌈ log2 N ⌉ Total I/O cost = 2N ∙ (# of passes)

8

1

  • PAGE

RUNS

PASS #0

3,4 2,6 4,9 7,8 5,6 1,3 2 ∅ 6,2 9,4 8,7 5,6 3,1 2 ∅ 3,4

EOF

slide-14
SLIDE 14

CMU 15-445/645 (Fall 2018)

2- WAY EXTERN AL M ERGE SO RT

In each pass, we read and write each page in file. Number of passes = 1 + ⌈ log2 N ⌉ Total I/O cost = 2N ∙ (# of passes)

8

1

  • PAGE

RUNS

PASS #0

2-PAGE RUNS

PASS #1

3,4 2,6 4,9 7,8 5,6 1,3 2 ∅ 6,2 9,4 8,7 5,6 3,1 2 ∅ 3,4 2,3 4,6 4,7 8,9 1,3 5,6 ∅ 2

EOF

slide-15
SLIDE 15

CMU 15-445/645 (Fall 2018)

2- WAY EXTERN AL M ERGE SO RT

In each pass, we read and write each page in file. Number of passes = 1 + ⌈ log2 N ⌉ Total I/O cost = 2N ∙ (# of passes)

8

1

  • PAGE

RUNS

PASS #0

2-PAGE RUNS

PASS #1

4-PAGE RUNS

PASS #2

3,4 2,6 4,9 7,8 5,6 1,3 2 ∅ 6,2 9,4 8,7 5,6 3,1 2 ∅ 3,4 2,3 4,6 4,7 8,9 1,3 5,6 ∅ 2 4,4 6,7 8,9 2,3 ∅ 1,2 3,5 6

EOF

slide-16
SLIDE 16

CMU 15-445/645 (Fall 2018)

2- WAY EXTERN AL M ERGE SO RT

In each pass, we read and write each page in file. Number of passes = 1 + ⌈ log2 N ⌉ Total I/O cost = 2N ∙ (# of passes)

8

1

  • PAGE

RUNS

PASS #0

2-PAGE RUNS

PASS #1

4-PAGE RUNS

PASS #2

8-PAGE RUNS

PASS #3

3,4 2,6 4,9 7,8 5,6 1,3 2 ∅ 6,2 9,4 8,7 5,6 3,1 2 ∅ 3,4 2,3 4,6 4,7 8,9 1,3 5,6 ∅ 2 4,4 6,7 8,9 2,3 ∅ 1,2 3,5 6 ∅ 1,2 2,3 3,4 4,5 6,6 7,8 9

EOF

slide-17
SLIDE 17

CMU 15-445/645 (Fall 2018)

2- WAY EXTERN AL M ERGE SO RT

This algorithm only requires three buffer pages (B=3). Even if we have more buffer space available (B>3), it does not effectively utilize them. Let’s next generalize the algorithm to make use of extra buffer space.

9

slide-18
SLIDE 18

CMU 15-445/645 (Fall 2018)

GEN ERAL EXTERN AL M ERGE SO RT

Pass #0

→ Use B buffer pages. → Produce ⌈N / B⌉ sorted runs of size B

Pass #1,2,3,…

→ Merge B-1 runs (i.e., K-way merge).

Number of passes = 1 + ⌈ logB-1 ⌈N / B⌉ ⌉ Total I/O Cost = 2N ∙ (# of passes)

10

slide-19
SLIDE 19

CMU 15-445/645 (Fall 2018)

GEN ERAL EXTERN AL M ERGE SO RT

Pass #0

→ Use B buffer pages. → Produce ⌈N / B⌉ sorted runs of size B

Pass #1,2,3,…

→ Merge B-1 runs (i.e., K-way merge).

Number of passes = 1 + ⌈ logB-1 ⌈N / B⌉ ⌉ Total I/O Cost = 2N ∙ (# of passes)

10

slide-20
SLIDE 20

CMU 15-445/645 (Fall 2018)

GEN ERAL EXTERN AL M ERGE SO RT

Pass #0

→ Use B buffer pages. → Produce ⌈N / B⌉ sorted runs of size B

Pass #1,2,3,…

→ Merge B-1 runs (i.e., K-way merge).

Number of passes = 1 + ⌈ logB-1 ⌈N / B⌉ ⌉ Total I/O Cost = 2N ∙ (# of passes)

10

slide-21
SLIDE 21

CMU 15-445/645 (Fall 2018)

EXAM PLE

Sort 108 page file with 5 buffer pages: N=108, B=5

→ Pass #0: ⌈N / B⌉ = ⌈108 / 5⌉ = 22 sorted runs of 5 pages each (last run is only 3 pages) → Pass #1: ⌈N’ / B-1⌉ = ⌈22 / 4⌉ = 6 sorted runs of 20 pages each (last run is only 8 pages) → Pass #2: ⌈N’’ / B-1⌉ = ⌈6 / 4⌉ = 2 sorted runs, 80 pages and 28 pages → Pass #3: Sorted file of 108 pages

1+⌈ logB-1⌈N / B⌉ ⌉ = 1+⌈log4 22⌉ = 1+⌈2.229...⌉ = 4 passes

12

slide-22
SLIDE 22

CMU 15-445/645 (Fall 2018)

USIN G B+ TREES

If the table that must be sorted already has a B+ tree index on the sort attribute(s), then we can use that to accelerate sorting. Retrieve tuples in desired sort order by simply traversing the leaf pages of the tree. Cases to consider:

→ Clustered B+ tree → Unclustered B+ tree

13

slide-23
SLIDE 23

CMU 15-445/645 (Fall 2018)

CASE 1: CLUSTERED B+ TREE

Traverse to the left-most leaf page, and then retrieve tuples from all leaf pages. This will always better than external sorting.

14

(Directs search) Data Records Index Data Entries ("Sequence set")

101 102 103 104

slide-24
SLIDE 24

CMU 15-445/645 (Fall 2018)

CASE 2: UN CLUSTERED B+ TREE

Chase each pointer to the page that contains the data. This is almost always a bad idea. In general, one I/O per data record.

15

101 102 103 104

(Directs search) Data Records Index Data Entries ("Sequence set")

slide-25
SLIDE 25

CMU 15-445/645 (Fall 2018)

AGGREGATIO NS

Collapse multiple tuples into a single scalar value. Two implementation choices:

→ Sorting → Hashing

17

slide-26
SLIDE 26

CMU 15-445/645 (Fall 2018)

SO RTIN G AGGREGATIO N

18

Filter

sid cid grade 53666 15-445 C 53688 15-826 B 53666 15-721 C 53655 15-445 C

SELECT DISTINCT cid FROM enrolled WHERE grade IN ('B','C')

sid cid grade 53666 15-445 C 53688 15-721 A 53688 15-826 B 53666 15-721 C 53655 15-445 C

enrolled(sid,cid,grade)

slide-27
SLIDE 27

CMU 15-445/645 (Fall 2018)

SO RTIN G AGGREGATIO N

18

Remove Columns Filter

sid cid grade 53666 15-445 C 53688 15-826 B 53666 15-721 C 53655 15-445 C cid 15-445 15-826 15-721 15-445

SELECT DISTINCT cid FROM enrolled WHERE grade IN ('B','C')

sid cid grade 53666 15-445 C 53688 15-721 A 53688 15-826 B 53666 15-721 C 53655 15-445 C

enrolled(sid,cid,grade)

slide-28
SLIDE 28

CMU 15-445/645 (Fall 2018)

cid 15-445 15-445 15-721 15-826

SO RTIN G AGGREGATIO N

18

Remove Columns Sort Filter

sid cid grade 53666 15-445 C 53688 15-826 B 53666 15-721 C 53655 15-445 C cid 15-445 15-826 15-721 15-445

SELECT DISTINCT cid FROM enrolled WHERE grade IN ('B','C')

sid cid grade 53666 15-445 C 53688 15-721 A 53688 15-826 B 53666 15-721 C 53655 15-445 C

enrolled(sid,cid,grade)

slide-29
SLIDE 29

CMU 15-445/645 (Fall 2018)

cid 15-445 15-445 15-721 15-826

SO RTIN G AGGREGATIO N

18

Remove Columns Sort Eliminate Dupes

X

Filter

sid cid grade 53666 15-445 C 53688 15-826 B 53666 15-721 C 53655 15-445 C cid 15-445 15-826 15-721 15-445

SELECT DISTINCT cid FROM enrolled WHERE grade IN ('B','C')

sid cid grade 53666 15-445 C 53688 15-721 A 53688 15-826 B 53666 15-721 C 53655 15-445 C

enrolled(sid,cid,grade)

slide-30
SLIDE 30

CMU 15-445/645 (Fall 2018)

ALTERN ATIVES TO SO RTIN G

What if we don’t need the data to be ordered?

→ Forming groups in GROUP BY (no ordering) → Removing duplicates in DISTINCT (no ordering)

20

slide-31
SLIDE 31

CMU 15-445/645 (Fall 2018)

ALTERN ATIVES TO SO RTIN G

What if we don’t need the data to be ordered?

→ Forming groups in GROUP BY (no ordering) → Removing duplicates in DISTINCT (no ordering)

Hashing is a better alternative in this scenario.

→ Only need to remove duplicates, no need for ordering. → Can be computationally cheaper than sorting.

20

slide-32
SLIDE 32

CMU 15-445/645 (Fall 2018)

H ASH IN G AGGREGATE

Populate an ephemeral hash table as the DBMS scans the table. For each record, check whether there is already an entry in the hash table:

→ DISTINCT: Discard duplicate. → GROUP BY: Perform aggregate computation.

If everything fits in memory, then it's easy. If we have to spill to disk, then we need to be smarter…

21

slide-33
SLIDE 33

CMU 15-445/645 (Fall 2018)

H ASH IN G AGGREGATE

Partition Phase

→ Divide tuples into buckets based on hash key.

ReHash Phase

→ Build in-memory hash table for each partition and compute the aggregation.

22

slide-34
SLIDE 34

CMU 15-445/645 (Fall 2018)

H ASH IN G AGGREGATE PH ASE # 1: PARTITIO N

Use a hash function h1 to split tuples into partitions on disk.

→ We know that all matches live in the same partition. → Partitions are "spilled" to disk via output buffers.

Assume that we have B buffers.

23

slide-35
SLIDE 35

CMU 15-445/645 (Fall 2018)

H ASH IN G AGGREGATE PH ASE # 1: PARTITIO N

24

Filter

sid cid grade 53666 15-445 C 53688 15-826 B 53666 15-721 C 53655 15-445 C

SELECT DISTINCT cid FROM enrolled WHERE grade IN ('B','C')

sid cid grade 53666 15-445 C 53688 15-721 A 53688 15-826 B 53666 15-721 C 53655 15-445 C

enrolled(sid,cid,grade)

slide-36
SLIDE 36

CMU 15-445/645 (Fall 2018)

H ASH IN G AGGREGATE PH ASE # 1: PARTITIO N

24

Remove Columns Filter

sid cid grade 53666 15-445 C 53688 15-826 B 53666 15-721 C 53655 15-445 C cid 15-445 15-826 15-721 15-445

SELECT DISTINCT cid FROM enrolled WHERE grade IN ('B','C')

sid cid grade 53666 15-445 C 53688 15-721 A 53688 15-826 B 53666 15-721 C 53655 15-445 C

enrolled(sid,cid,grade)

slide-37
SLIDE 37

CMU 15-445/645 (Fall 2018)

H ASH IN G AGGREGATE PH ASE # 1: PARTITIO N

24

Remove Columns Filter

sid cid grade 53666 15-445 C 53688 15-826 B 53666 15-721 C 53655 15-445 C cid 15-445 15-826 15-721 15-445

SELECT DISTINCT cid FROM enrolled WHERE grade IN ('B','C')

15-445 15-826 15-721 ⋮

h1

B-1 partitions

sid cid grade 53666 15-445 C 53688 15-721 A 53688 15-826 B 53666 15-721 C 53655 15-445 C

enrolled(sid,cid,grade)

slide-38
SLIDE 38

CMU 15-445/645 (Fall 2018)

H ASH IN G AGGREGATE PH ASE # 2: REH ASH

For each partition on disk:

→ Read it into memory and build an in-memory hash table based on a second hash function h2. → Then go through each bucket of this hash table to bring together matching tuples.

This assumes that each partition fits in memory.

25

slide-39
SLIDE 39

CMU 15-445/645 (Fall 2018)

H ASH IN G AGGREGATE PH ASE # 2: REH ASH

26

SELECT DISTINCT cid FROM enrolled WHERE grade IN ('B','C')

15-445 15-826 15-721 ⋮

Phase #1 Buckets

sid cid grade 53666 15-445 C 53688 15-721 A 53688 15-826 B 53666 15-721 C 53655 15-445 C

enrolled(sid,cid,grade)

slide-40
SLIDE 40

CMU 15-445/645 (Fall 2018)

H ASH IN G AGGREGATE PH ASE # 2: REH ASH

26

SELECT DISTINCT cid FROM enrolled WHERE grade IN ('B','C')

15-445 15-826 15-721 ⋮

h2 h2 h2

Phase #1 Buckets

Key Value XXX 15-445 YYY 15-826 ZZZ 15-721

Hash Table

sid cid grade 53666 15-445 C 53688 15-721 A 53688 15-826 B 53666 15-721 C 53655 15-445 C

enrolled(sid,cid,grade)

slide-41
SLIDE 41

CMU 15-445/645 (Fall 2018)

H ASH IN G AGGREGATE PH ASE # 2: REH ASH

26

SELECT DISTINCT cid FROM enrolled WHERE grade IN ('B','C')

15-445 15-826 15-721 ⋮

h2 h2 h2

Phase #1 Buckets

Key Value XXX 15-445 YYY 15-826 ZZZ 15-721 cid 15-445 15-721 15-826

Hash Table

sid cid grade 53666 15-445 C 53688 15-721 A 53688 15-826 B 53666 15-721 C 53655 15-445 C

enrolled(sid,cid,grade)

slide-42
SLIDE 42

CMU 15-445/645 (Fall 2018)

H ASH IN G SUM M ARIZATIO N

During the ReHash phase, store pairs of the form (GroupKey→RunningVal) When we want to insert a new tuple into the hash table:

→ If we find a matching GroupKey, just update the RunningVal appropriately → Else insert a new GroupKey→RunningVal

27

slide-43
SLIDE 43

CMU 15-445/645 (Fall 2018)

H ASH IN G SUM M ARIZATIO N

28

SELECT cid, AVG(s.gpa) FROM student AS s, enrolled AS e WHERE s.sid = e.sid GROUP BY cid

15-445 15-445 15-826 15-721 ⋮

Phase #1 Buckets

slide-44
SLIDE 44

CMU 15-445/645 (Fall 2018)

H ASH IN G SUM M ARIZATIO N

28

SELECT cid, AVG(s.gpa) FROM student AS s, enrolled AS e WHERE s.sid = e.sid GROUP BY cid

15-445 15-445 15-826 15-721 ⋮

h2 h2 h2

Phase #1 Buckets

key value XXX 15-445→(2,7.32) YYY 15-826→(1,3.33) ZZZ 15-721→(1,2.89)

Hash Table

slide-45
SLIDE 45

CMU 15-445/645 (Fall 2018)

H ASH IN G SUM M ARIZATIO N

28

SELECT cid, AVG(s.gpa) FROM student AS s, enrolled AS e WHERE s.sid = e.sid GROUP BY cid

15-445 15-445 15-826 15-721 ⋮

h2 h2 h2

Phase #1 Buckets

key value XXX 15-445→(2,7.32) YYY 15-826→(1,3.33) ZZZ 15-721→(1,2.89)

Hash Table

AVG(col) → (COUNT,SUM) MIN(col) → (MIN) MAX(col) → (MAX) SUM(col) → (SUM) COUNT(col) → (COUNT)

Running Totals

slide-46
SLIDE 46

CMU 15-445/645 (Fall 2018)

H ASH IN G SUM M ARIZATIO N

28

SELECT cid, AVG(s.gpa) FROM student AS s, enrolled AS e WHERE s.sid = e.sid GROUP BY cid

15-445 15-445 15-826 15-721 ⋮

h2 h2 h2

Phase #1 Buckets

key value XXX 15-445→(2,7.32) YYY 15-826→(1,3.33) ZZZ 15-721→(1,2.89)

Hash Table

cid AVG(gpa) 15-445 3.66 15-826 3.33 15-721 2.89

Final Result

AVG(col) → (COUNT,SUM) MIN(col) → (MIN) MAX(col) → (MAX) SUM(col) → (SUM) COUNT(col) → (COUNT)

Running Totals

slide-47
SLIDE 47

CMU 15-445/645 (Fall 2018)

CO ST AN ALYSIS

How big of a table can we hash using this approach?

→ B-1 "spill partitions" in Phase #1 → Each should be no more than B blocks big

Answer: B ∙ (B-1)

→ A table of N pages needs about sqrt(N) buffers → Assumes hash distributes records evenly. Use a "fudge factor" f>1 for that: we need B ∙ sqrt(f ∙ N)

29

slide-48
SLIDE 48

CMU 15-445/645 (Fall 2018)

CO N CLUSIO N

Choice of sorting vs. hashing is subtle and depends

  • n optimizations done in each case.

We already discussed the optimizations for sorting:

→ Chunk I/O into large blocks to amortize seek+RD costs. → Double-buffering to overlap CPU and I/O.

30

slide-49
SLIDE 49

CMU 15-445/645 (Fall 2018)

N EXT CLASS

Nested Loop Join Sort-Merge Join Hash Join "Exotic" Joins

31