Top Banner
Lecture #17 Parallel Join Algorithms (Hashing) @Andy_Pavlo // 15-721 // Spring 2020 ADVANCED DATABASE SYSTEMS
123

7 ADVANCED DATABASE SYSTEMS - CMU 15-721

Dec 23, 2021

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

Le

ctu

re #

17

Parallel Join Algorithms(Hashing)@Andy_Pavlo // 15-721 // Spring 2020

ADVANCEDDATABASE SYSTEMS

Page 2: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

Background

Parallel Hash Join

Hash Functions

Hashing Schemes

Evaluation

2

Page 3: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

PARALLEL JOIN ALGORITHMS

Perform a join between two relations on multiple threads simultaneously to speed up operation.

Two main approaches:→ Hash Join→ Sort-Merge Join

We won't discuss nested-loop joins…

3

Page 4: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

OBSERVATION

Many OLTP DBMSs do not implement hash join.

But an index nested-loop join with a small number of target tuples is at a high-level equivalent to a hash join.

4

Page 5: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

HASHING VS. SORTING

1970s – Sorting

1980s – Hashing

1990s – Equivalent

2000s – Hashing

2010s – Hashing (Partitioned vs. Non-Partitioned)

2020s – ???

5

Page 6: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

PARALLEL JOIN ALGORITHMS

6

→ Hashing is faster than Sort-Merge.→ Sort-Merge is faster w/ wider SIMD.

SORT VS. HASH REVISITED: FAST JOIN IMPLEMENTATION ON MODERN MULTI-CORE CPUSVLDB 2009

→ Sort-Merge is already faster than Hashing, even without SIMD.

MASSIVELY PARALLEL SORT-MERGE JOINS IN MAIN MEMORY MULTI-CORE DATABASE SYSTEMSVLDB 2012

→ New optimizations and results for Radix Hash Join.

MAIN-MEMORY HASH JOINS ON MULTI-CORE CPUS: TUNING TO THE UNDERLYING HARDWAREICDE 2013

→ Trade-offs between partitioning & non-partitioning Hash-Join.

DESIGN AND EVALUATION OF MAIN MEMORY HASH JOIN ALGORITHMS FOR MULTI-CORE CPUSSIGMOD 2011

→ Ignore what we said last year.→ You really want to use Hashing!

MASSIVELY PARALLEL NUMA-AWARE HASH JOINSIMDM 2013

→ Hold up everyone! Let's look at everything more carefully!

AN EXPERIMENTAL COMPARISON OF THIRTEEN RELATIONAL EQUI-JOINS IN MAIN MEMORYSIGMOD 2016

Page 7: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

JOIN ALGORITHM DESIGN GOALS

Goal #1: Minimize Synchronization→ Avoid taking latches during execution.

Goal #2: Minimize Memory Access Cost→ Ensure that data is always local to worker thread.→ Reuse data while it exists in CPU cache.

7

Page 8: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

IMPROVING CACHE BEHAVIOR

Factors that affect cache misses in a DBMS:→ Cache + TLB capacity.→ Locality (temporal and spatial).

Non-Random Access (Scan):→ Clustering data to a cache line.→ Execute more operations per cache line.

Random Access (Lookups):→ Partition data to fit in cache + TLB.

8

Source: Johannes Gehrke

Page 9: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

PARALLEL HASH JOINS

Hash join is the most important operator in a DBMS for OLAP workloads.

It is important that we speed up our DBMS's join algorithm by taking advantage of multiple cores.→ We want to keep all cores busy, without becoming

memory bound.

9

Page 10: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

HASH JOIN (R⨝S)

Phase #1: Partition (optional)→ Divide the tuples of R and S into sets using a hash on the

join key.

Phase #2: Build→ Scan relation R and create a hash table on join key.

Phase #3: Probe→ For each tuple in S, look up its join key in hash table for

R. If a match is found, output combined tuple.

10

AN EXPERIMENTAL COMPARISON OF THIRTEEN RELATIONAL EQUI-JOINS IN MAIN MEMORYSIGMOD 2016

Page 11: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

PARTITION PHASE

Split the input relations into partitioned buffers by hashing the tuples’ join key(s).→ Ideally the cost of partitioning is less than the cost of

