Database System Internals - courses.cs.washington.edu

93
1 April 15, 2020 Database System Internals CSE 444 - Spring 2020 Operator Algorithms (part 2)

Transcript of Database System Internals - courses.cs.washington.edu

Page 1: Database System Internals - courses.cs.washington.edu

1April 15, 2020

Database System Internals

CSE 444 - Spring 2020

Operator Algorithms (part 2)

Page 2: Database System Internals - courses.cs.washington.edu

Announcements

§ Sections tomorrow: B+ trees

§Homework 2 released, due on Monday, 4/27

§544 paper 1 report due this Friday, 4/17

§ Lab 2 will be posted tomorrow morning• Part 1 (operator algos) due next Friday, 4/24• Part 2 (insert/delete support) due following Friday

CSE 444 - Spring 2020 2April 15, 2020

Page 3: Database System Internals - courses.cs.washington.edu

Today’s Outline

Query Execution Algorithms:

§Catch-up from last lecture

§ Finish operator implementation

April 15, 2020 CSE 444 - Spring 2020 3

Page 4: Database System Internals - courses.cs.washington.edu

Operator Algorithms

Design criteria

§Cost: IO, CPU, Network

§Memory utilization

§ Load balance (for parallel operators)

CSE 444 - Spring 2020 4April 15, 2020

Page 5: Database System Internals - courses.cs.washington.edu

Cost Parameters

§Cost = total number of I/Os• This is a simplification that ignores CPU, network

§Parameters:• B(R) = # of blocks (i.e., pages) for relation R• T(R) = # of tuples in relation R• V(R, a) = # of distinct values of attribute a

• When a is a key, V(R,a) = T(R)• When a is not a key, V(R,a) can be anything < T(R)

5CSE 444 - Spring 2020April 15, 2020

Page 6: Database System Internals - courses.cs.washington.edu

CSE 444 - Spring 2020

Convention

§Cost = the cost of reading operands from disk,plus cost to read/write intermediate results

§Cost of writing the final result to disk is not included; need to count it separately when applicable

6April 15, 2020

Page 7: Database System Internals - courses.cs.washington.edu

CSE 444 - Spring 2020

Outline

§ Join operator algorithms• One-pass algorithms (Sec. 15.2 and 15.3)• Index-based algorithms (Sec 15.6)• Two-pass algorithms (Sec 15.4 and 15.5)

§Note about readings: • In class, we discuss only algorithms for joins• Other operators are easier: book has extra details

7April 15, 2020

Page 8: Database System Internals - courses.cs.washington.edu

CSE 444 - Spring 2020

Join Algorithms

§Hash join

§Nested loop join

§ Sort-merge join

8April 15, 2020

Page 9: Database System Internals - courses.cs.washington.edu

CSE 444 - Spring 2020

Hash Join

Hash join: R ⋈ S§ Scan R, build buckets in main memory§ Then scan S and join§Cost: B(R) + B(S)

§One-pass algorithm when B(R) ≤ M

9April 15, 2020

Note: the inner relation is the relation on which we build the hash table• Usually this is the right relation of R ⋈ S, i.e. S.• But the following slides choose the left relation, i.e. R

Page 10: Database System Internals - courses.cs.washington.edu

Hash Join Example

10

Patient Insurance

Patient(pid, name, address)Insurance(pid, provider, policy_nb)

1 ‘Bob’ ‘Seattle’

2 ‘Ela’ ‘Everett’

3 ‘Jill’ ‘Kent’

4 ‘Joe’ ‘Seattle’

Patient2 ‘Blue’ 123

4 ‘Prem’ 432

Insurance

4 ‘Prem’ 343

1 ‘GrpH’ 554

Two tuplesper page

April 15, 2020 CSE 444 - Spring 2020

Page 11: Database System Internals - courses.cs.washington.edu

Hash Join Example

11

Patient Insurance

1 2

3 4

Patient2 4

Insurance

4 3

Showing pid only

8 5

9 6 2 8

8 9

6 6

1 3

Disk

Memory M = 21 pages

Some large-enough nb

April 15, 2020 CSE 444 - Spring 2020

This is one page with two tuples

Page 12: Database System Internals - courses.cs.washington.edu

Hash Join Example

12

Step 1: Scan Patient and build hash table in memoryCan be done inmethod open()

1 2

3 4

Patient2 4

Insurance

4 3

8 5

9 6 2 8

8 9

6 6

1 3

Disk

Memory M = 21 pagesHash h: pid % 5

Input buffer

1 2 43 96 85

1 2

April 15, 2020 CSE 444 - Spring 2020

Page 13: Database System Internals - courses.cs.washington.edu

Hash Join Example

13

Step 2: Scan Insurance and probe into hash tableDone during calls to next()

1 2

3 4

Patient2 4

Insurance

4 3

8 5

9 6 2 8

8 9

6 6

1 3

Disk

