ADVANCED DATABASE SYSTEMS Parallel Join Algorithms (Sorting) @ - - PowerPoint PPT Presentation

advanced
SMART_READER_LITE
LIVE PREVIEW

ADVANCED DATABASE SYSTEMS Parallel Join Algorithms (Sorting) @ - - PowerPoint PPT Presentation

Lect ure # 18 ADVANCED DATABASE SYSTEMS Parallel Join Algorithms (Sorting) @ Andy_Pavlo // 15- 721 // Spring 2020 2 PRO J ECT # 2 This Week Status Meetings Wednesday April 8 th Code Review Submission Update Presentation


slide-1
SLIDE 1

Lect ure # 18

Parallel Join Algorithms (Sorting)

@ Andy_Pavlo // 15- 721 // Spring 2020

ADVANCED DATABASE SYSTEMS

slide-2
SLIDE 2

15-721 (Spring 2020)

PRO J ECT # 2

This Week

→ Status Meetings

Wednesday April 8th

→ Code Review Submission → Update Presentation → Design Document

2

slide-3
SLIDE 3

15-721 (Spring 2020)

PARALLEL J O IN ALGO RITH M S

Perform a join between two relations on multiple threads simultaneously to speed up operation. Two main approaches:

→ Hash Join → Sort-Merge Join

3

slide-4
SLIDE 4

15-721 (Spring 2020)

Background Sorting Algorithms Parallel Sort-Merge Join Evaluation

4

slide-5
SLIDE 5

15-721 (Spring 2020)

SO RT- M ERGE J O IN (R⨝S)

Phase #1: Sort

→ Sort the tuples of R and S based on the join key.

Phase #2: Merge

→ Scan the sorted relations and compare tuples. → The outer relation R only needs to be scanned once.

5

slide-6
SLIDE 6

15-721 (Spring 2020)

SO RT- M ERGE J O IN (R⨝S)

6

Relation R Relation S

slide-7
SLIDE 7

15-721 (Spring 2020)

SO RT- M ERGE J O IN (R⨝S)

6

Relation R Relation S SORT! SORT!

slide-8
SLIDE 8

15-721 (Spring 2020)

SO RT- M ERGE J O IN (R⨝S)

6

Relation R Relation S

SORT! SORT! MERGE!

slide-9
SLIDE 9

15-721 (Spring 2020)

SO RT- M ERGE J O IN (R⨝S)

6

Relation R Relation S

SORT! SORT! MERGE!

slide-10
SLIDE 10

15-721 (Spring 2020)

PARALLEL SO RT- M ERGE J O IN S

Sorting is the most expensive part. Use hardware correctly to speed up the join algorithm as much as possible.

→ Utilize as many CPU cores as possible. → Be mindful of NUMA boundaries. → Use SIMD instructions where applicable.

7

MULTI- CORE, MAIN- MEMORY JOINS: SORT VS. HASH REVISITED

VLDB 20 13

slide-11
SLIDE 11

15-721 (Spring 2020)

PARALLEL SO RT- M ERGE J O IN (R⨝S)

Phase #1: Partitioning (optional)

→ Partition R and assign them to workers / cores.

Phase #2: Sort

→ Sort the tuples of R and S based on the join key.

Phase #3: Merge

→ Scan the sorted relations and compare tuples. → The outer relation R only needs to be scanned once.

8

slide-12
SLIDE 12

15-721 (Spring 2020)

PARTITIO N IN G PH ASE

Approach #1: Implicit Partitioning

→ The data was partitioned on the join key when it was loaded into the database. → No extra pass over the data is needed.

Approach #2: Explicit Partitioning

→ Divide only the outer relation and redistribute among the different CPU cores. → Can use the same radix partitioning approach we talked about last time.

9

slide-13
SLIDE 13

15-721 (Spring 2020)

SO RT PH ASE

Create runs of sorted chunks of tuples for both input relations. It used to be that Quicksort was good enough and it usually still is. We can explore other methods that try to take advantage of NUMA and parallel architectures …

10

slide-14
SLIDE 14

15-721 (Spring 2020)

CACH E- CO N SCIO US SO RTIN G

Level #1: In-Register Sorting

→ Sort runs that fit into CPU registers.

Level #2: In-Cache Sorting

→ Merge Level #1 output into runs that fit into CPU caches. → Repeat until sorted runs are ½ cache size.

Level #3: Out-of-Cache Sorting

→ Used when the runs of Level #2 exceed the size of caches.