cache misses during build phase.→ Sometimes called hybrid hash join / radix hash join.

Contents of buffers depends on storage model:→ NSM: Usually the entire tuple.→ DSM: Only the columns needed for the join + offset.

11

Page 12: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

PARTITION PHASE

Approach #1: Non-Blocking Partitioning→ Only scan the input relation once.→ Produce output incrementally.

Approach #2: Blocking Partitioning (Radix)→ Scan the input relation multiple times.→ Only materialize results all at once.→ Sometimes called radix hash join.

12

Page 13: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

NON-BLOCKING PARTITIONING

Scan the input relation only once and generate the output on-the-fly.

Approach #1: Shared Partitions→ Single global set of partitions that all threads update.→ Must use a latch to synchronize threads.

Approach #2: Private Partitions→ Each thread has its own set of partitions.→ Must consolidate them after all threads finish.

13

Page 14: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

SHARED PARTITIONS

14

Data Table

A B ChashP(key)

#p

#p

#p

Page 15: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

Partitions

SHARED PARTITIONS

14

Data Table

A B ChashP(key)

P1

P2

Pn

#p

#p

#p

Page 16: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

Partitions

PRIVATE PARTITIONS

15

Data Table

A B ChashP(key)

#p

#p

#p

Page 17: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

Partitions

PRIVATE PARTITIONS

15

Data Table

A B ChashP(key)

#p

#p

#p

Page 18: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

Partitions

PRIVATE PARTITIONS

15

Data Table

A B ChashP(key)

#p

#p

#p

Combined

P1

P2

Pn

Page 19: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

Partitions

PRIVATE PARTITIONS

15

Data Table

A B ChashP(key)

#p

#p

#p

Combined

P1

P2

Pn

Page 20: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

RADIX PARTITIONING

Scan the input relation multiple times to generate the partitions.

Multi-step pass over the relation:→ Step #1: Scan R and compute a histogram of the # of

tuples per hash key for the radix at some offset.→ Step #2: Use this histogram to determine output offsets

by computing the prefix sum.→ Step #3: Scan R again and partition them according to the

hash key.

16

Page 21: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

RADIX

The radix of a key is the value of an integer at a position (using its base).

17

89 12 23 08 41 64Keys

Page 22: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

RADIX

The radix of a key is the value of an integer at a position (using its base).

17

89 12 23 08 41 64

9 2 3 8 1 4

Keys

Radix

Page 23: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

RADIX

The radix of a key is the value of an integer at a position (using its base).

17

89 12 23 08 41 64Keys

Radix 8 1 2 0 4 6

Page 24: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

PREFIX SUM

The prefix sum of a sequence of numbers(x0, x1, …, xn)

is a second sequence of numbers(y0, y1, …, yn)

that is a running total of the input sequence.

18

+ + + + +

1 2 3 4 5 6

1 3 6 10 15 21

Input

Prefix Sum

Page 25: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

RADIX PARTITIONS

19

Step #1: Inspect input, create histograms

0 7

1 8

1 9

0 7

0 3

1 1

1 5

1 0

0

1

#p

#p

#p

#p

#p

#p

#p

#p

hash

P(k

ey)

Page 26: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

RADIX PARTITIONS

19

Step #1: Inspect input, create histograms

0 7

1 8

1 9

0 7

0 3

1 1

1 5

1 0

0

1

#p

#p

#p

#p

#p

#p

#p

#p

hash

P(k

ey)

Page 27: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

RADIX PARTITIONS

19

Step #1: Inspect input, create histograms

Partition 0: 2Partition 1: 2

Partition 0: 1Partition 1: 3

0 7

1 8

1 9

0 7

0 3

1 1

1 5

1 0

0

1

#p

#p

#p

#p

#p

#p

#p

#p

hash

P(k

ey)

Page 28: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

RADIX PARTITIONS

19

Partition 0: 2Partition 1: 2

Partition 0: 1Partition 1: 3

Partition 0

Partition 0, CPU 1

Partition 1

Partition 1, CPU 1

Step #2: Compute output offsets

, CPU 0

, CPU 0

0 7

1 8

1 9

0 7

0 3

1 1

1 5

1 0

0

1

#p

#p

#p

#p

#p

#p

#p

#p

hash

P(k

ey)

Page 29: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

