SlideShare a Scribd company logo
1 of 53
Clustered & NonClustered
Index
IT개발실 Noah
B+ Tree
인덱스에 가장 많이 쓰이는 자료구조
특징
- Leaf Node만 데이터를 가진다.
- 루트를 제외한 각 Node는 절반 이상의 Entry를 가진다.
- 작업은 항상 루트부터 시작된다.
- 항상 균형을 유지한다.
B+ Tree : 노드의 구성
일반적인 노드의 특징
- 다른 노드를 가리키는 Pointer와 Search Key의 집합.
- 각 Search Key는 정렬된 상태이다.
(ex. K1 < K2 < K3 < K4 …. < Kn-1 )
- P1은 K1보다 작은 값을 가지는 노드를 가리킨다.
- P2는 K1과 같거나 큰 값을 가지는 노드를 가리킨다.
- (Pm, Km) 한 묶음을 엔트리라고 한다.
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
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
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
B+ Tree : Inserting 80
200 400
100 250
200 300 32050 150
80
Find the leaf node L that should contain K
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)
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
B+ Tree : Inserting 60
200 400
100 250
200 300 32050 80 150
60
Find the leaf node L that should contain K
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
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 . . .
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
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
B+ Tree : Inserting 40
200 400
80 250
200 300 32050 60 15080
100
40
Find the leaf node L that should contain K
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′)
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’
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
B+ Tree : Inserting 40 (cont.)
80
60 250
20040 50 1508060
이전과 같이 노드를 분할시켜서 삽입한 후,
insert-in-parent(P, K′′, P′)
100
200
400
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
B+ Tree : Delete
procedure delete(value V, pointer P)
Find the leaf node L that contains V, P
delete-entry(L, V, P)
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
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
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 …
B+ Tree : Delete 60
200 400
80 250
200 300 32050 60 15080
100
Find the leaf node L that contains V, P
B+ Tree : Delete 60(cont.)
200 400
80 250
200 300 32050 15080
100
Delete V, P from L
B+ Tree : Delete 50
200 400
80 250
200 300 32050 15080
100
Find the leaf node L that contains V, P
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
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)
B+ Tree : Delete 50(cont.)
200 400
100 250
200 300 32015080
delete node L
B+ Tree : Delete 150
200 400
100 250
200 300 32015080
Find the leaf node L that contains V, P
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
B+ Tree : Delete 150(cont.)
200 400
250
200 300 32080
L’
V’
delete V, P from L
coalesce-nodes(L, V’, L’)
…
L
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’
B+ Tree : Delete 150(cont.)
200 400
200 250
200 300 32080
L’
V’
delete-entry(parent(L), V’, L)
delete node L
B+ Tree : Delete 150(cont.)
400
200 250
200 300 32080
B+ Tree : Delete 80
200
100 250 400
200 300 32080 150
Find the leaf node L that contains V, P
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’)
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
B+ Tree : Delete 80(cont.)
250
200 400
200 300 320150
L’
V’
L
Remove L’.Km, L’.Pm from L’
Clustered Index
특징
- 한 테이블에 하나만 만들 수 있다.
- 인덱스의 리프노드가 테이블 Row 그 자체가 된다.
- 테이블에 클러스터드 인덱스를 생성하게 되면, 해당 컬럼으로 정렬된다.
- 새로운 데이터 삽입시 페이지 분할이 일어날 수 있다
- 새로운 데이터 삽입시 실제 테이블이 다시 정렬된다
LSG 이승기
KBS 김범수
KKH 김경호
JYP 조용필
S나 성시경
LJB 임재범
YJS 윤종신
EJW 은지원
JKW 조관우
BBK 바비킴
100210011000
BBK 바비킴
EJW 은지원
JWK 조관우
JYP 조용필
KBS 김범수
KKH 김경호
LJB 임재범
LSG 이승기
SSK 성시경
YJS 윤종신
100210011000
Clustered Index (cont.)
BBK 1000
KBS 1001
SSK 1002
Root
BBK 바비킴
EJW 은지원
JWK 조관우
JYP 조용필
KBS 김범수
KKH 김경호
LJB 임재범
LSG 이승기
SSK 성시경
YJS 윤종신
100210011000
Clustered Index : Insert
BBK 1000
KBS 1001
SSK 1002
Insert : KSR, 김수린
KSR 김수린
Root
BBK 바비킴
EJW 은지원
JWK 조관우
JYP 조용필
KBS 김범수
KKH 김경호
KSR 김수린
SSK 성시경
YJS 윤종신
100210011000
Clustered Index : Insert(cont.)
BBK 1000
KBS 1001
SSK 1002
LJB 1003
Insert : KSR, 김수린
LJB 임재범
LSG 이승기
1003
Root
NonClustered Index
특징
- 각 컬럼마다 생성할 수 있다.
- 리프노드는 페이지번호 + 오프셋을 가진다.
- 새로운 데이터 삽입시 가장 마지막 페이지 뒤쪽에 추가한다.
- 클러스터드 인덱스와 같이 있을경우, 리프노드는 페이지번호 + 오프셋대신
클러스터드 인덱스의 키값을 가진다.
LSG 이승기
KBS 김범수
KKH 김경호
JYP 조용필
SSK 성시경
LJB 임재범
YJS 윤종신
EJW 은지원
JKW 조관우
BBK 바비킴
100210011000
LSG 이승기
KBS 김범수
KKH 김경호
JYP 조용필
SSK 성시경
LJB 임재범
YJS 윤종신
EJW 은지원
JKW 조관우
BBK 바비킴
100210011000
BBK 1002+#2
EJW 1001+#4
KKH 1000+#3
JYP 1000+#4
KBS 1000+#2
KKH 1000+#3
LJB 1001+#2
LSG 1000+#1
SSK 1001+#1
YJS 1001+#3
BBK 100
KKH 200
100 200
Index
Data
Leaf Page
Root Page
NonClustered Index(cont.)
LSG 이승기
KBS 김범수
KKH 김경호
JYP 조용필
SSK 성시경
LJB 임재범
YJS 윤종신
EJW 은지원
JKW 조관우
BBK 바비킴
100210011000
BBK 1002+#2
EJW 1001+#4
KKH 1000+#3
JYP 1000+#4
KBS 1000+#2
KKH 1000+#3
LJB 1001+#2
LSG 1000+#1
SSK 1001+#1
YJS 1001+#3
BBK 100
KKH 200
100 200
Data
NonClustered Index : Insert
Insert : KSR, 김수린
KSR 김수린
LSG 이승기
KBS 김범수
KKH 김경호
JYP 조용필
SSK 성시경
LJB 임재범
YJS 윤종신
EJW 은지원
JKW 조관우
BBK 바비킴
KSR 김수린
100210011000
BBK 1002+#2
EJW 1001+#4
KKH 1000+#3
JYP 1000+#4
KBS 1000+#2
KSR 1002+#3
KKH 1000+#3
LJB 1001+#2
LSG 1000+#1
SSK 1001+#1
YJS 1001+#3
BBK 100
KKH 200
100 200
Data
NonClustered Index : Insert
Inserted : KSR, 김수린
BBK 바비킴
EJW 은지원
JWK 조관우
JYP 조용필
KBS 김범수
KKH 김경호
LJB 임재범
LSG 이승기
SSK 성시경
YJS 윤종신
100210011000
BBK 1000
KBS 1001
SSK 1002
김경호 KKH
김범수 KBS
바비킴 BBK
성시경 SSK
윤종신 YJS
은지원 EJW
이승기 LSG
임재범 LJB
조관우 JWK
조용필 JYP
김경호 100
은지원 200
100 200
Clustered &
NonClustered
BBK 바비킴
EJW 은지원
JWK 조관우
JYP 조용필
KBS 김범수
KKH 김경호
LJB 임재범
LSG 이승기
SSK 성시경
YJS 윤종신
100210011000
BBK 1000
KBS 1001
SSK 1002
김경호 KKH
김범수 KBS
바비킴 BBK
성시경 SSK
윤종신 YJS
은지원 EJW
이승기 LSG
임재범 LJB
조관우 JWK
조용필 JYP
김경호 100
은지원 200
100 200
Clustered &
NonClustered
Insert : KSR, 김수린
KSR 김수린
BBK 1000
KBS 1001
SSK 1002
LJB 1003
김경호 KKH
김범수 KBS
김수린 KSR
바비킴 BBK
성시경 SSK
윤종신 YJS
은지원 EJW
이승기 LSG
임재범 LJB
조관우 JWK
조용필 JYP
김경호 100
은지원 200
100 200
Clustered &
NonClustered
Inserted : KSR, 김수린
BBK 바비킴
EJW 은지원
JWK 조관우
JYP 조용필
KBS 김범수
KKH 김경호
KSR 김수린
SSK 성시경
YJS 윤종신
100210011000
LJB 임재범
LSG 이승기
1003
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
Table Join(cont.)
INDEX TABLE1 INDEX TABLE2
A.KEY = ‘111’
COL1 LIKE
‘222%’
B.KEY = ‘111’
COL2 = ‘333’