Memory M = 21 pagesHash h: pid % 5

Input buffer

1 2 43 96 85

1 22 4

Output buffer

2 2

Write to disk or pass to next

operatorApril 15, 2020 CSE 444 - Spring 2020

Page 14: Database System Internals - courses.cs.washington.edu

Hash Join Example

14

Step 2: Scan Insurance and probe into hash tableDone during calls to next()

1 2

3 4

Patient2 4

Insurance

4 3

8 5

9 6 2 8

8 9

6 6

1 3

Disk

Memory M = 21 pagesHash h: pid % 5

Input buffer

1 2 43 96 85

1 22 4

Output buffer

4 4

April 15, 2020 CSE 444 - Spring 2020

Page 15: Database System Internals - courses.cs.washington.edu

Hash Join Example

15

Step 2: Scan Insurance and probe into hash tableDone during calls to next()

1 2

3 4

Patient2 4

Insurance

4 3

8 5

9 6 2 8

8 9

6 6

1 3

Disk

Memory M = 21 pagesHash h: pid % 5

Input buffer

1 2 43 96 85

1 24 3

Output buffer

4 4

Keep going until read all of Insurance

Cost: B(R) + B(S)

April 15, 2020 CSE 444 - Spring 2020

Page 16: Database System Internals - courses.cs.washington.edu

Discussion

§Hash-join is the workhorse of database systems

§ The hash table is built on the heap, not in BP;hence it is not organized in pages,but pages are still convenient to measure its size

§Hash-join works great when:• The inner table fits in main memory• The hash function is good (never write your own!)• The data has no skew (discuss in class…)

April 15, 2020 CSE 444 - Spring 2020 16

Page 17: Database System Internals - courses.cs.washington.edu

CSE 444 - Spring 2020

Nested Loop Joins

§ Tuple-based nested loop R ⋈ S§ R is the outer relation, S is the inner relation

for each tuple t1 in R dofor each tuple t2 in S do

if t1 and t2 join then output (t1,t2)

17

What is the Cost?

April 15, 2020

Page 18: Database System Internals - courses.cs.washington.edu

CSE 444 - Spring 2020

Nested Loop Joins

§ Tuple-based nested loop R ⋈ S§ R is the outer relation, S is the inner relation

§Cost: B(R) + T(R) B(S)§Multiple-pass since S is read many times

18April 15, 2020

for each tuple t1 in R dofor each tuple t2 in S do

if t1 and t2 join then output (t1,t2)

What is the Cost?

Page 19: Database System Internals - courses.cs.washington.edu

Page-at-a-time Refinement

CSE 444 - Spring 2020 19

for each page of tuples r in R dofor each page of tuples s in S do

for all pairs of tuples t1 in r, t2 in sif t1 and t2 join then output (t1,t2)

What is the Cost?

April 15, 2020

Page 20: Database System Internals - courses.cs.washington.edu

CSE 444 - Spring 2020

Page-at-a-time Refinement

§Cost: B(R) + B(R)B(S)

20April 15, 2020

for each page of tuples r in R dofor each page of tuples s in S do

for all pairs of tuples t1 in r, t2 in sif t1 and t2 join then output (t1,t2)

What is the Cost?

Page 21: Database System Internals - courses.cs.washington.edu

1 2

Page-at-a-time Refinement

21

1 2

3 4

Patient2 4

Insurance

4 3

8 5

9 6 2 8

8 9

6 6

1 3

Disk

Input buffer for Patient

Output buffer

2 2

Input buffer for Insurance2 4

April 15, 2020 CSE 444 - Spring 2020

Page 22: Database System Internals - courses.cs.washington.edu

Page-at-a-time Refinement

22

1 2

3 4

Patient2 4

Insurance

4 3

8 5

9 6 2 8

8 9

6 6

1 3

Disk

Input buffer for Patient1 2

Output buffer

Input buffer for Insurance4 3

1 2

April 15, 2020 CSE 444 - Spring 2020

Page 23: Database System Internals - courses.cs.washington.edu

Page-at-a-time Refinement

23

1 2

3 4

Patient2 4

Insurance

4 3

8 5

9 6 2 8

8 9

6 6

1 3

Disk

Input buffer for Patient1 2

Output buffer

Input buffer for Insurance2 8

1 2

2 2

Cost: B(R) + B(R)B(S)

Keep going until read all of Insurance

Then repeat for next page of Patient… until end of Patient

April 15, 2020 CSE 444 - Spring 2020

Page 24: Database System Internals - courses.cs.washington.edu

Block-Memory Refinement

24

for each group of M-1 pages r in R dofor each page of tuples s in S dofor all pairs of tuples t1 in r, t2 in s

if t1 and t2 join then output (t1,t2)

What is the Cost?

April 15, 2020 CSE 444 - Spring 2020

Page 25: Database System Internals - courses.cs.washington.edu

Block Memory Refinement

25