RADIX PARTITIONS

19

Partition 0: 2Partition 1: 2

Partition 0: 1Partition 1: 3

Partition 0

Partition 0, CPU 1

Partition 1

Partition 1, CPU 1

Step #2: Compute output offsets

, CPU 0

, CPU 0

0 7

1 8

1 9

0 7

0 3

1 1

1 5

1 0

0

1

#p

#p

#p

#p

#p

#p

#p

#p

hash

P(k

ey)

Page 30: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

RADIX PARTITIONS

19

Partition 0: 2Partition 1: 2

Partition 0: 1Partition 1: 3

Partition 0

Partition 0, CPU 1

Partition 1

Partition 1, CPU 1

Step #3: Read inputand partition

0 7

0 3

, CPU 0

, CPU 0

0 7

1 8

1 9

0 7

0 3

1 1

1 5

1 0

0

1

#p

#p

#p

#p

#p

#p

#p

#p

hash

P(k

ey)

Page 31: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

RADIX PARTITIONS

19

Partition 0: 2Partition 1: 2

Partition 0: 1Partition 1: 3

Partition 0

Partition 0, CPU 1

Partition 1

Partition 1, CPU 1

Step #3: Read inputand partition

0 7

0 7

0 3

1 8

1 9

1 1

1 5

1 0

, CPU 0

, CPU 0

0 7

1 8

1 9

0 7

0 3

1 1

1 5

1 0

0

1

#p

#p

#p

#p

#p

#p

#p

#p

hash

P(k

ey)

Page 32: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

RADIX PARTITIONS

19

Partition 0: 2Partition 1: 2

Partition 0: 1Partition 1: 3

Partition 0

Partition 1

0 7

0 7

0 3

1 8

1 9

1 1

1 5

1 0

Recursively repeat until target number of partitions have been created

0 7

1 8

1 9

0 7

0 3

1 1

1 5

1 0

0

1

#p

#p

#p

#p

#p

#p

#p

#p

hash

P(k

ey)

Page 33: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

RADIX PARTITIONS

19

Partition 0: 2Partition 1: 2

Partition 0: 1Partition 1: 3

0 7

0 7

0 3

1 8

1 9

1 1

1 5

1 0

Recursively repeat until target number of partitions have been created

0 7

1 8

1 9

0 7

0 3

1 1

1 5

1 0

0

1

#p

#p

#p

#p

#p

#p

#p

#p

hash

P(k

ey)

Page 34: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

RADIX PARTITIONS

19

Partition 0: 2Partition 1: 2

Partition 0: 1Partition 1: 3

0 7

0 7

0 3

1 8

1 9

1 1

1 5

1 0

Recursively repeat until target number of partitions have been created

0 7

1 8

1 9

0 7

0 3

1 1

1 5

1 0

0

1

#p

#p

#p

#p

#p

#p

#p

#p

hash

P(k

ey)

Page 35: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

BUILD PHASE

The threads are then to scan either the tuples (or partitions) of R.

For each tuple, hash the join key attribute for that tuple and add it to the appropriate bucket in the hash table.→ The buckets should only be a few cache lines in size.

20

Page 36: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

HASH TABLE

Design Decision #1: Hash Function→ How to map a large key space into a smaller domain.→ Trade-off between being fast vs. collision rate.

Design Decision #2: Hashing Scheme→ How to handle key collisions after hashing.→ Trade-off between allocating a large hash table vs.

additional instructions to find/insert keys.

21

Page 37: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

HASH FUNCTIONS

We do not want to use a cryptographic hash function for our join algorithm.

We want something that is fast and will have a low collision rate.→ Best Speed: Always return '1'→ Best Collision Rate: Perfect hashing

See SMHasher for a comprehensive hash function benchmark suite.

22

Page 38: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

HASH FUNCTIONS

CRC-64 (1975)→ Used in networking for error detection.

MurmurHash (2008)→ Designed to a fast, general purpose hash function.

Google CityHash (2011)→ Designed to be faster for short keys (<64 bytes).

Facebook XXHash (2012)→ From the creator of zstd compression.

Google FarmHash (2014)→ Newer version of CityHash with better collision rates.

23

Page 39: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

HASH FUNCTION BENCHMARK

24

0

7000