11

SORT VS. HASH REVISITED: FAST JOIN IMPLEMENTATION ON MODERN M MULTI- CORE C CPUS

VLDB 20 0 9

slide-15
SLIDE 15

15-721 (Spring 2020)

CACH E- CO N SCIO US SO RTIN G

12

Level #1 Level #2 Level #3

SORTED UNSORTED

slide-16
SLIDE 16

15-721 (Spring 2020)

LEVEL # 1 SO RTIN G N ETWO RKS

Abstract model for sorting keys.

→ Fixed wiring “paths” for lists with the same # of elements. → Efficient to execute on modern CPUs because of limited data dependencies and no branches.

13

9 5 3 6

Input Output

slide-17
SLIDE 17

15-721 (Spring 2020)

LEVEL # 1 SO RTIN G N ETWO RKS

Abstract model for sorting keys.

→ Fixed wiring “paths” for lists with the same # of elements. → Efficient to execute on modern CPUs because of limited data dependencies and no branches.

13

9 5 3 6 3 6 5 9

Input Output

slide-18
SLIDE 18

15-721 (Spring 2020)

LEVEL # 1 SO RTIN G N ETWO RKS

Abstract model for sorting keys.

→ Fixed wiring “paths” for lists with the same # of elements. → Efficient to execute on modern CPUs because of limited data dependencies and no branches.

13

9 5 3 6 3 6 5 9 5 3

Input Output

slide-19
SLIDE 19

15-721 (Spring 2020)

LEVEL # 1 SO RTIN G N ETWO RKS

Abstract model for sorting keys.

→ Fixed wiring “paths” for lists with the same # of elements. → Efficient to execute on modern CPUs because of limited data dependencies and no branches.

13

9 5 3 6 3 6 5 9 5 3

Input Output

3

slide-20
SLIDE 20

15-721 (Spring 2020)

LEVEL # 1 SO RTIN G N ETWO RKS

Abstract model for sorting keys.

→ Fixed wiring “paths” for lists with the same # of elements. → Efficient to execute on modern CPUs because of limited data dependencies and no branches.

13

9 5 3 6 3 6 5 9 9 6 5 3

Input Output

3 9

slide-21
SLIDE 21

15-721 (Spring 2020)

LEVEL # 1 SO RTIN G N ETWO RKS

Abstract model for sorting keys.

→ Fixed wiring “paths” for lists with the same # of elements. → Efficient to execute on modern CPUs because of limited data dependencies and no branches.

13

9 5 3 6 3 6 5 9 9 6 5 3 5 6

Input Output

3 5 6 9

slide-22
SLIDE 22

15-721 (Spring 2020)

LEVEL # 1 SO RTIN G N ETWO RKS

Abstract model for sorting keys.

→ Fixed wiring “paths” for lists with the same # of elements. → Efficient to execute on modern CPUs because of limited data dependencies and no branches.

13

9 5 3 6 3 6 5 9 9 6 5 3 5 6

Input Output

3 5 6 9

wires = [9,5,3,6] wires[0] = min(wires[0], wires[1]) wires[1] = max(wires[0], wires[1]) wires[2] = min(wires[2], wires[3]) wires[3] = max(wires[2], wires[3]) wires[0] = min(wires[0], wires[2]) wires[2] = max(wires[0], wires[2]) wires[1] = min(wires[1], wires[3]) wires[3] = max(wires[1], wires[3]) wires[1] = min(wires[1], wires[2]) wires[2] = max(wires[1], wires[2])

slide-23
SLIDE 23

15-721 (Spring 2020)

LEVEL # 1 SO RTIN G N ETWO RKS

Abstract model for sorting keys.

→ Fixed wiring “paths” for lists with the same # of elements. → Efficient to execute on modern CPUs because of limited data dependencies and no branches.

13

9 5 3 6 3 6 5 9 9 6 5 3 5 6

Input Output

3 5 6 9

wires = [9,5,3,6] wires[0] = min(wires[0], wires[1]) wires[1] = max(wires[0], wires[1]) wires[2] = min(wires[2], wires[3]) wires[3] = max(wires[2], wires[3]) wires[0] = min(wires[0], wires[2]) wires[2] = max(wires[0], wires[2]) wires[1] = min(wires[1], wires[3]) wires[3] = max(wires[1], wires[3]) wires[1] = min(wires[1], wires[2]) wires[2] = max(wires[1], wires[2])