1 2

3 4

Patient2 4

Insurance

4 3

8 5

9 6 2 8

8 9

6 6

1 3

Disk

Input buffer for Patient1 2

Input buffer for Insurance

1 2

M= 3

No output buffer: stream to output

April 15, 2020 CSE 444 - Spring 2020

Block Memory Refinement

Page 26: Database System Internals - courses.cs.washington.edu

Block Memory Refinement

26

1 2

3 4

Patient2 4

Insurance

4 3

8 5

9 6 2 8

8 9

6 6

1 3

Disk

Input buffer for Patient1 2

Input buffer for Insurance

1 2

M= 3

No output buffer: stream to output

April 15, 2020 CSE 444 - Spring 2020

Block Memory Refinement

Page 27: Database System Internals - courses.cs.washington.edu

Block Memory Refinement

27

1 2

3 4

Patient2 4

Insurance

4 3

8 5

9 6 2 8

8 9

6 6

1 3

Disk

Input buffer for Patient1 2

Input buffer for Insurance

1 2

1 23 4

M= 3

No output buffer: stream to output

April 15, 2020 CSE 444 - Spring 2020

Block Memory Refinement

Page 28: Database System Internals - courses.cs.washington.edu

Block Memory Refinement

28

1 2

3 4

Patient2 4

Insurance

4 3

8 5

9 6 2 8

8 9

6 6

1 3

Disk

Input buffer for Patient1 2

Input buffer for Insurance

1 2

1 23 4

M= 3

No output buffer: stream to output

April 15, 2020 CSE 444 - Spring 2020

Block Memory Refinement

Page 29: Database System Internals - courses.cs.washington.edu

Block Memory Refinement

29

1 2

3 4

Patient2 4

Insurance

4 3

8 5

9 6 2 8

8 9

6 6

1 3

Disk

Input buffer for Patient1 2

Input buffer for Insurance2 4

1 2

1 23 4

M= 3

No output buffer: stream to output

April 15, 2020 CSE 444 - Spring 2020

Block Memory Refinement

Page 30: Database System Internals - courses.cs.washington.edu

Block Memory Refinement

30

1 2

3 4

Patient2 4

Insurance

4 3

8 5

9 6 2 8

8 9

6 6

1 3

Disk

Input buffer for Patient1 2

Input buffer for Insurance4 3

1 2

1 23 4

M= 3

No output buffer: stream to output

April 15, 2020 CSE 444 - Spring 2020

Block Memory Refinement

Page 31: Database System Internals - courses.cs.washington.edu

Block Memory Refinement

31

1 2

3 4

Patient2 4

Insurance

4 3

8 5

9 6 2 8

8 9

6 6

1 3

Disk

Input buffer for Patient1 2

Input buffer for Insurance2 8

1 2

1 23 4

M= 3

No output buffer: stream to output

April 15, 2020 CSE 444 - Spring 2020

Block Memory Refinement

Page 32: Database System Internals - courses.cs.washington.edu

Block Memory Refinement

32

1 2

3 4

Patient2 4

Insurance

4 3

8 5

9 6 2 8

8 9

6 6

1 3

Disk

Input buffer for Patient1 2

Input buffer for Insurance

1 2

M= 3

No output buffer: stream to output

April 15, 2020 CSE 444 - Spring 2020

Block Memory Refinement

Page 33: Database System Internals - courses.cs.washington.edu

Block Memory Refinement

33

1 2

3 4

Patient2 4

Insurance

4 3

8 5

9 6 2 8

8 9

6 6

1 3

Disk

Input buffer for Patient1 2

Input buffer for Insurance

9 6

1 28 5

M= 3

No output buffer: stream to output

April 15, 2020 CSE 444 - Spring 2020

Block Memory Refinement

Page 34: Database System Internals - courses.cs.washington.edu

Block Memory Refinement

34

1 2

3 4

Patient2 4

Insurance

4 3

8 5

9 6 2 8

8 9

6 6

1 3

Disk

Input buffer for Patient1 2

Input buffer for Insurance2 4

9 6

1 28 5

M= 3

No output buffer: stream to output

April 15, 2020 CSE 444 - Spring 2020

Block Memory Refinement

Page 35: Database System Internals - courses.cs.washington.edu

Block Memory Refinement

35

What is the Cost

April 15, 2020 CSE 444 - Spring 2020

for each group of M-1 pages r in R dofor each page of tuples s in S dofor all pairs of tuples t1 in r, t2 in s

if t1 and t2 join then output (t1,t2)

Page 36: Database System Internals - courses.cs.washington.edu

Block Memory Refinement

§Cost: B(R) + B(R)B(S)/(M-1)

36April 15, 2020 CSE 444 - Spring 2020

What is the Cost

for each group of M-1 pages r in R dofor each page of tuples s in S dofor all pairs of tuples t1 in r, t2 in s

if t1 and t2 join then output (t1,t2)