14000

21000

28000

1 51 101 151 201 251

Thr

ough

put (

MB

/sec

)

Key Size (bytes)

crc64 std::hash MurmurHash3 CityHash FarmHash XXHash3

Source: Fredrik Widlund

Intel Core i7-8700K @ 3.70GHz

32

64128

192

Page 40: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

HASHING SCHEMES

Approach #1: Chained Hashing

Approach #2: Linear Probe Hashing

Approach #3: Robin Hood Hashing

Approach #4: Hopscotch Hashing

Approach #5: Cuckoo Hashing

25

Page 41: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

CHAINED HASHING

Maintain a linked list of buckets for each slot in the hash table.

Resolve collisions by placing all elements with the same hash key into the same bucket.→ To determine whether an element is present, hash to its

bucket and scan for it.→ Insertions and deletions are generalizations of lookups.

26

Page 42: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

CHAINED HASHING

27

ABCD

hash(key)

EF

Page 43: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

CHAINED HASHING

27

ABCD

hash(key)

EF

| Ahash(A)Buckets

Page 44: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

CHAINED HASHING

27

ABCD

hash(key)

EF

| Ahash(A)

| Bhash(B)

Buckets

Page 45: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

CHAINED HASHING

27

ABCD

hash(key)

EF

| Ahash(A)

| Bhash(B)

Buckets| Chash(C)

Page 46: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

CHAINED HASHING

27

ABCD

hash(key)

EF

| Ahash(A)

| Bhash(B)

Buckets| Chash(C)

Page 47: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

CHAINED HASHING

27

ABCD

hash(key)

EF

| Ahash(A)

| Bhash(B)

| Chash(C)

| Dhash(D)

Page 48: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

CHAINED HASHING

27

ABCD

hash(key)

EF

| Ahash(A)

| Bhash(B)

| Chash(C)

| Dhash(D)

| Ehash(E)

Page 49: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

CHAINED HASHING

27

ABCD

hash(key)

EF

| Ahash(A)

| Bhash(B)

| Chash(C)

| Dhash(D)

| Ehash(E)

| Fhash(F)

Page 50: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

CHAINED HASHING

27

ABCD

hash(key)

EF

| Ahash(A)

| Bhash(B)

| Chash(C)

| Dhash(D)

| Ehash(E)

| Fhash(F)

64-bit Bucket Pointers

16-bit Bloom Filter

48-bit Pointer¤

Page 51: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

LINEAR PROBE HASHING

Single giant table of slots.

Resolve collisions by linearly searching for the next free slot in the table.→ To determine whether an element is present, hash to a

location in the table and scan for it.→ Must store the key in the table to know when to stop

scanning.→ Insertions and deletions are generalizations of lookups.

28

Page 52: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

LINEAR PROBE HASHING

29

ABCD

hash(key)

| Ahash(A)

EF

Page 53: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

LINEAR PROBE HASHING

29

ABCD

hash(key)

| Ahash(A)

| Bhash(B)

EF

Page 54: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

LINEAR PROBE HASHING

29

ABCD

hash(key)

| Ahash(A)

| Bhash(B)

EF

Page 55: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

LINEAR PROBE HASHING

29

ABCD

hash(key)

| Ahash(A)

| Bhash(B)

| Chash(C)

EF

Page 56: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

LINEAR PROBE HASHING

29

ABCD

hash(key)

| Ahash(A)

| Bhash(B)

| Chash(C)

| Dhash(D)EF

Page 57: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

LINEAR PROBE HASHING

29

ABCD

hash(key)

| Ahash(A)

| Bhash(B)

| Chash(C)

| Dhash(D)EF

Page 58: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

LINEAR PROBE HASHING

29

ABCD

hash(key)

| Ahash(A)

| Bhash(B)

| Chash(C)

| Dhash(D)E

| Ehash(E)F

Page 59: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

LINEAR PROBE HASHING

29

ABCD

hash(key)

| Ahash(A)

| Bhash(B)

| Chash(C)

| Dhash(D)E

| Ehash(E)F

| Fhash(F)

Page 60: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

OBSERVATION

To reduce the # of wasteful comparisons during the join, it is important to avoid collisions of hashed keys.

This requires a chained hash table with ~2× the number of slots as the # of elements in R.