More Related Content

Featured

PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...DevGAMM Conference
 

Featured (20)

Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 

Clustered &amp; non clustered

  • 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
  • 36. B+ Tree : Delete 150(cont.) 400 200 250 200 300 32080
  • 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
  • 42. BBK 바비킴 EJW 은지원 JWK 조관우 JYP 조용필 KBS 김범수 KKH 김경호 LJB 임재범 LSG 이승기 SSK 성시경 YJS 윤종신 100210011000 Clustered Index (cont.) BBK 1000 KBS 1001 SSK 1002 Root
  • 43. BBK 바비킴 EJW 은지원 JWK 조관우 JYP 조용필 KBS 김범수 KKH 김경호 LJB 임재범 LSG 이승기 SSK 성시경 YJS 윤종신 100210011000 Clustered Index : Insert BBK 1000 KBS 1001 SSK 1002 Insert : KSR, 김수린 KSR 김수린 Root
  • 44. BBK 바비킴 EJW 은지원 JWK 조관우 JYP 조용필 KBS 김범수 KKH 김경호 KSR 김수린 SSK 성시경 YJS 윤종신 100210011000 Clustered Index : Insert(cont.) BBK 1000 KBS 1001 SSK 1002 LJB 1003 Insert : KSR, 김수린 LJB 임재범 LSG 이승기 1003 Root
  • 45. NonClustered Index 특징 - 각 컬럼마다 생성할 수 있다. - 리프노드는 페이지번호 + 오프셋을 가진다. - 새로운 데이터 삽입시 가장 마지막 페이지 뒤쪽에 추가한다. - 클러스터드 인덱스와 같이 있을경우, 리프노드는 페이지번호 + 오프셋대신 클러스터드 인덱스의 키값을 가진다. LSG 이승기 KBS 김범수 KKH 김경호 JYP 조용필 SSK 성시경 LJB 임재범 YJS 윤종신 EJW 은지원 JKW 조관우 BBK 바비킴 100210011000
  • 46. LSG 이승기 KBS 김범수 KKH 김경호 JYP 조용필 SSK 성시경 LJB 임재범 YJS 윤종신 EJW 은지원 JKW 조관우 BBK 바비킴 100210011000 BBK 1002+#2 EJW 1001+#4 KKH 1000+#3 JYP 1000+#4 KBS 1000+#2 KKH 1000+#3 LJB 1001+#2 LSG 1000+#1 SSK 1001+#1 YJS 1001+#3 BBK 100 KKH 200 100 200 Index Data Leaf Page Root Page NonClustered Index(cont.)
  • 47. LSG 이승기 KBS 김범수 KKH 김경호 JYP 조용필 SSK 성시경 LJB 임재범 YJS 윤종신 EJW 은지원 JKW 조관우 BBK 바비킴 100210011000 BBK 1002+#2 EJW 1001+#4 KKH 1000+#3 JYP 1000+#4 KBS 1000+#2 KKH 1000+#3 LJB 1001+#2 LSG 1000+#1 SSK 1001+#1 YJS 1001+#3 BBK 100 KKH 200 100 200 Data NonClustered Index : Insert Insert : KSR, 김수린 KSR 김수린
  • 48. LSG 이승기 KBS 김범수 KKH 김경호 JYP 조용필 SSK 성시경 LJB 임재범 YJS 윤종신 EJW 은지원 JKW 조관우 BBK 바비킴 KSR 김수린 100210011000 BBK 1002+#2 EJW 1001+#4 KKH 1000+#3 JYP 1000+#4 KBS 1000+#2 KSR 1002+#3 KKH 1000+#3 LJB 1001+#2 LSG 1000+#1 SSK 1001+#1 YJS 1001+#3 BBK 100 KKH 200 100 200 Data NonClustered Index : Insert Inserted : KSR, 김수린
  • 49. BBK 바비킴 EJW 은지원 JWK 조관우 JYP 조용필 KBS 김범수 KKH 김경호 LJB 임재범 LSG 이승기 SSK 성시경 YJS 윤종신 100210011000 BBK 1000 KBS 1001 SSK 1002 김경호 KKH 김범수 KBS 바비킴 BBK 성시경 SSK 윤종신 YJS 은지원 EJW 이승기 LSG 임재범 LJB 조관우 JWK 조용필 JYP 김경호 100 은지원 200 100 200 Clustered & NonClustered
  • 50. BBK 바비킴 EJW 은지원 JWK 조관우 JYP 조용필 KBS 김범수 KKH 김경호 LJB 임재범 LSG 이승기 SSK 성시경 YJS 윤종신 100210011000 BBK 1000 KBS 1001 SSK 1002 김경호 KKH 김범수 KBS 바비킴 BBK 성시경 SSK 윤종신 YJS 은지원 EJW 이승기 LSG 임재범 LJB 조관우 JWK 조용필 JYP 김경호 100 은지원 200 100 200 Clustered & NonClustered Insert : KSR, 김수린 KSR 김수린
  • 51. BBK 1000 KBS 1001 SSK 1002 LJB 1003 김경호 KKH 김범수 KBS 김수린 KSR 바비킴 BBK 성시경 SSK 윤종신 YJS 은지원 EJW 이승기 LSG 임재범 LJB 조관우 JWK 조용필 JYP 김경호 100 은지원 200 100 200 Clustered & NonClustered Inserted : KSR, 김수린 BBK 바비킴 EJW 은지원 JWK 조관우 JYP 조용필 KBS 김범수 KKH 김경호 KSR 김수린 SSK 성시경 YJS 윤종신 100210011000 LJB 임재범 LSG 이승기 1003
  • 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
  • 53. Table Join(cont.) INDEX TABLE1 INDEX TABLE2 A.KEY = ‘111’ COL1 LIKE ‘222%’ B.KEY = ‘111’ COL2 = ‘333’