Page 37: Database System Internals - courses.cs.washington.edu

Discussion

R ⋈ S: R=outer table, S=inner table

§ Tuple-based nested loop join is never used

§ Page-at-a-time nested loop join:• Usually combined with index access to inner table• Efficient when the outer table is small

§ Block memory refinement nested loop:• Usually builds a hash table on the outer table• Efficient when the outer table is small

April 15, 2020 CSE 444 - Spring 2020 37

Page 38: Database System Internals - courses.cs.washington.edu

Sort-Merge Join

Sort-merge join: R ⋈ S§ Scan R and sort in main memory§ Scan S and sort in main memory§Merge R and S

38April 15, 2020 CSE 444 - Spring 2020

Page 39: Database System Internals - courses.cs.washington.edu

Sort-Merge Join

Sort-merge join: R ⋈ S§ Scan R and sort in main memory§ Scan S and sort in main memory§Merge R and S

§Cost: B(R) + B(S)§One pass algorithm when B(S) + B(R) <= M§ Typically, this is NOT a one pass algorithm,

• We’ll see the multi-pass version next lecture

39April 15, 2020 CSE 444 - Spring 2020

Page 40: Database System Internals - courses.cs.washington.edu

Sort-Merge Join Example

40

1 2

3 4

Patient2 4

Insurance

4 3

8 5

9 6 2 8

8 9

6 6

1 3

Disk

Memory M = 21 pages

1 2 43 96 85

Step 1: Scan Patient and sort in memory

April 15, 2020 CSE 444 - Spring 2020

Page 41: Database System Internals - courses.cs.washington.edu

Sort-Merge Join Example

41

1 2

3 4

Patient2 4

Insurance

4 3

8 5

9 6 2 8

8 9

6 6

1 3

Disk

Memory M = 21 pages

1 2 43 96 85

Step 2: Scan Insurance and sort in memory

1 2 3 4

6 8 8 9

2 3 4 6

April 15, 2020 CSE 444 - Spring 2020

Page 42: Database System Internals - courses.cs.washington.edu

Sort-Merge Join Example

42

1 2

3 4

Patient2 4

Insurance

4 3

8 5

9 6 2 8

8 9

6 6

1 3

Disk

Memory M = 21 pages

1 2 43 96 85

Step 3: Merge Patient and Insurance

1 2 3 4

6 8 8 9

2 3 4 6

Output buffer

April 15, 2020 CSE 444 - Spring 2020

Page 43: Database System Internals - courses.cs.washington.edu

Sort-Merge Join Example

43

1 2

3 4

Patient2 4

Insurance

4 3

8 5

9 6 2 8

8 9

6 6

1 3

Disk

Memory M = 21 pages

1 2 43 96 85

Step 3: Merge Patient and Insurance

1 2 3 4

6 8 8 9

2 3 4 6

Output buffer

1 1

April 15, 2020 CSE 444 - Spring 2020

Page 44: Database System Internals - courses.cs.washington.edu

Sort-Merge Join Example

44

1 2

3 4

Patient2 4

Insurance

4 3

8 5

9 6 2 8

8 9

6 6

1 3

Disk

Memory M = 21 pages

1 2 43 96 85

Step 3: Merge Patient and Insurance

1 2 3 4

6 8 8 9

2 3 4 6

Output buffer

April 15, 2020 CSE 444 - Spring 2020

Page 45: Database System Internals - courses.cs.washington.edu

Sort-Merge Join Example

45

1 2

3 4

Patient2 4

Insurance

4 3

8 5

9 6 2 8

8 9

6 6

1 3

Disk

Memory M = 21 pages

1 2 43 96 85

Step 3: Merge Patient and Insurance

1 2 3 4

6 8 8 9

2 3 4 6

Output buffer

April 15, 2020 CSE 444 - Spring 2020

Page 46: Database System Internals - courses.cs.washington.edu

Sort-Merge Join Example

46

1 2

3 4

Patient2 4

Insurance

4 3

8 5

9 6 2 8

8 9

6 6

1 3

Disk

Memory M = 21 pages

1 2 43 96 85

Step 3: Merge Patient and Insurance

1 2 3 4

6 8 8 9

2 3 4 6

Output buffer

2 2

April 15, 2020 CSE 444 - Spring 2020

Page 47: Database System Internals - courses.cs.washington.edu

Sort-Merge Join Example

47

1 2

3 4

Patient2 4

Insurance

4 3

8 5

9 6 2 8

8 9

6 6

1 3

Disk

Memory M = 21 pages

1 2 43 96 85

Step 3: Merge Patient and Insurance

1 2 3 4

6 8 8 9

2 3 4 6

Output buffer

2 2

April 15, 2020 CSE 444 - Spring 2020

Page 48: Database System Internals - courses.cs.washington.edu

Sort-Merge Join Example

48

1 2

3 4

Patient2 4