30

Page 61: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

ROBIN HOOD HASHING

Variant of linear probe hashing that steals slots from "rich" keys and give them to "poor" keys.→ Each key tracks the number of positions they are from

where its optimal position in the table.→ On insert, a key takes the slot of another key if the first

key is farther away from its optimal position than the second key.

31

ROBIN HOOD HASHING FOUNDATIONS OF COMPUTER SCIENCE 1985

Page 62: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

ROBIN HOOD HASHING

32

ABCD

hash(key)

| A [0]hash(A)

E

# of "Jumps" From First Position

F

Page 63: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

ROBIN HOOD HASHING

32

ABCD

hash(key)

| A [0]hash(A)

| B [0]hash(B)

EF

Page 64: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

ROBIN HOOD HASHING

32

ABCD

hash(key)

| A [0]hash(A)

| B [0]hash(B)

EF

A[0] == C[0]

Page 65: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

ROBIN HOOD HASHING

32

ABCD

hash(key)

| A [0]hash(A)

| B [0]hash(B)

| C [1]hash(C)

EF

A[0] == C[0]

Page 66: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

ROBIN HOOD HASHING

32

ABCD

hash(key)

| A [0]hash(A)

| B [0]hash(B)

| C [1]hash(C)

EF

C[1] > D[0]

Page 67: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

ROBIN HOOD HASHING

32

ABCD

hash(key)

| A [0]hash(A)

| B [0]hash(B)

| C [1]hash(C)

| D [1]hash(D)EF

C[1] > D[0]

Page 68: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

ROBIN HOOD HASHING

32

ABCD

hash(key)

| A [0]hash(A)

| B [0]hash(B)

| C [1]hash(C)

| D [1]hash(D)E

A[0] == E[0]

F

Page 69: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

ROBIN HOOD HASHING

32

ABCD

hash(key)

| A [0]hash(A)

| B [0]hash(B)

| C [1]hash(C)

| D [1]hash(D)E

A[0] == E[0]

C[1] == E[1]

F

Page 70: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

ROBIN HOOD HASHING

32

ABCD

hash(key)

| A [0]hash(A)

| B [0]hash(B)

| C [1]hash(C)

| D [1]hash(D)E

A[0] == E[0]

C[1] == E[1]

D[1] < E[2]

F

Page 71: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

ROBIN HOOD HASHING

32

ABCD

hash(key)

| A [0]hash(A)

| B [0]hash(B)

| C [1]hash(C)

E | E [2]hash(E)

A[0] == E[0]

C[1] == E[1]

D[1] < E[2]

F | D [2]hash(D)

Page 72: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

ROBIN HOOD HASHING

32

ABCD

hash(key)

| A [0]hash(A)

| B [0]hash(B)

| C [1]hash(C)

E | E [2]hash(E)

F | D [2]hash(D)

| F [1]hash(F)

D[2] > F[0]

Page 73: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

HOPSCOTCH HASHING

Variant of linear probe hashing where keys can move between positions in a neighborhood.→ A neighborhood is contiguous range of slots in the table.→ The size of a neighborhood is a configurable constant.

A key is guaranteed to be in its neighborhood or not exist in the table.

33

HOPSCOTCH HASHING SYMPOSIUM ON DISTRIBUTED COMPUTING 2008

Page 74: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

HOPSCOTCH HASHING

34

ABCD

hash(key)

EF

Neighborhood Size = 3

Page 75: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

HOPSCOTCH HASHING

34

ABCD

hash(key)

EF

Neighborhood Size = 3

Neighborhood #1

Page 76: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

HOPSCOTCH HASHING

34

ABCD

hash(key)

EF

Neighborhood Size = 3

Neighborhood #1

Neighborhood #2

Neighborhood #3

Page 77: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

HOPSCOTCH HASHING

34

ABCD

hash(key)

EF

Neighborhood Size = 3

Neighborhood #3

Page 78: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

HOPSCOTCH HASHING

34

ABCD

hash(key)

EF

Neighborhood Size = 3

Neighborhood #3| Ahash(A)

Page 79: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

HOPSCOTCH HASHING

34

ABCD

hash(key)

EF

Neighborhood Size = 3

Neighborhood #1

| Ahash(A)

Page 80: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