1 1 2 2 3 3

slide-24
SLIDE 24

15-721 (Spring 2020)

LEVEL # 1 SO RTIN G N ETWO RKS

14

12 21 4 13 9 8 6 7 1 14 3 5 11 15 10

<64-bit Join Key, 64-bit Tuple Pointer>

slide-25
SLIDE 25

15-721 (Spring 2020)

LEVEL # 1 SO RTIN G N ETWO RKS

14

12 21 4 13 9 8 6 7 1 14 3 5 11 15 10

Instructions:

→ 4 LOAD

slide-26
SLIDE 26

15-721 (Spring 2020)

LEVEL # 1 SO RTIN G N ETWO RKS

14

12 21 4 13 9 8 6 7 1 14 3 5 11 15 10

Sort Across Registers Instructions:

→ 4 LOAD

slide-27
SLIDE 27

15-721 (Spring 2020)

LEVEL # 1 SO RTIN G N ETWO RKS

14

12 21 4 13 9 8 6 7 1 14 3 5 11 15 10 1 8 3 5 11 4 7 9 14 6 10 12 21 15 13

Sort Across Registers Instructions:

→ 4 LOAD

Instructions:

→ 10 MIN/MAX

slide-28
SLIDE 28

15-721 (Spring 2020)

LEVEL # 1 SO RTIN G N ETWO RKS

14

12 21 4 13 9 8 6 7 1 14 3 5 11 15 10 1 8 3 5 11 4 7 9 14 6 10 12 21 15 13 1 5 9 12 8 11 14 21 3 4 6 15 7 10 13

Sort Across Registers Transpose Registers Instructions:

→ 4 LOAD

Instructions:

→ 10 MIN/MAX

Instructions:

→ 8 SHUFFLE → 4 STORE

slide-29
SLIDE 29

15-721 (Spring 2020)

LEVEL # 2 BITO N IC M ERGE N ETWO RK

Like a Sorting Network but it can merge two locally-sorted lists into a globally-sorted list. Can expand network to merge progressively larger lists up to ½ LLC size. Intel’s Measurements

→ 2.25–3.5× speed-up over SISD implementation.

15

EFFICIENT IMPLEMENTATION OF SORTING ON MULTI- CORE

VLDB 20 0 8

slide-30
SLIDE 30

15-721 (Spring 2020)

LEVEL # 2 BITO N IC M ERGE N ETWO RK

16

Input Output

b4 b3 b2 b1

Sorted Run Reverse Sorted Run

a1 a2 a3 a4

S H U F F L E S H U F F L E

Sorted Run

min/max min/max min/max

slide-31
SLIDE 31

15-721 (Spring 2020)

LEVEL # 3 M ULTI- WAY M ERGIN G

Use the Bitonic Merge Networks but split the process up into tasks.

→ Still one worker thread per core. → Link together tasks with a cache-sized FIFO queue.

A task blocks when either its input queue is empty,

  • r its output queue is full.

Requires more CPU instructions but brings bandwidth and compute into balance.

17

slide-32
SLIDE 32

15-721 (Spring 2020)

Sorted Runs

LEVEL # 3 M ULTI- WAY M ERGIN G

18

MERGE MERGE MERGE MERGE MERGE MERGE MERGE

Cache-Sized Queue

slide-33
SLIDE 33

15-721 (Spring 2020)

IN- PLACE SUPERSCALAR SAM PLESO RT

Recursively partition the table by sampling keys to determine partition boundaries. It copies data into output buffers during the partitioning phases. But when a buffer gets full, it writes it back into portions of the input array already distributed instead of allocating a new buffer.

19

IN IN- PLACE PARALLEL S SUPER SCALAR SAMPLESORT

ESA 20 17

slide-34
SLIDE 34

15-721 (Spring 2020)

M ERGE PH ASE

Iterate through the outer table and inner table in lockstep and compare join keys. May need to backtrack if there are duplicates. Can be done in parallel at the different cores without synchronization if there are separate

  • utput buffers.

20

slide-35
SLIDE 35

15-721 (Spring 2020)

SO RT- M ERGE J O IN VARIAN TS

Multi-Way Sort-Merge (M-WAY) Multi-Pass Sort-Merge (M-PASS) Massively Parallel Sort-Merge (MPSM)

21

slide-36
SLIDE 36

15-721 (Spring 2020)

M ULTI- WAY SO RT- M ERGE

Outer Table