Insurance

4 3

8 5

9 6 2 8

8 9

6 6

1 3

Disk

Memory M = 21 pages

1 2 43 96 85

Step 3: Merge Patient and Insurance

1 2 3 4

6 8 8 9

2 3 4 6

Output buffer

Keep going until end of first relation

April 15, 2020 CSE 444 - Spring 2020

Page 49: Database System Internals - courses.cs.washington.edu

CSE 444 - Spring 2020

Outline

§ Join operator algorithms• One-pass algorithms (Sec. 15.2 and 15.3)• Index-based algorithms (Sec 15.6)• Two-pass algorithms (Sec 15.4 and 15.5)

49April 15, 2020

Page 50: Database System Internals - courses.cs.washington.edu

CSE 444 - Spring 2020

Index Based Selection

Selection on equality: sa=v(R)§ B(R)= size of R in blocks§ T(R) = number of tuples in R§V(R, a) = # of distinct values of attribute a

50April 15, 2020

Note: we ignore I/O cost for index pages

Page 51: Database System Internals - courses.cs.washington.edu

CSE 444 - Spring 2020

Index Based Selection

Selection on equality: sa=v(R)§ B(R)= size of R in blocks§ T(R) = number of tuples in R§V(R, a) = # of distinct values of attribute a

What is the cost in each case?§Clustered index on a: §Unclustered index on a:

51April 15, 2020

Note: we ignore I/O cost for index pages

Page 52: Database System Internals - courses.cs.washington.edu

CSE 444 - Spring 2020

Index Based Selection

Selection on equality: sa=v(R)§ B(R)= size of R in blocks§ T(R) = number of tuples in R§V(R, a) = # of distinct values of attribute a

What is the cost in each case?§Clustered index on a: B(R)/V(R,a)§Unclustered index on a:

52April 15, 2020

Note: we ignore I/O cost for index pages

Page 53: Database System Internals - courses.cs.washington.edu

CSE 444 - Spring 2020

Index Based Selection

Selection on equality: sa=v(R)§ B(R)= size of R in blocks§ T(R) = number of tuples in R§V(R, a) = # of distinct values of attribute a

What is the cost in each case?§Clustered index on a: B(R)/V(R,a)§Unclustered index on a: T(R)/V(R,a)

53April 15, 2020

Note: we ignore I/O cost for index pages

Page 54: Database System Internals - courses.cs.washington.edu

Index Based Selection

§ Example:

§ Table scan:§ Index based selection:

B(R) = 2000T(R) = 100,000V(R, a) = 20

cost of sa=v(R) = ?

CSE 444 - Spring 2020 54April 15, 2020

Page 55: Database System Internals - courses.cs.washington.edu

Index Based Selection

§ Example:

§ Table scan: B(R) = 2,000 I/Os§ Index based selection:

B(R) = 2000T(R) = 100,000V(R, a) = 20

cost of sa=v(R) = ?

CSE 444 - Spring 2020 55April 15, 2020

Page 56: Database System Internals - courses.cs.washington.edu

Index Based Selection

§ Example:

§ Table scan: B(R) = 2,000 I/Os§ Index based selection:

• If index is clustered:• If index is unclustered:

B(R) = 2000T(R) = 100,000V(R, a) = 20

cost of sa=v(R) = ?

CSE 444 - Spring 2020 56April 15, 2020

Page 57: Database System Internals - courses.cs.washington.edu

Index Based Selection

§ Example:

§ Table scan: B(R) = 2,000 I/Os§ Index based selection:

• If index is clustered: B(R)/V(R,a) = 100 I/Os• If index is unclustered:

B(R) = 2000T(R) = 100,000V(R, a) = 20

cost of sa=v(R) = ?

CSE 444 - Spring 2020 57April 15, 2020

Page 58: Database System Internals - courses.cs.washington.edu

Index Based Selection

§ Example:

§ Table scan: B(R) = 2,000 I/Os§ Index based selection:

• If index is clustered: B(R)/V(R,a) = 100 I/Os• If index is unclustered: T(R)/V(R,a) = 5,000 I/Os

B(R) = 2000T(R) = 100,000V(R, a) = 20

cost of sa=v(R) = ?

CSE 444 - Spring 2020 58April 15, 2020

Page 59: Database System Internals - courses.cs.washington.edu

Index Based Selection

§ Example:

§ Table scan: B(R) = 2,000 I/Os§ Index based selection:

• If index is clustered: B(R)/V(R,a) = 100 I/Os• If index is unclustered: T(R)/V(R,a) = 5,000 I/Os

B(R) = 2000T(R) = 100,000V(R, a) = 20

cost of sa=v(R) = ?

CSE 444 - Spring 2020 59

!

!

April 15, 2020

Page 60: Database System Internals - courses.cs.washington.edu

Index Based Selection

§ Example:

§ Table scan: B(R) = 2,000 I/Os§ Index based selection:

• If index is clustered: B(R)/V(R,a) = 100 I/Os• If index is unclustered: T(R)/V(R,a) = 5,000 I/Os

B(R) = 2000T(R) = 100,000V(R, a) = 20

cost of sa=v(R) = ?

CSE 444 - Spring 2020 60

!

Lesson: Don’t build unclustered indexes when V(R,a) is small !

April 15, 2020

!

Page 61: Database System Internals - courses.cs.washington.edu

Index Based Selection

§ Example:

§ Table scan: B(R) = 2,000 I/Os§ Index based selection:

• If index is clustered: B(R)/V(R,a) = 100 I/Os• If index is unclustered: T(R)/V(R,a) = 5,000 I/Os

B(R) = 2000T(R) = 100,000V(R, a) = 20

cost of sa=v(R) = ?

Lesson: Don’t build unclustered indexes when V(R,a) is small !

CSE 444 - Spring 2020 61April 15, 2020

Page 62: Database System Internals - courses.cs.washington.edu

CSE 444 - Spring 2020

Index Nested Loop Join

R ⋈ S§Assume S has an index on the join attribute§ Iterate over R, for each tuple fetch

corresponding tuple(s) from S

§ Previous nested loop join: cost• B(R) + T(R)*B(S)

§ Index Nested Loop Join Cost:• If index on S is clustered: B(R) + T(R)B(S)/V(S,a)• If index on S is unclustered: B(R) + T(R)T(S)/V(S,a)

62April 15, 2020

Page 63: Database System Internals - courses.cs.washington.edu

CSE 444 - Spring 2020

Outline

§ Join operator algorithms• One-pass algorithms (Sec. 15.2 and 15.3)• Index-based algorithms (Sec 15.6)• Two-pass algorithms (Sec 15.4 and 15.5)

63April 15, 2020

Page 64: Database System Internals - courses.cs.washington.edu

CSE 444 - Spring 2020

Two-Pass Algorithms

§ Fastest algorithm seen so far is one-pass hash join What if data does not fit in memory?

§Need to process it in multiple passes

§ Two key techniques• Sorting• Hashing

64April 15, 2020

Page 65: Database System Internals - courses.cs.washington.edu

Basic Terminology

§A run in a sequence is an increasing subsequence

§What are the runs?

2, 4, 99, 103, 88, 77, 3, 79, 100, 2, 50

CSE 444 - Spring 2020 65April 15, 2020

Page 66: Database System Internals - courses.cs.washington.edu

Basic Terminology

§A run in a sequence is an increasing subsequence

§What are the runs?

2, 4, 99, 103, 88, 77, 3, 79, 100, 2, 50

CSE 444 - Spring 2020 66April 15, 2020

Page 67: Database System Internals - courses.cs.washington.edu

External Merge-Sort: Step 1

Phase one: load M blocks in memory, sort, send to disk, repeat

CSE 444 - Spring 2020 70April 15, 2020

Page 68: Database System Internals - courses.cs.washington.edu

External Merge-Sort: Step 1

DiskDisk

. . .. . .M

Main memory

Q: How long are the runs?

Phase one: load M blocks in memory, sort, send to disk, repeat

CSE 444 - Spring 2020 71April 15, 2020

Page 69: Database System Internals - courses.cs.washington.edu

External Merge-Sort: Step 1

DiskDisk

. . .. . .M

Main memory

Q: How long are the runs?

Phase one: load M blocks in memory, sort, send to disk, repeat

A: Length = M blocks

CSE 444 - Spring 2020 72April 15, 2020

Page 70: Database System Internals - courses.cs.washington.edu

External Merge-Sort: Step 2

§Merge M – 1 runs into a new run§Result: runs of length M (M – 1) ≈ M2

DiskDisk

. . .. . .Input M

Input 1

Input 2. . . .

Output

Main memory

74

Phase two: merge M runs into a bigger run

CSE 444 - Spring 2020April 15, 2020

Page 71: Database System Internals - courses.cs.washington.edu

Example

§Merging three runs to produce a longer run:

0, 14, 33, 88, 92, 192, 3222, 4, 7, 43, 78, 103, 5231, 6, 9, 12, 33, 52, 88, 320

Output:0

CSE 444 - Spring 2020 75April 15, 2020

Page 72: Database System Internals - courses.cs.washington.edu

Example

§Merging three runs to produce a longer run:

0, 14, 33, 88, 92, 192, 3222, 4, 7, 43, 78, 103, 5231, 6, 9, 12, 33, 52, 88, 320

Output:0, ?

CSE 444 - Spring 2020 76April 15, 2020

Page 73: Database System Internals - courses.cs.washington.edu

Example

§Merging three runs to produce a longer run:

0, 14, 33, 88, 92, 192, 3222, 4, 7, 43, 78, 103, 5231, 6, 9, 12, 33, 52, 88, 320

Output:0, 1, ?