HOPSCOTCH HASHING

34

ABCD

hash(key)

EF

Neighborhood Size = 3

Neighborhood #1

| Ahash(A)

| Bhash(B)

Page 81: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

HOPSCOTCH HASHING

34

ABCD

hash(key)

EF

Neighborhood Size = 3

Neighborhood #3| Ahash(A)

| Bhash(B)

Page 82: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

HOPSCOTCH HASHING

34

ABCD

hash(key)

EF

Neighborhood Size = 3

Neighborhood #3| Ahash(A)

| Bhash(B)

Page 83: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

HOPSCOTCH HASHING

34

ABCD

hash(key)

EF

Neighborhood Size = 3

Neighborhood #3| Ahash(A)

| Bhash(B)

| Chash(C)

Page 84: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

HOPSCOTCH HASHING

34

ABCD

hash(key)

EF

Neighborhood Size = 3

| Ahash(A)

| Bhash(B)

| Chash(C)

Page 85: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

HOPSCOTCH HASHING

34

ABCD

hash(key)

EF

Neighborhood Size = 3

| Ahash(A)

| Bhash(B)

| Chash(C)

Page 86: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

HOPSCOTCH HASHING

34

ABCD

hash(key)

EF

Neighborhood Size = 3

| Ahash(A)

| Bhash(B)

| Chash(C)

| Dhash(D)

Page 87: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

HOPSCOTCH HASHING

34

ABCD

hash(key)

EF

Neighborhood Size = 3

| Ahash(A)

| Bhash(B)

| Chash(C)

| Dhash(D)

Page 88: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

HOPSCOTCH HASHING

34

ABCD

hash(key)

EF

Neighborhood Size = 3

Neighborhood #3| Ahash(A)

| Bhash(B)

| Chash(C)

| Dhash(D)

Page 89: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

HOPSCOTCH HASHING

34

ABCD

hash(key)

EF

Neighborhood Size = 3

Neighborhood #3| Ahash(A)

| Bhash(B)

| Chash(C)

| Dhash(D)

Page 90: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

HOPSCOTCH HASHING

34

ABCD

hash(key)

EF

Neighborhood Size = 3

Neighborhood #3| Ahash(A)

| Bhash(B)

| Chash(C)

| Dhash(D)

Page 91: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

HOPSCOTCH HASHING

34

ABCD

hash(key)

EF

Neighborhood Size = 3

| Ahash(A)

| Bhash(B)

| Chash(C)

| Dhash(D)

Page 92: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

HOPSCOTCH HASHING

34

ABCD

hash(key)

EF

Neighborhood Size = 3

Neighborhood #3| Ahash(A)

| Bhash(B)

| Chash(C)

| Dhash(D)

| Ehash(E)

Page 93: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

HOPSCOTCH HASHING

34

ABCD

hash(key)

EF

Neighborhood Size = 3

Neighborhood #6

| Ahash(A)

| Bhash(B)

| Chash(C)

| Dhash(D)

| Ehash(E)

Page 94: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

HOPSCOTCH HASHING

34

ABCD

hash(key)

EF

Neighborhood Size = 3

Neighborhood #6

| Ahash(A)

| Bhash(B)

| Chash(C)

| Dhash(D)

| Ehash(E)

| Fhash(F)

Page 95: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

CUCKOO HASHING

Use multiple tables with different hash functions.→ On insert, check every table and pick anyone that has a

free slot.→ If no table has a free slot, evict the element from one of

them and then re-hash it find a new location.

Look-ups are always O(1) because only one location per hash table is checked.

35

Page 96: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

CUCKOO HASHING

36

Hash Table #1

Hash Table #2

Page 97: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

CUCKOO HASHING

36

Hash Table #1

Hash Table #2

Insert Xhash1(X) hash2(X)

Page 98: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

CUCKOO HASHING

36

Hash Table #1

Hash Table #2

Insert Xhash1(X) hash2(X)

hash1(X) | X

Page 99: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

CUCKOO HASHING

36

Hash Table #1

Hash Table #2

Insert Xhash1(X) hash2(X)

Insert Yhash1(Y) hash2(Y)

hash1(X) | X

Page 100: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

CUCKOO HASHING

36

Hash Table #1

Hash Table #2

