Skip to content

Commit f5df955

Browse files
committed
parallel join
1 parent c36c0ef commit f5df955

File tree

1 file changed

+48
-9
lines changed

1 file changed

+48
-9
lines changed

_posts/2024-11-27-join_algorithms.md

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,24 @@ end
2828
## Sort-Merge Join
2929

3030
```sudo
31-
while not at the end of either relation do begin
32-
if r and s match on the join attribute
33-
output the tuple <r, s>
34-
if r < s
35-
advance to the next tuple in R
36-
else advance to the next tuple in S
37-
end
31+
do {
32+
if (!mark) {
33+
while (r < s) { advance r }
34+
while (r > s) { advance s }
35+
// mark start of "block" of s
36+
mark = s
37+
}
38+
if (r == s) {
39+
result = <r, s>
40+
advance s
41+
return result
42+
}
43+
else {
44+
reset s to mark
45+
advance r
46+
mark = NULL
47+
}
48+
}
3849
```
3950
- 조인 컬럼으로 정렬되어 있어야 한다.
4051
- 정렬되어 있다면 시간 복잡도는 $O(n)$이다.
@@ -58,8 +69,36 @@ end
5869
- 해시 테이블은 메모리보다 작아야 하기 때문이다.
5970
- 시간 복잡도는 $O(n)$이다.
6071

61-
# Apache Spark의 조인 알고리즘
72+
# 병렬 조인 알고리즘
73+
74+
## Parallel Hash Join
75+
76+
단일 머신 해시 조인을 병렬로 수행.
77+
- 1. hash partitioning
78+
- S, R 이 여러 파티션으로 나뉘어지고 해시하여 동일한 키를 가진 레코드는 한 노드로 모인다.
79+
- shuffle 발생
80+
- 2. build - local hash table build
81+
- 로컬에서 해시테이블 build 한다. - 병렬처리 가능.
82+
- 3. probe
83+
- probe가 수행되려면 build 단계가 완료되어야 한다. (즉 pipeline-break.)
84+
- 로컬에서 수행된다. - 병렬처리 가능.
85+
![](https://dt5vp8kor0orz.cloudfront.net/deb3b1023aa97d164a291e64032fa3f05d566a58/5-Figure4-1.png)
86+
87+
## Parallel Sort-Merge Join
88+
- 1. range partitioning - sort
89+
- S, R을 조인 키의 범위로 파티셔닝한다. 동일한 키를 가진 레코드는 한 노드로 모인다.
90+
- data skew가 발생할 수 있다. 이는 샘플링을 통해 해결할 수 있다.
91+
- shuffle 발생
92+
- 2. merge
93+
- 로컬에서 수행된다. - 병렬처리 가능.
94+
- spark 나 MapReduce에서는 input이 정렬되어있는(1번 단계가 완료된) 경우가 많아서 주로 사용한다.
95+
96+
## Broadcast Join
97+
- 작은 테이블을 모든 노드로 복제(broadcast)하여 조인을 수행.
98+
- 각 노드에서 로컬 조인을 수행. - 병렬처리 가능.
99+
- 작은 테이블이 노드 메모리에 올라갈 수 있을 때 사용.
100+
- 어떤 조인 알고리즘이든 사용 가능하다.
62101

63102
# 참고
64103
- Database System Concepts, 7th Edition. *Abraham Silberschatz, Henry F. Korth, S. Sudarshan. McGraw-Hill Education. 2019.*
65-
-
104+
- [CS186 berkely lecture](https://www.youtube.com/@CS186Berkeley/videos)

0 commit comments

Comments
 (0)