CSE 444 - Spring 2020 77April 15, 2020

Page 74: Database System Internals - courses.cs.washington.edu

Example

§Merging three runs to produce a longer run:

0, 14, 33, 88, 92, 192, 3222, 4, 7, 43, 78, 103, 5231, 6, 9, 12, 33, 52, 88, 320

Output:0, 1, 2, 4, 6, 7, ?

CSE 444 - Spring 2020 78April 15, 2020

Page 75: Database System Internals - courses.cs.washington.edu

External Merge-Sort: Step 2

§Merge M – 1 runs into a new run§Result: runs of length M (M – 1) ≈ M2

DiskDisk

. . .. . .Input M

Input 1

Input 2. . . .

Output

Main memory

79

Phase two: merge M runs into a bigger run

CSE 444 - Spring 2020

If approx. B <= M2 then we are doneApril 15, 2020

Page 76: Database System Internals - courses.cs.washington.edu

Cost of External Merge Sort

§Assumption: B(R) <= M2

§Read+write+read = 3B(R)

CSE 444 - Spring 2020 80April 15, 2020

Page 77: Database System Internals - courses.cs.washington.edu

Discussion

§What does B(R) <= M2 mean? §How large can R be?

CSE 444 - Spring 2020 81April 15, 2020

Page 78: Database System Internals - courses.cs.washington.edu

Discussion

§What does B(R) <= M2 mean? §How large can R be?

§ Example:• Page size = 32KB• Memory size 32GB: M = 106-pages

CSE 444 - Spring 2020 82April 15, 2020

Page 79: Database System Internals - courses.cs.washington.edu

Discussion

§What does B(R) <= M2 mean?§How large can R be?

§ Example:• Page size = 32KB• Memory size 32GB: M = 106 pages

§ R can be as large as 1012 pages• 32 × 1015 Bytes = 32 PB

CSE 444 - Spring 2020 83April 15, 2020

Page 80: Database System Internals - courses.cs.washington.edu

Merge-Join

Join R ⨝ S§How?....

CSE 444 - Spring 2020 84April 15, 2020

Page 81: Database System Internals - courses.cs.washington.edu

Merge-Join

Join R ⨝ S§Step 1a: generate initial runs for R§Step 1b: generate initial runs for S§Step 2: merge and join

• Either merge first and then join• Or merge & join at the same time

CSE 444 - Spring 2020 85April 15, 2020

Page 82: Database System Internals - courses.cs.washington.edu

4 1

5 2

R3 0

S

1 7

8 6

3 4 4 3

2 5

Disk

7 9

12 14

2 3

5 11

9 8

11 9

12 1

5 7

11 9

1 7

Memory M = 5 pages

CSE 444 - Spring 2020 86

Merge-Join ExampleSetup: Want to join R and SRelation R has 10 pages with 2 tuples per pageRelation S has 8 pages with 2 tuples per pageValues shown are values of join attribute for each given tuple

April 15, 2020

Page 83: Database System Internals - courses.cs.washington.edu

Step 1: Read M pages of R and sort in memory

4 1

5 2

R3 0

S

1 7

8 6

3 4 4 3

2 5

Disk

7 9

12 14

2 3

5 11

9 8

11 9

12 1

5 7

11 9

1 7

Memory M = 5 pages

4 1

5 2

8 6

3 4

7 9

CSE 444 - Spring 2020 87

Merge-Join Example

April 15, 2020

Page 84: Database System Internals - courses.cs.washington.edu

4 1

5 2

R3 0

S

1 7

8 6

3 4 4 3

2 5

Disk

7 9

12 14

2 3

5 11

9 8

11 9

12 1

5 7

11 9

1 7

Memory M = 5 pages

1 2

3 4

6 7

4 5

8 9

Run 1 of R

CSE 444 - Spring 2020 88

Step 1: Read M pages of R and sort in memory, then write to disk

Merge-Join Example

April 15, 2020

Page 85: Database System Internals - courses.cs.washington.edu

4 1

5 2

R3 0

S

1 7

8 6

3 4 4 3

2 5

Disk

7 9

12 14

2 3

5 11

9 8

11 9

12 1

5 7

11 9

1 7

Memory M = 5 pages

1 2

3 4

6 7

4 5

8 9

Run 1 of R1 2

3 5

11 11

7 9

12 14

Run 2 of R

CSE 444 - Spring 2020 89

Step 1: Repeat for next M pages until all R is processed

Merge-Join Example

April 15, 2020

Page 86: Database System Internals - courses.cs.washington.edu

4 1

5 2

R3 0

S

1 7

8 6

3 4 4 3

2 5

Disk

7 9

12 14

2 3

5 11

9 8

11 9

12 1

5 7

11 9

1 7

Memory M = 5 pages

0 1

2 3

5 7

3 4

8 9

Run 1 of S1 5

7 9

11 12

Run 2 of S