Insert Xhash1(X) hash2(X)

Insert Yhash1(Y) hash2(Y)

hash1(X) | Xhash2(Y) | Y

Page 101: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

CUCKOO HASHING

36

Hash Table #1

Hash Table #2

Insert Xhash1(X) hash2(X)

Insert Yhash1(Y) hash2(Y)

hash1(X) | Xhash2(Y) | Y

Insert Zhash1(Z) hash2(Z)

Page 102: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

CUCKOO HASHING

36

Hash Table #1

Hash Table #2

Insert Xhash1(X) hash2(X)

Insert Yhash1(Y) hash2(Y)

hash1(X) | Xhash2(Y) | Y

Insert Zhash1(Z) hash2(Z)

Page 103: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

CUCKOO HASHING

36

Hash Table #1

Hash Table #2

Insert Xhash1(X) hash2(X)

Insert Yhash1(Y) hash2(Y)

hash1(X) | X

Insert Zhash1(Z) hash2(Z)

hash2(Z) | Z

hash1(Y)

Page 104: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

CUCKOO HASHING

36

Hash Table #1

Hash Table #2

Insert Xhash1(X) hash2(X)

Insert Yhash1(Y) hash2(Y)

hash1(X) | X

Insert Zhash1(Z) hash2(Z)

hash2(Z) | Z

hash1(Y)

Page 105: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

CUCKOO HASHING

36

Hash Table #1

Hash Table #2

Insert Xhash1(X) hash2(X)

Insert Yhash1(Y) hash2(Y)

Insert Zhash1(Z) hash2(Z)

hash2(Z) | Z

hash1(Y)

hash1(Y) | Y

Page 106: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

CUCKOO HASHING

36

Hash Table #1

Hash Table #2

Insert Xhash1(X) hash2(X)

Insert Yhash1(Y) hash2(Y)

Insert Zhash1(Z) hash2(Z)

hash2(Z) | Z

hash1(Y)

hash1(Y) | Y

hash2(X)

hash2(X) | X

Page 107: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

CUCKOO HASHING

Threads have to make sure that they don’t get stuck in an infinite loop when moving keys.

If we find a cycle, then we can rebuild the entire hash tables with new hash functions.→ With two hash functions, we (probably) won’t need to

rebuild the table until it is at about 50% full.→ With three hash functions, we (probably) won’t need to

rebuild the table until it is at about 90% full.

37

Page 108: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

PROBE PHASE

For each tuple in S, hash its join key and check to see whether there is a match for each tuple in corresponding bucket in the hash table constructed for R.→ If inputs were partitioned, then assign each thread a

unique partition.→ Otherwise, synchronize their access to the cursor on S.

38

Page 109: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

PROBE PHASE BLOOM FILTER

Create a Bloom Filter during the build phase when the key is likely to not exist in the hash table.→ Threads check the filter before probing the hash table.

This will be faster since the filter will fit in CPU caches.→ Sometimes called sideways information passing.

39

A B

MICRO ADAPTIVITY IN VECTORWISESIGMOD 2013

Page 110: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

PROBE PHASE BLOOM FILTER

Create a Bloom Filter during the build phase when the key is likely to not exist in the hash table.→ Threads check the filter before probing the hash table.

This will be faster since the filter will fit in CPU caches.→ Sometimes called sideways information passing.

39

A B

⨝Bloom Filter

MICRO ADAPTIVITY IN VECTORWISESIGMOD 2013

Page 111: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

PROBE PHASE BLOOM FILTER

Create a Bloom Filter during the build phase when the key is likely to not exist in the hash table.→ Threads check the filter before probing the hash table.

This will be faster since the filter will fit in CPU caches.→ Sometimes called sideways information passing.

39

A B

⨝ Bloom Filter

MICRO ADAPTIVITY IN VECTORWISESIGMOD 2013

Page 112: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

PROBE PHASE BLOOM FILTER

Create a Bloom Filter during the build phase when the key is likely to not exist in the hash table.→ Threads check the filter before probing the hash table.

This will be faster since the filter will fit in CPU caches.→ Sometimes called sideways information passing.

39

A B

⨝ Bloom Filter

MICRO ADAPTIVITY IN VECTORWISESIGMOD 2013

Page 113: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

HASH JOIN VARIANTS