→ Each core sorts in parallel on local data (levels #1/#2). → Redistribute sorted runs across cores using the multi- way merge (level #3).

Inner Table

→ Same as outer table.

Merge phase is between matching pairs of chunks

  • f outer/inner tables at each core.

22

MULTI- CORE, MAIN- MEMORY JOINS: SORT VS. HASH REVISITED

VLDB 20 13

slide-37
SLIDE 37

15-721 (Spring 2020)

M ULTI- WAY SO RT- M ERGE

23

Local-NUMA Partitioning

slide-38
SLIDE 38

15-721 (Spring 2020)

M ULTI- WAY SO RT- M ERGE

23

Local-NUMA Partitioning Sort

slide-39
SLIDE 39

15-721 (Spring 2020)

M ULTI- WAY SO RT- M ERGE

23

Local-NUMA Partitioning Sort Multi-Way Merge

slide-40
SLIDE 40

15-721 (Spring 2020)

M ULTI- WAY SO RT- M ERGE

23

Local-NUMA Partitioning Sort Multi-Way Merge

slide-41
SLIDE 41

15-721 (Spring 2020)

M ULTI- WAY SO RT- M ERGE

23

Local-NUMA Partitioning Sort Multi-Way Merge

slide-42
SLIDE 42

15-721 (Spring 2020)

M ULTI- WAY SO RT- M ERGE

23

SORT! SORT! SORT! SORT!

Local-NUMA Partitioning Sort Multi-Way Merge Same steps as Outer Table

slide-43
SLIDE 43

15-721 (Spring 2020)

M ULTI- WAY SO RT- M ERGE

23

SORT! SORT! SORT! SORT!

⨝ ⨝ ⨝ ⨝

Local-NUMA Partitioning Sort Multi-Way Merge Local Merge Join Same steps as Outer Table

slide-44
SLIDE 44

15-721 (Spring 2020)

M ULTI- PASS SO RT- M ERGE

Outer Table

→ Same level #1/#2 sorting as Multi-Way. → But instead of redistributing, it uses a multi-pass naïve merge on sorted runs.

Inner Table

→ Same as outer table.

Merge phase is between matching pairs of chunks

  • f outer table and inner table.

24

MULTI- CORE, MAIN- MEMORY JOINS: SORT VS. HASH REVISITED

VLDB 20 13

slide-45
SLIDE 45

15-721 (Spring 2020)

M ULTI- PASS SO RT- M ERGE

25

Local-NUMA Partitioning Local-NUMA Partitioning

slide-46
SLIDE 46

15-721 (Spring 2020)

M ULTI- PASS SO RT- M ERGE

25

Local-NUMA Partitioning Sort Local-NUMA Partitioning Sort

slide-47
SLIDE 47

15-721 (Spring 2020)

M ULTI- PASS SO RT- M ERGE

25

Local-NUMA Partitioning Sort Global Merge Join

Local-NUMA Partitioning Sort

slide-48
SLIDE 48

15-721 (Spring 2020)

M ULTI- PASS SO RT- M ERGE

25

Local-NUMA Partitioning Sort Global Merge Join

Local-NUMA Partitioning Sort

slide-49
SLIDE 49

15-721 (Spring 2020)

M ASSIVELY PARALLEL SO RT- M ERGE

Outer Table

→ Range-partition outer table and redistribute to cores. → Each core sorts in parallel on their partitions.

Inner Table

→ Not redistributed like outer table. → Each core sorts its local data.

Merge phase is between entire sorted run of outer table and a segment of inner table.

26

MASSIVELY PARALLEL S SORT- MERGE JOINS IN MAIN MEMORY M MULTI- CORE D DATABASE SYSTEMS

VLDB 20 12

slide-50
SLIDE 50

15-721 (Spring 2020)

M ASSIVELY PARALLEL SO RT- M ERGE

27

Cross-NUMA Partitioning

slide-51
SLIDE 51

15-721 (Spring 2020)

M ASSIVELY PARALLEL SO RT- M ERGE

27

Cross-NUMA Partitioning Sort Globally Sorted

slide-52
SLIDE 52

15-721 (Spring 2020)

M ASSIVELY PARALLEL SO RT- M ERGE

27

Cross-NUMA Partitioning Sort

slide-53
SLIDE 53

15-721 (Spring 2020)

M ASSIVELY PARALLEL SO RT- M ERGE

27

SORT! SORT! SORT! SORT!

Cross-NUMA Partitioning Sort

slide-54
SLIDE 54

15-721 (Spring 2020)

M ASSIVELY PARALLEL SO RT- M ERGE

27

SORT! SORT! SORT! SORT!

Cross-NUMA Partitioning Sort Cross-Partition Merge Join

slide-55
SLIDE 55

15-721 (Spring 2020)

M ASSIVELY PARALLEL SO RT- M ERGE

27

SORT! SORT! SORT! SORT!

⨝ ⨝

Cross-NUMA Partitioning Sort Cross-Partition Merge Join

slide-56
SLIDE 56

15-721 (Spring 2020)

M ASSIVELY PARALLEL SO RT- M ERGE

27

SORT! SORT! SORT! SORT!

⨝ ⨝ ⨝ ⨝

Cross-NUMA Partitioning Sort Cross-Partition Merge Join

slide-57
SLIDE 57

15-721 (Spring 2020)

H YPER's RULES FO R PARALLELIZATIO N

Rule #1: No random writes to non-local memory

→ Chunk the data, redistribute, and then each core sorts/works on local data.

Rule #2: Only perform sequential reads on non-local memory

→ This allows the hardware prefetcher to hide remote access latency.

Rule #3: No core should ever wait for another

→ Avoid fine-grained latching or sync barriers.

28

Source: Martina- Cezara Albutiu

slide-58
SLIDE 58

15-721 (Spring 2020)

EVALUATIO N

Compare the different join algorithms using a synthetic data set.

→ Sort-Merge: M-WAY, M-PASS, MPSM → Hash: Radix Partitioning

Hardware:

→ 4 Socket Intel Xeon E4640 @ 2.4GHz → 8 Cores with 2 Threads Per Core → 512 GB of DRAM

29

MULTI- CORE, MAIN- MEMORY JOINS: SORT VS. HASH REVISITED

VLDB 20 13

slide-59
SLIDE 59

15-721 (Spring 2020)

RAW SO RTIN G PERFO RM AN CE

30

9 18 27 36 1 2 4 8 16 32 64 128 256

Throughput (M Tuples/sec) Number of Tuples (in 220)

C++ STL Sort SIMD Sort

Source: Cagri Balkesen

Single-threaded sorting performance

2.5–3x Faster

slide-60
SLIDE 60

15-721 (Spring 2020)

CO M PARISO N O F SO RT- M ERGE J O IN S

31

100 200 300 400

5 10 15 20 25

M-WAY M-PASS MPSM

Throughput (M Tuples/sec) Cycles / Output Tuple

Partition Sort S-Merge J-Merge Throughput

13.6

Source: Cagri Balkesen

Workload: 1.6B⋈ 128M (8-byte tuples)

7.6 22.9

slide-61
SLIDE 61

15-721 (Spring 2020)

Hyper- Threading

M - WAY J O IN VS. M PSM J O IN

32

100 200 300 400 1 2 4 8 16 32 64

Throughput (M Tuples/sec) Number of Threads

Multi-Way Massively Parallel 108 M/sec 315 M/sec

Source: Cagri Balkesen

Workload: 1.6B⋈ 128M (8-byte tuples)

130 M/sec 54 M/sec 259 M/sec 90 M/sec

slide-62
SLIDE 62

15-721 (Spring 2020)

SO RT- M ERGE J O IN VS. H ASH J O IN

33

2 4 6 8

SORT HASH SORT HASH SORT HASH SORT HASH 128M⨝128M 1.6B⨝1.6B 128M⨝512M 1.6B⨝6.4B

Cycles / Output Tuple

Partition Sort S-Merge J-Merge Build+Probe

Source: Cagri Balkesen

Workload: Different Table Sizes (8-byte tuples)

slide-63
SLIDE 63

15-721 (Spring 2020)

SO RT- M ERGE J O IN VS. H ASH J O IN

34

150 300 450 600 750 128 256 384 512 768 1024 1280 1536 1792 1920

Throughput (M Tuples/sec) Millions of Tuples

Multi-Way Sort-Merge Join Radix Hash Join

Source: Cagri Balkesen

Varying the size of the input relations

slide-64
SLIDE 64

15-721 (Spring 2020)

PARTIN G TH O UGH TS

Both join approaches are equally important. Every serious OLAP DBMS supports both. We did not consider the impact of queries where the output needs to be sorted.

35

slide-65
SLIDE 65

15-721 (Spring 2020)

N EXT CLASS

Optimizers – The Hardest Topic in Databases

36