CSE 444 - Spring 2020 90

Step 1: Do the same with S

Merge-Join Example

April 15, 2020

Page 87: Database System Internals - courses.cs.washington.edu

Step 2: Join while merging sorted runs

Memory M = 5 pages

0 1

2 3

5 7

3 4

8 9

Run 1 of S1 5

7 9

11 12

Run 2 of S

1 2

3 4

6 7

4 5

8 9

Run 1 of R1 2

3 5

11 11

7 9

12 14

Run 2 of R

1 2

1 2

0 1

1 5

Outputbuffer

Input buffers

Run1

Run2

Run1

Run2

Total cost: 3B(R) + 3B(S)

CSE 444 - Spring 2020 91

Merge-Join Example

Step 2: Join while mergingOutput tuples

April 15, 2020

Page 88: Database System Internals - courses.cs.washington.edu

Step 2: Join while merging sorted runs

Memory M = 5 pages

0 1

2 3

5 7

3 4

8 9

Run 1 of S1 5

7 9

11 12

Run 2 of S

1 2

3 4

6 7

4 5

8 9

Run 1 of R1 2

3 5

11 11

7 9

12 14

Run 2 of R

1 2

1 2

0 1

1 5

Outputbuffer

Input buffers

Run1

Run2

Run1

Run2

Total cost: 3B(R) + 3B(S)

CSE 444 - Spring 2020 93

Merge-Join Example

Step 2: Join while mergingOutput tuples

April 15, 2020

Page 89: Database System Internals - courses.cs.washington.edu

Step 2: Join while merging sorted runs

Memory M = 5 pages

0 1

2 3

5 7

3 4

8 9

Run 1 of S1 5

7 9

11 12

Run 2 of S

1 2

3 4

6 7

4 5

8 9

Run 1 of R1 2

3 5

11 11

7 9

12 14

Run 2 of R

1 2

1 2

0 1

1 5

Outputbuffer

Input buffers

Run1

Run2

Run1

Run2

Total cost: 3B(R) + 3B(S)

CSE 444 - Spring 2020 94

Merge-Join Example

Step 2: Join while mergingOutput tuples(1,1)(1,1)(1,1)(1,1)

April 15, 2020

Page 90: Database System Internals - courses.cs.washington.edu

Step 2: Join while merging sorted runs

Memory M = 5 pages

0 1

2 3

5 7

3 4

8 9

Run 1 of S1 5

7 9

11 12

Run 2 of S

1 2

3 4

6 7

4 5

8 9

Run 1 of R1 2

3 5

11 11

7 9

12 14

Run 2 of R

1 2

1 2

2 3

1 5

Outputbuffer

Input buffers

Run1

Run2

Run1

Run2

Total cost: 3B(R) + 3B(S)

CSE 444 - Spring 2020 95

Merge-Join Example

Step 2: Join while mergingOutput tuples(1,1)(1,1)(1,1)(1,1)

April 15, 2020

Page 91: Database System Internals - courses.cs.washington.edu

Step 2: Join while merging sorted runs

Memory M = 5 pages

0 1

2 3

5 7

3 4

8 9

Run 1 of S1 5

7 9

11 12

Run 2 of S

1 2

3 4

6 7

4 5

8 9

Run 1 of R1 2

3 5

11 11

7 9

12 14

Run 2 of R

1 2

1 2

2 3

1 5

Outputbuffer

Input buffers

Run1

Run2

Run1

Run2

Total cost: 3B(R) + 3B(S)

CSE 444 - Spring 2020 96

Merge-Join Example

Step 2: Join while mergingOutput tuples(1,1)(1,1)(1,1)(1,1)(2,2)(2,2)

April 15, 2020

Page 92: Database System Internals - courses.cs.washington.edu

Step 2: Join while merging sorted runs

Memory M = 5 pages

0 1

2 3

5 7

3 4

8 9

Run 1 of S1 5

7 9

11 12

Run 2 of S

1 2

3 4

6 7

4 5

8 9

Run 1 of R1 2

3 5

11 11

7 9

12 14

Run 2 of R

3 4

3 5

2 3

1 5

Outputbuffer

Input buffers

Run1

Run2

Run1

Run2

Total cost: 3B(R) + 3B(S)

CSE 444 - Spring 2020 97

Merge-Join Example

Step 2: Join while mergingOutput tuples(1,1)(1,1)(1,1)(1,1)(2,2)(2,2)(3,3)(3,3)…

April 15, 2020

Page 93: Database System Internals - courses.cs.washington.edu

Merge-Join

Main memoryDiskDisk

. . .. . .Input M

Input 1

Input 2. . . .

Output

M1 = B(R)/M runs for RM2 = B(S)/M runs for SMerge-join M1 + M2 runs; need M1 + M2 <= M to process all runs

i.e. B(R) + B(S) <= M2

98CSE 444 - Spring 2020April 15, 2020