40

No-P Shared-P Private-P Radix

Partitioning No Yes Yes Yes

Input scans 0 1 1 2

Sync during partitioning

– Spinlock per tuple

Barrier, once at end

Barrier, 4 · #passes

Hash table Shared Private Private Private

Sync during build phase

Yes No No No

Sync during probe phase

No No No No

Page 114: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

BENCHMARKS

Primary key – foreign key join→ Outer Relation (Build): 16M tuples, 16 bytes each→ Inner Relation (Probe): 256M tuples, 16 bytes each

Uniform and highly skewed (Zipf; s=1.25)

No output materialization

41

DESIGN AND EVALUATION OF MAIN MEMORY HASH JOIN ALGORITHMS FOR MULTI-CORE CPUSSIGMOD 2011

Page 115: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

HASH JOIN UNIFORM DATA SET

42

0

40

80

120

160

No Partitioning Shared Partitioning

Private Partitioning

Radix

Cyc

les /

Ou

tpu

t Tu

ple

Partition Build Probe

Intel Xeon CPU X5650 @ 2.66GHz6 Cores with 2 Threads Per Core

60.2 67.676.8

47.3

24% faster thanNo Partitioning

3.3x cache misses70x TLB misses

Source: Spyros Blanas

Page 116: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

HASH JOIN SKEWED DATA SET

43

0

40

80

120

160

No Partitioning Shared Partitioning

Private Partitioning

Radix

Cyc

les /

Ou

tpu

t Tu

ple

Partition Build Probe

Intel Xeon CPU X5650 @ 2.66GHz6 Cores with 2 Threads Per Core

25.2

167.1

56.5 50.7

Source: Spyros Blanas

Page 117: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

OBSERVATION

We have ignored a lot of important parameters for all these algorithms so far.→ Whether to use partitioning or not?→ How many partitions to use?→ How many passes to take in partitioning phase?

In a real DBMS, the optimizer will select what it thinks are good values based on what it knows about the data (and maybe hardware).

44

Page 118: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

RADIX HASH JOIN UNIFORM DATA SET

45

0

40

80

120

64 256

512

1024

4096

8192

3276

8

1310

72 64 256

512

1024

4096

8192

3276

8

1310

72

Radix / 1-Pass Radix / 2-Pass

Cyc

les /

Ou

tpu

t Tu

ple

Partition Build Probe

Intel Xeon CPU X5650 @ 2.66GHzVarying the # of Partitions

▼No Partitioning+24% -5%

Source: Spyros Blanas

Page 119: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

RADIX HASH JOIN UNIFORM DATA SET

46

0

40

80

120

64 256

512

1024

4096

8192

3276

8

1310

72 64 256

512

1024

4096

8192

3276

8

1310

72

Radix / 1-Pass Radix / 2-Pass

Cyc

les /

Ou

tpu

t Tu

ple

Partition Build Probe

Intel Xeon CPU X5650 @ 2.66GHzVarying the # of Partitions

▼No Partitioning

Source: Spyros Blanas

Page 120: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

EFFECTS OF HYPER-THREADING

Radix join has fewer cache & TLB misses but this has marginal benefit.

Non-partitioned join relies on multi-threading for high performance.

47

Intel Xeon CPU X5650 @ 2.66GHzUniform Data Set

1

3

5

7

9

11

1 3 5 7 9 11

Spee

dup

Threads

No Partitioning Radix Ideal

Source: Spyros Blanas

Hyper-Threading

Page 121: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

TPC-H Q19

48

250 279301 285

0

100

200

300

400

No-Part (Linear)

No-Part (Array)

Radix (Linear)

Radix (Array)

Ru

nti

me

(ms)

Join Remaining Query

4× Intel Xeon CPU E7-4870v4Scale Factor 100

Source: Stefan Schuh

Page 122: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

PARTING THOUGHTS

Partitioned-based joins outperform no-partitioning algorithms is most settings, but it is non-trivial to tune it correctly.

AFAIK, every DBMS vendor picks one hash join implementation and does not try to be adaptive.

49

Page 123: 7 ADVANCED DATABASE SYSTEMS - CMU 15-721

15-721 (Spring 2020)

NEXT CL ASS

Parallel Sort-Merge Joins

50