Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
これでわかるB-treeアルゴリズム / B-tree algorithm
Search
ハトネコエ
December 06, 2018
Technology
12
9.8k
これでわかるB-treeアルゴリズム / B-tree algorithm
・二分探索木 (binary search tree)
・AVL tree
・B-tree
・B+ tree
について順を追いながら説明。
流れを細かく書いたので、わかりやすいと思います。
ハトネコエ
December 06, 2018
Tweet
Share
More Decks by ハトネコエ
See All by ハトネコエ
Godot 4.3 と学ぶインタラクティブミュージック / Interactive Music Basics with Godot 4.3
nekonenene
0
47
Developer Consoleを使い倒そう / Use Web Browser DevTools
nekonenene
0
8
まだまだマイナー?! 未踏事業について教えます / Introduction of Mitou Project
nekonenene
1
97
Docker for Windows/macOS
nekonenene
0
8
技術的負債を防ぐには / What is the Technical Debt
nekonenene
0
290
画像処理の基礎の基礎 / Ultra Basic of Image Processing
nekonenene
0
24
伝わる文章を書こう講座 / Write the Kind Japanese Message
nekonenene
2
140
Unity で Android 自動ビルドしたかった話 / I tried Android build of Unity using Docker, but...
nekonenene
0
2.1k
なぜ社内勉強会をするのか
nekonenene
0
140
Other Decks in Technology
See All in Technology
マイクロサービスにおける容易なトランザクション管理に向けて
scalar
0
130
Turing × atmaCup #18 - 1st Place Solution
hakubishin3
0
480
podman_update_2024-12
orimanabu
1
270
Microsoft Azure全冠になってみた ~アレを使い倒した者が試験を制す!?~/Obtained all Microsoft Azure certifications Those who use "that" to the full will win the exam! ?
yuj1osm
2
110
10個のフィルタをAXI4-Streamでつなげてみた
marsee101
0
170
5分でわかるDuckDB
chanyou0311
10
3.2k
20241214_WACATE2024冬_テスト設計技法をチョット俯瞰してみよう
kzsuzuki
3
450
.NET 9 のパフォーマンス改善
nenonaninu
0
900
re:Invent 2024 Innovation Talks(NET201)で語られた大切なこと
shotashiratori
0
310
なぜCodeceptJSを選んだか
goataka
0
160
Fanstaの1年を大解剖! 一人SREはどこまでできるのか!?
syossan27
2
170
kargoの魅力について伝える
magisystem0408
0
210
Featured
See All Featured
Designing on Purpose - Digital PM Summit 2013
jponch
116
7k
Fontdeck: Realign not Redesign
paulrobertlloyd
82
5.3k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
The Pragmatic Product Professional
lauravandoore
32
6.3k
Building Your Own Lightsaber
phodgson
103
6.1k
The Invisible Side of Design
smashingmag
298
50k
It's Worth the Effort
3n
183
28k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
191
16k
The World Runs on Bad Software
bkeepers
PRO
65
11k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
44
9.3k
A better future with KSS
kneath
238
17k
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.5k
Transcript
͜ΕͰΘ͔Δ(?) B-tree ΞϧΰϦζϜ 2018/12/06 ࣾษڧձ ϋτωίΤ
ϋτωίΤ @nekonenene ࢲॳԻϛΫͰ͢ ͝ଘ͋ͷํ
• BͱݺΕΔ • σʔλߏͷҰछͰ͋ΔߏͷҰछ • ϧʔτ͔Βͷߴ͕͞Ұఆͷฏߧʢ͍͜͏͗ʣͷҰछ • Mongo DBͳͲͷσʔλϕʔεͷ΄͔ɺ WindowsͷϑΝΠϧγεςϜNTFS
MacͷϑΝΠϧγεςϜAPFS ͳͲͰΘΕ͍ͯΔ B-tree
• MySQLͷσʔλϕʔεΤϯδϯ InnoDB Ͱɺ ॱʑʹΞΫηε͢ΔੑೳΛ্͛ͨ B+ tree ͕ΘΕ͍ͯΔ • Oracle
Database MacͷϑΝΠϧγεςϜͩͬͨHFS+ Ͱ B* tree ͕ΘΕ͍ͯΔʢࠓճѻ͍·ͤΜʣ B-tree ѥछ
Χϯλϯͳͷ͔Β ࢝Ί͍ͯ͜͏
• ͬͱ୯७ͳߏ • খ͍͞ΛࠨԼʹɺେ͖͍ΛӈԼʹ͚͍ͭͯ͘ ೋ୳ࡧ
• 10 → 5 → 3 → 2 → 8
→ 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ ೋ୳ࡧɿྫ 10 5
• 10 → 5 → 3 → 2 → 8
→ 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ ೋ୳ࡧɿྫ 10 5 ϊʔυ ࢠϊʔυ
• 10 → 5 → 3 → 2 → 8
→ 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ ೋ୳ࡧɿྫ 10 5 3 2
• 10 → 5 → 3 → 2 → 8
→ 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ ೋ୳ࡧɿྫ 10 5 3 2 8
• 10 → 5 → 3 → 2 → 8
→ 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ ೋ୳ࡧɿྫ 10 5 3 2 8 4
• 10 → 5 → 3 → 2 → 8
→ 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ ೋ୳ࡧɿྫ 10 5 3 2 1 8 4
• 10 → 5 → 3 → 2 → 8
→ 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ ೋ୳ࡧɿྫ 10 5 3 2 1 8 4 6
• ͷόϥϯε͕ۉʹ͍ͬͯΕɺ ཁૉ n ʹରͯ͠୳ࡧʹ͔͔Δ࠷ѱܭࢉྔ O (log n) ʹͰ͖Δ •
ͭ·Γɺσʔλ͕૿͑ͯݕࡧͷ͕࣌ؒ૿͑ͳ͍ ೋ୳ࡧͷར 10 5 2 8 16 12 20 ཁૉ n ܭࢉྔ
• ࠨԼͷਤͷΑ͏ͳภͬͨߏͷ࠷ѱͷ߹Λߟ͑Δ • ཁૉ͕ଟ͍΄ͲൺֱΛ͓͜ͳ͏ͷͰ࠷ѱܭࢉྔ O (n) ೋ୳ࡧͷܽ ཁૉ n ܭࢉྔ
10 5 3 2
ͦΕͳΒͬͨ ߏʹ͠Α͏ʂ
• ߟҊऀͷ2ਓͷ໊લ͕༝དྷʢAdelson-Velskii and Landis' treeʣ • ࠨ෦ͷߴ͞ͱӈ෦ͷߴ͕͞1ΑΓେ͖͘ͳͬͨΒ ʮͷճసʯʹΑΓ࠶ߏ͠ɺฏߧΛอͭ AVL
• 10 → 5 → 3 → 2 → 8
→ 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ AVLɿྫ 10 5
• 10 → 5 → 3 → 2 → 8
→ 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ AVLɿྫ 10 5 3 ʮ10ʯ͔Βݟͯ ࠨߴ͞ 2 ӈߴ͞ 0 ʹͳͬͯ͠·͏ ͦ͜Ͱɺͷճసʂ
• 10 → 5 → 3 → 2 → 8
→ 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ AVLɿྫ 10 5 3 த৺ʹ͍ͨ ʮ5ʯΛʹͯ͠ ଞΛࢠʹ͢Δ
• 10 → 5 → 3 → 2 → 8
→ 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ AVLɿྫ 10 5 3 2
• 10 → 5 → 3 → 2 → 8
→ 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ AVLɿྫ 10 5 3 2 8
• 10 → 5 → 3 → 2 → 8
→ 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ AVLɿྫ 10 5 3 2 8 4
• 10 → 5 → 3 → 2 → 8
→ 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ AVLɿྫ 10 5 3 2 8 4 1
• 10 → 5 → 3 → 2 → 8
→ 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ AVLɿྫ 10 5 3 2 8 4 1 ͭͳ͛ΒΕΔՕॴͷߴ͞ͷ͕ࠩ2 ͜Εͷճస͕ى͜Δʁ ? ?
• 10 → 5 → 3 → 2 → 8
→ 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ AVLɿྫ 10 5 3 2 8 4 1 ʮ5ʯ͔Βݟͯ ࠨͷߴ͞ 3 ӈͷߴ͞ 2 ʮ3ʯ͔Βݟͯ ࠨͷߴ͞ 2 ӈͷߴ͞ 1 ࠨ෦ͱӈ෦ͷߴ͞ͷࠩΛ ݟΔͷͰɺ·ͩ͑·͢
• 10 → 5 → 3 → 2 → 8
→ 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ AVLɿྫ 10 5 3 2 8 4 1 6 ʮ10ʯ͔Βݟͯ ࠨͷߴ͞ 2 ӈͷߴ͞ 0 ͷճసνϟϯεʂ
• 10 → 5 → 3 → 2 → 8
→ 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ AVLɿྫ 10 5 3 2 8 4 1 6 த৺ʹ͍Δ ʮ8ʯΛʹ͠ɺ ଞ2ͭࢠʹ
• 10 → 5 → 3 → 2 → 8
→ 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ AVLɿྫ 8 5 3 2 6 4 1 10 ʂ
• ࠷ѱܭࢉྔ͕େ͖͘ͳΒͳ͍Α͏ɺ ͷճసΛ༻͍ɺͰ͖Δ͚ͩภΓͷͳ͍Λ࡞͍ͬͯͬͨ • ͜ͷΑ͏ͳฏୱʹ͚ۙΔߏΛ ฏߧʢ͍͜͏͗ʣͱݺͿ AVL ͓͞Β͍
• ͷߴ͕͞ߴ͘ͳΓ͍͢ • ݁Ռɺσʔλྔ͕ͱͯଟ͍߹ɺݕࡧʹ͕͔͔࣌ؒΔ • → ͦ͜Ͱ B-tree ʂ AVLͷܽ
• ֤ϊʔυʹ࣋ͨͤΔΛ1ͭͰͳ͘ෳʹ͠ɺͷߴ͞Λ͑Δ • ࠓ·ͰͷϊʔυXΑΓখ͍͔ͦ͞ΕҎ্͔ɺͷ 2ຊͷࢬ͔͍࣋ͬͯ͠ͳ͔͕ͬͨɺB-treeͰΑΓଟ͘ͷࢬΛ࣋ͭ • ྫ͑ X ͱ Y
ͷ 2 ͭͷΛϊʔυʹͭͱܾΊͨ߹ɺ Xະຬ / XҎ্ͰYະຬ / YҎ্ ͷ3ຊͷࢬΛϊʔυ࠷େͰ࣋ͭʢΦʔμʔ3ʣ ͦ͜Ͱ B-tree
• ϊʔυ͕࠷େͰ࣋ͭࢬͷʹԠͯ͡ɺ ʮΦʔμʔ m ͷB-treeʯͱ͍͏ݴ͍ํΛ͢Δ • Φʔμʔ 3 ʙ 5
͕Α͋͘ΔΒ͍͠ • ࠓճɺΦʔμʔ 3 ͷ B-tree ʢͭ·Γϊʔυʹ2ͭͷΛ࣋ͭʣ ʹ͍ͭͯྫΛݟ͍ͯ͘ɻ ࢠϊʔυΛ2ʙ3࣋ͭ͜ͱ͔Βɺ2-3 tree ͱݺΕΔ Φʔμʔ
• 10 → 5 → 3 → 2 → 8
→ 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B-treeɿྫ 5 10 ϊʔυʹ2ͭͷ Λͭ
• 10 → 5 → 3 → 2 → 8
→ 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B-treeɿྫ 5 10 ϊʔυʹ 3ͭͷ͕…… 3 த৺ͷϊʔυʹҠಈʂ
• 10 → 5 → 3 → 2 → 8
→ 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B-treeɿྫ 5 10 3
• 10 → 5 → 3 → 2 → 8
→ 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B-treeɿྫ 5 10 2 3
• 10 → 5 → 3 → 2 → 8
→ 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B-treeɿྫ 5 8 2 3 10
• 10 → 5 → 3 → 2 → 8
→ 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B-treeɿྫ 5 8 2 3 10 4
• 10 → 5 → 3 → 2 → 8
→ 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B-treeɿྫ 5 8 2 3 10 4 த৺ͷ ϊʔυʹҠಈʂ
• 10 → 5 → 3 → 2 → 8
→ 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B-treeɿྫ 5 8 2 3 10 4 Զ͕ϊʔυͩʂ த৺ͷ ϊʔυʹҠಈʂ
• 10 → 5 → 3 → 2 → 8
→ 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B-treeɿྫ 3 8 2 10 4 5
• 10 → 5 → 3 → 2 → 8
→ 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B-treeɿྫ 3 8 1 10 4 5 2
• 10 → 5 → 3 → 2 → 8
→ 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B-treeɿྫ 3 6 1 8 4 5 2 10 த৺ͷ ϊʔυʹҠಈʂ
• 10 → 5 → 3 → 2 → 8
→ 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B-treeɿྫ 3 6 1 8 4 5 2 10 ͋Ε……͓अຐʁ
• 10 → 5 → 3 → 2 → 8
→ 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B-treeɿྫ 3 6 1 8 4 5 2 10 த৺ͷ ϊʔυʹҠಈʂ
• 10 → 5 → 3 → 2 → 8
→ 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B-treeɿྫ 3 6 1 8 4 5 2 10 ߴ͞Ξοϓʂ ˍʂ
• جຊͷΞϧΰϦζϜ B-tree • ࠷ԼͷϊʔυΛϙΠϯλͰͭͳ͗߹Θͤͨ͜ͱͰɺ খ͞ͳ͔Βେ͖ͳॱʑʹΞΫηε͍ͯ͘͠ ͱ͍͏ڍಈͷύϑΥʔϚϯεΛ্͛Δ͜ͱʹޭ • ઌ΄Ͳಉ༷ɺΦʔμʔ 3
ʢϊʔυ͕࠷େͰ࣋ͭࢬ͕3ຊʣͷ B+ tree ʹ͍ͭͯݟ͍ͯ͘ B+ tree
• 10 → 5 → 3 → 2 → 8
→ 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B+ treeɿྫ 5 10
• 10 → 5 → 3 → 2 → 8
→ 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B+ treeɿྫ 5 10 ϊʔυʹ 3ͭͷ͕…… 3 த৺ͷϊʔυʹҠಈʂ
• 10 → 5 → 3 → 2 → 8
→ 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B+ treeɿྫ 5 5 3 10 ϊʔυҠಈͭͭ͠… ͱͷ͢ʂ
• 10 → 5 → 3 → 2 → 8
→ 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B+ treeɿྫ 5 5 3 10 ࠷Լಉ࢜Λ ϙΠϯλͰͭͳ͙ʂ ͜Ε͕ B+ tree ͷ Ұ൪ͷಛʂ
• 10 → 5 → 3 → 2 → 8
→ 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B+ treeɿྫ 5 5 3 10
• 10 → 5 → 3 → 2 → 8
→ 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B+ treeɿྫ 5 5 2 10 3
• 10 → 5 → 3 → 2 → 8
→ 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B+ treeɿྫ 5 5 2 8 3 10 த৺ͷ ϊʔυʹҠಈʂ
• 10 → 5 → 3 → 2 → 8
→ 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B+ treeɿྫ 5 5 2 8 3 10 8 ͱͷ͢
• 10 → 5 → 3 → 2 → 8
→ 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B+ treeɿྫ 5 5 2 8 3 10 8 4 த৺ͷ ϊʔυʹҠಈʂ
• 10 → 5 → 3 → 2 → 8
→ 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B+ treeɿྫ 5 5 2 8 3 10 8 4 3 ͱͷ͢
• 10 → 5 → 3 → 2 → 8
→ 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B+ treeɿྫ 5 5 8 3 10 8 4 3 3ͭʹͳͬͯ͠·ͬͨ…… 2
• 10 → 5 → 3 → 2 → 8
→ 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B+ treeɿྫ 5 5 8 3 10 8 4 3 2 த৺ͷ ϊʔυʹҠಈʂ 3ͭʹͳͬͯ͠·ͬͨ……
• 10 → 5 → 3 → 2 → 8
→ 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B+ treeɿྫ 5 3 8 5 Θ͔Γ͍͢Α͏ ্෦͚ͩߟ͑Δ ৽͍͠ύύͩΑ ͱͷ͢
• 10 → 5 → 3 → 2 → 8
→ 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B+ treeɿྫ 5 3 8 5 Θ͔Γ͍͢Α͏ ্෦͚ͩߟ͑Δ 5ະຬ 5Ҏ্
• 10 → 5 → 3 → 2 → 8
→ 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B+ treeɿྫ 5 5 8 3 10 8 4 3 2 ͳͷͰಉ༷ʹ……
• 10 → 5 → 3 → 2 → 8
→ 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B+ treeɿྫ 5 5 8 3 10 8 4 3 2 5 ͜͏ͳΔʂ
• 10 → 5 → 3 → 2 → 8
→ 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B+ treeɿྫ 5 5 8 3 10 8 4 3 2 5 ʢҙʣ ͜͏ͳΒͳ͍ Φʔμʔ 3 ͷ BͰ ࠷ 2 ͭͷ ࢠϊʔυΛ࣋ͭ
• 10 → 5 → 3 → 2 → 8
→ 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B+ treeɿྫ 5 5 8 3 10 8 4 3 2 5 Λͯ࣍͠…
• 10 → 5 → 3 → 2 → 8
→ 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B+ treeɿྫ 5 5 8 3 10 8 4 3 1 5 2
• 10 → 5 → 3 → 2 → 8
→ 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B+ treeɿྫ 5 5 8 3 10 8 4 3 1 5 2 6
• 10 → 5 → 3 → 2 → 8
→ 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B+ treeɿྫ 5 5 8 3 10 8 4 3 1 5 2 6 ʂ
• ࠷Լͷϊʔυ͕ϙΠϯλͰͭͳ͕͍ͬͯΔͨΊɺ খ͞ͳ͔Βେ͖ͳॱʑʹΞΫηε͍͖͍ͯͨ͠߹ʹɺ ຖճݕࡧͷखؒΛڬ·ͳͯ͘ࡁΉ • ͳ͓ɺ͜ͷ࠷ԼͷϊʔυΛ ʮϦʔϑϊʔυʢleaf nodesʣʯͱݴ͍·͢ B+ tree
͓͞Β͍
• 10 → 5 → 3 → 2 → 8
→ 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B+ treeɿྫ 5 5 8 3 10 8 4 3 1 5 2 6 Ϧʔϑϊʔυ
• ΧϥϜʹΠϯσοΫεΛషΔ͜ͱͰɺ B+ tree ͷߏ͕࡞ΒΕɺॱʑʹͳ͍ͬͯΔͷͰ ൣғݕࡧ͕εϐʔυग़͍͢͠ • ͳ͓ɺInno DB ͰϦʔϑϊʔυؒͷϙΠϯλ͕
ҰํͰͳ͘ํʹͳ͍ͬͯΔ Inno DB ʹ͓͍ͯ 5 8 10 4 3 1 2 6
• AVL https://www.cs.usfca.edu/~galles/visualization/AVLtree.html • B-tree https://www.cs.usfca.edu/~galles/visualization/BTree.html • B+ tree https://www.cs.usfca.edu/~galles/visualization/BPlusTree.html
• ͦͷଞ https://www.cs.usfca.edu/~galles/visualization/Algorithms.html ศརͳγϛϡϨʔλʔ