2. B+ Tree
인덱스에 가장 많이 쓰이는 자료구조
특징
- Leaf Node만 데이터를 가진다.
- 루트를 제외한 각 Node는 절반 이상의 Entry를 가진다.
- 작업은 항상 루트부터 시작된다.
- 항상 균형을 유지한다.
3. B+ Tree : 노드의 구성
일반적인 노드의 특징
- 다른 노드를 가리키는 Pointer와 Search Key의 집합.
- 각 Search Key는 정렬된 상태이다.
(ex. K1 < K2 < K3 < K4 …. < Kn-1 )
- P1은 K1보다 작은 값을 가지는 노드를 가리킨다.
- P2는 K1과 같거나 큰 값을 가지는 노드를 가리킨다.
- (Pm, Km) 한 묶음을 엔트리라고 한다.
4. B+ Tree : Insert
procedure insert(value K, pointer P)
Find the leaf node L that should contain K
if L has less than n−1 values then
insert-in-leaf(L, K, P)
else
Create node L′
Copy L.P1 . . . L.Kn−1 to T (an auxiliary node)
insert-in-leaf(T, K, P)
Set L′.Pn = L.Pn; Set L.Pn = L′
Erase L.P1 . . . L.Kn−1 from L
Copy T.P1 . . .T.K⌈n/2⌉ to L.P1 . . .
Copy T.P⌈n/2⌉+1 . . .T.Kn to L′ .P1 . . .
Let K′ be the smallest key in L′
insert-in-parent(L, K′, L′)
end
5. B+ Tree : Insert (cont.)
procedure insert-in-leaf(node L, value K, pointer P)
if K < L.K1 then
Insert P, K into L before L.P1
else
Let Ki be the highest value in L that is less than K
Insert P, K into L after L.Ki
end
6. B+ Tree : Insert (cont.)
procedure insert-in-parent(node N, value K′, Pointer N′)
if N is the root then
Create a new node R = (N, K′, N′)
Make R the new root; return
end
Let P = parent(N)
if P has less than n pointers then
Insert K′, N′ into P, after N
else
Copy P to T
Insert K′, N′ into T, after N
Erase all from P; Create node P′
Copy T.P1 ...T.P⌈n/2⌉ to P
Let K′′ = T.K⌈n/2⌉
Copy T.P⌈n/2⌉+1 ...T.Pn+1 to P′
insert-in-parent(P, K′′, P′)
end
7. B+ Tree : Inserting 80
200 400
100 250
200 300 32050 150
80
Find the leaf node L that should contain K
8. B+ Tree : Inserting 80 (cont.)
200 400
100 250
200 300 32050 150
80
if L has less than n−1 values then
insert-in-leaf(L, K, P)
9. B+ Tree : Inserting 80 (cont.)
200 400
100 250
200 300 32050 80 150
else
Let Ki be the highest value in L that is less than K
Insert P, K into L after L.Ki
10. B+ Tree : Inserting 60
200 400
100 250
200 300 32050 80 150
60
Find the leaf node L that should contain K
11. B+ Tree : Inserting 60 (cont.)
200 400
100 250
200 300 32050 80 150
60
else
Create node L′
Copy L.P1 . . . L.Kn−1 to T (an auxiliary node)
insert-in-leaf(T, K, P)
50 60 80
L`
T
12. B+ Tree : Inserting 60 (cont.)
200 400
100 250
200 300 32050 60 150
50 60 80
80
T
Set L′.Pn = L.Pn; Set L.Pn = L′
Erase L.P1 . . . L.Kn−1 from L
Copy T.P1 . . .T.K⌈n/2⌉ to L.P1 . . .
Copy T.P⌈n/2⌉+1 . . .T.Kn to L′.P1 . . .
13. B+ Tree : Inserting 60 (cont.)
200 400
100 250
200 300 32050 60 15080
Let K′ be the smallest key in L′
insert-in-parent(L, K′, L′)
80
14. B+ Tree : Inserting 60 (cont.)
200 400
80 250
200 300 32050 60 15080
Let P = parent(N)
if P has less than n pointers then
Insert K′, N′ into P, after N
100
15. B+ Tree : Inserting 40
200 400
80 250
200 300 32050 60 15080
100
40
Find the leaf node L that should contain K
16. B+ Tree : Inserting 40 (cont.)
200 400
80 250
20040 50 15080
100
앞과 같이 리프노드에서의 삽입 후
60
60
Let K′ be the smallest key in L′
insert-in-parent(L, K′, L′)
17. B+ Tree : Inserting 40 (cont.)
200 400
250
20040 50 1508060
Let P = parent(N)
Copy P to T
Insert K′, N′ into T, after N
Erase all from P; Create node P′
60 80 100
T
P’
18. B+ Tree : Inserting 40 (cont.)
200 400
60 250
20040 50 1508060
Copy T.P1 ...T.P⌈n/2⌉ to P
Let K′′ = T.K⌈n/2⌉
Copy T.P⌈n/2⌉+1 ...T.Pn+1 to P′
insert-in-parent(P, K′′, P′)
100
80
19. B+ Tree : Inserting 40 (cont.)
80
60 250
20040 50 1508060
이전과 같이 노드를 분할시켜서 삽입한 후,
insert-in-parent(P, K′′, P′)
100
200
400
20. B+ Tree : Inserting 40 (cont.)
80
60 250
20040 50 1508060
100
400
200
if N is the root then
Create a new node R = (N, K′, N′)
Make R the new root; return
end
21. B+ Tree : Delete
procedure delete(value V, pointer P)
Find the leaf node L that contains V, P
delete-entry(L, V, P)
22. B+ Tree : Delete
procedure delete-entry(node L, value V, pointer P)
delete V, P from L
if L is root and has only 1 child then
delete L
make the child the new root
return
end
if L has too few pointers/values then
Let L’ be the previous or next child of parent(L)
Let V’ be the value between L and L’ in parent(L)
if entries in L and L’ fit in one node then
coalesce-nodes(L, V’, L’)
else
redistribute-nodes(L, V’, L’)
end
23. B+ Tree : Delete
procedure coalesce-nodes(node L, value V’, node L’)
if L is a leaf then
Append all (Ki, Pi) pairs in L to L’
Set L’.Pn to L.pn, if needed
else
Append V’ and all pointers/values in L to L’
delete-entry(parent(L), V’, L)
delete node L
24. B+ Tree : Delete
procedure redistribute-nodes(node L, value V’, node L’)
if L’ is a predecessor of L then
if L is a non-leaf node then
Let L’.Pm be the last pointer in L’
Remove L’.Km-1, L’.Pm from L’
Insert L’.Pm, V’ as the first pointer/value in L
Replace V’ in parent(L) by L’Km-1
else
Let L’.Pm, L’.Km be the last pointer/value pair in L’
Remove L’.Pm, L’.Km from L’
Insert L’.Pm, L’.Km as the first pointer/value in L
Replace V’ in parent(L) by L’Km
end
else … perform the symetric case …
25. B+ Tree : Delete 60
200 400
80 250
200 300 32050 60 15080
100
Find the leaf node L that contains V, P
26. B+ Tree : Delete 60(cont.)
200 400
80 250
200 300 32050 15080
100
Delete V, P from L
27. B+ Tree : Delete 50
200 400
80 250
200 300 32050 15080
100
Find the leaf node L that contains V, P
28. B+ Tree : Delete 50(cont.)
200 400
80 250
200 300 32015080
100
Delete V, P from L
if L has too few pointers/values then
Let L’ be the previous or next child of parent(L)
Let V’ be the value between L and L’ in parent(L)
if entries in L and L’ fit in one node then
coalesce-nodes(L, V’, L’)
L’
V’
L
29. B+ Tree : Delete 50(cont.)
200 400
250
200 300 32015080
100
L’
V’
L
if L is a leaf then
Append all (Ki, Pi) pairs in L to L’
Set L’.Pn to L.pn, if needed
delete-entry(parent(L), V’, L)
30. B+ Tree : Delete 50(cont.)
200 400
100 250
200 300 32015080
delete node L
31. B+ Tree : Delete 150
200 400
100 250
200 300 32015080
Find the leaf node L that contains V, P
32. B+ Tree : Delete 150(cont.)
200 400
100 250
200 300 32080
coalesce-nodes(L, V’, L’)
if L is a leaf then
Append all (Ki, Pi) pairs in L to L’
Set L’.Pn to L.Pn, if needed
delete-entry(parent(L), V’, L)
delete node L
L’
V’
L
33. B+ Tree : Delete 150(cont.)
200 400
250
200 300 32080
L’
V’
delete V, P from L
coalesce-nodes(L, V’, L’)
…
L
34. B+ Tree : Delete 150(cont.)
200 400
200 250
200 300 32080
L’
V’
L
Append V’ and all pointers/values in L to L’
35. B+ Tree : Delete 150(cont.)
200 400
200 250
200 300 32080
L’
V’
delete-entry(parent(L), V’, L)
delete node L
37. B+ Tree : Delete 80
200
100 250 400
200 300 32080 150
Find the leaf node L that contains V, P
38. B+ Tree : Delete 80(cont.)
200
100 250 400
200 300 320150
delete V, P from L
...
coalesce-nodes(L, V’, L’)
...
Redistribute-nodes(L, V’, L’)
39. B+ Tree : Delete 80(cont.)
250
200 250 400
200 300 320150
Let L’.Pm be the first pointer in L’
Insert L’.Pm, V’ as the last pointer/value in L
Replace V’ in parent(L) by L’Km
L’
V’
L
40. B+ Tree : Delete 80(cont.)
250
200 400
200 300 320150
L’
V’
L
Remove L’.Km, L’.Pm from L’
41. Clustered Index
특징
- 한 테이블에 하나만 만들 수 있다.
- 인덱스의 리프노드가 테이블 Row 그 자체가 된다.
- 테이블에 클러스터드 인덱스를 생성하게 되면, 해당 컬럼으로 정렬된다.
- 새로운 데이터 삽입시 페이지 분할이 일어날 수 있다
- 새로운 데이터 삽입시 실제 테이블이 다시 정렬된다
LSG 이승기
KBS 김범수
KKH 김경호
JYP 조용필
S나 성시경
LJB 임재범
YJS 윤종신
EJW 은지원
JKW 조관우
BBK 바비킴
100210011000
45. NonClustered Index
특징
- 각 컬럼마다 생성할 수 있다.
- 리프노드는 페이지번호 + 오프셋을 가진다.
- 새로운 데이터 삽입시 가장 마지막 페이지 뒤쪽에 추가한다.
- 클러스터드 인덱스와 같이 있을경우, 리프노드는 페이지번호 + 오프셋대신
클러스터드 인덱스의 키값을 가진다.
LSG 이승기
KBS 김범수
KKH 김경호
JYP 조용필
SSK 성시경
LJB 임재범
YJS 윤종신
EJW 은지원
JKW 조관우
BBK 바비킴
100210011000
52. Table Join
SELECT COL1, COL2
FROM TABLE1 A INNER JOIN TABLE2 B
ON A.KEY = B.KEY
WHERE A.KEY = ‘111’
AND A.COL LIKE ‘222%’
AND B.COL2 = ‘333’
INDEX = A.KEY, B.KEY