Skip to content

Commit

Permalink
Add new post
Browse files Browse the repository at this point in the history
  • Loading branch information
hhhyunwoo committed Jun 21, 2023
1 parent aed06a0 commit c415408
Showing 1 changed file with 85 additions and 0 deletions.
85 changes: 85 additions & 0 deletions _posts/2023-06-21-data-mysql-index-b-tree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
---
layout: post
title: "[Data Structure] MySQL Index ์ž๋ฃŒ๊ตฌ์กฐ B-tree"
date: 2023-06-21
categories:
- Data Structure
tags: [
data structure,
b-tree,
index,
hashtable,
]
---

Index ๋Š” ์šฐ๋ฆฌ๊ฐ€ ํ”ํžˆ ์•Œ๊ณ  ์žˆ๋“ฏ์ด, ์–ด๋– ํ•œ ๋‚ด์šฉ์„ ๋น ๋ฅด๊ฒŒ ์ฐพ๊ธฐ ์œ„ํ•œ ์ƒ‰์ธ์ด๋‹ค.

์ฑ…์„ ์ฝ์„ ๋•Œ๋„ Index ๋ฅผ ํ†ตํ•ด ์ฐพ๊ณ  ์‹ถ์€ ๋‚ด์šฉ์„ ๋น ๋ฅด๊ฒŒ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.

MySQL ๊ณผ ๊ฐ™์€ Database ์—์„œ๋„ ์ด๋Ÿฌํ•œ Index ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๋Š”๋ฐ, ์ด ๋•Œ ์ฃผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ์ž๋ฃŒ๊ตฌ์กฐ๋Š” B-tree ์ด๋‹ค.

๋‹จ์ˆœํžˆ ์ƒ๊ฐํ–ˆ์„ ๋•Œ, ๋น ๋ฅด๊ฒŒ ์ฐพ๋Š”๋‹ค๋ฉด Hash map ์„ ์ƒ๊ฐํ•  ์ˆ˜๋„ ์žˆ๊ณ , Binary Tree ๋ฅผ ๋– ์˜ฌ๋ ค ๋ณผ ์ˆ˜๋„ ์žˆ๋‹ค.

๊ทผ๋ฐ ์™œ B-tree๋ฅผ ์‚ฌ์šฉํ• ๊นŒ?

์ด๋ฒˆ ๊ธ€์—์„œ๋Š” B-tree ๊ฐ€ ๋ฌด์—‡์ด๊ณ  ์–ด๋–ค ์žฅ๋‹จ์ ์„ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฉฐ, ์™œ MySQL๊ณผ ๊ฐ™์€ Database ๋Š” B-tree๋ฅผ ์ž๋ฃŒ๊ตฌ์กฐ๋กœ ์„ ํƒํ–ˆ๋Š”์ง€ ๊ฐ„๋žตํ•˜๊ฒŒ ์•Œ์•„๋ณด์ž.

# B-tree ๋ž€

B-tree์˜ B ๋Š” ์ฃผ๋กœ `Balanced` ๋ผ๋Š” ๋œป์œผ๋กœ ์•Œ๋ ค์ ธ์žˆ๋‹ค. ์ฆ‰, Left ์™€ Right ์— ๊ท ํ˜•์ด ์žกํ˜€์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

B-tree ๋Š” Binary Tree๊ฐ€ ํ™•์žฅ๋œ ๊ตฌ์กฐ์ธ๋ฐ, ํ•˜๋‚˜์˜ ๋…ธ๋“œ๊ฐ€ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š” ์ž์‹ ๋…ธ๋“œ์˜ ์ตœ๋Œ€ ์ˆซ์ž๊ฐ€ 2๋ณด๋‹ค ํฐ ํŠธ๋ฆฌ ๊ตฌ์กฐ์ด๋‹ค.

<img width="952" alt="image" src="https://github.com/hhhyunwoo/hhhyunwoo/assets/37402136/b1d9c7da-ea14-4156-99d2-9d78f64cbc7f">

- ํ•œ ๋…ธ๋“œ์— ํ•œ๊ฐ€์ง€ ๊ฐ’์ด ์•„๋‹Œ ์—ฌ๋Ÿฌ ๊ฐ’์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค.
- ๋ฐ์ดํ„ฐ๊ฐ€ ์ •๋ ฌ๋œ ์ƒํƒœ๋กœ ์œ ์ง€๋˜์–ด ์žˆ๋‹ค.

## ์™œ B-tree๊ฐ€ ๋น ๋ฅธ๊ฐ€

- B-tree ์˜ ๊ฐ€์žฅ ํฐ ์žฅ์  ์ค‘ ํ•˜๋‚˜๋Š” ์–‘์ชฝ ์ž์‹์˜ ๊ท ํ˜•์„ ์œ ์ง€ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.
- ์ด๋ฅผ ํ†ตํ•ด์„œ ํƒ์ƒ‰ ์‹œ์— ๋ฌด์กฐ๊ฑด `O(logN)`์˜ Time Complexity ๋ฅผ ๋ณด์žฅํ•œ๋‹ค.
- ํ•˜์ง€๋งŒ ๋…ธ๋“œ์˜ ์‚ฝ์ž… ๋ฐ ์‚ญ์ œ ์‹œ์— ์žฌ์ •๋ ฌํ•˜๋Š” ๊ณผ์ •์—์„œ ๋งŽ์€ ์„ฑ๋Šฅ ์ €ํ•˜๋ฅผ ์ดˆ๋ž˜ํ•œ๋‹ค.

# B+tree ๋ž€

<img width="948" alt="image" src="https://github.com/hhhyunwoo/hhhyunwoo/assets/37402136/5b47c027-34eb-4d6e-9825-9b5d702b93f5">

B+tree ๋Š” B-tree ์˜ ํ™•์žฅ ๊ฐœ๋…์ธ๋ฐ, ๋ฐ์ดํ„ฐ๋ฅผ ์˜ค์ง Leaf Node์—๋งŒ ์ €์žฅํ•œ๋‹ค.

์ฆ‰, Branch ๋…ธ๋“œ์—๋Š” Key ๋งŒ ๋‹ด์•„๋‘๊ณ , ํ•ด๋‹น Key์˜ Data ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” Leaf Node ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ํฌ์ธํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.

## B+tree ์˜ ์žฅ์ 

- Leaf node๋ฅผ ์ œ์™ธํ•˜๊ณ  ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด์•„๋‘์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์—, ๋ฉ”๋ชจ๋ฆฌ ํ™•๋ณด์— ์œ ์šฉํ•จ.
- ํ•˜๋‚˜์˜ ๋…ธ๋“œ์— ๋” ๋งŽ์€ Key๋“ค์„ ๋‹ด์„ ์ˆ˜ ์žˆ์–ด์„œ, ํŠธ๋ฆฌ์˜ ๋†’์ด๊ฐ€ ๋” ๋‚ฎ์•„์ง

# Index ์ž๋ฃŒ๊ตฌ์กฐ๋กœ B-tree๋ฅผ ์„ ํƒํ•œ ์ด์œ 

## Hash table ์€ ์™œ ์•ˆ๋˜๋Š”๊ฐ€

<img width="890" alt="image" src="https://github.com/hhhyunwoo/hhhyunwoo/assets/37402136/c52fcedf-2336-41e2-a4c5-c412319a02c8">

`Hash Table` ์€ Key ๊ฐ’๋งŒ ์•ˆ๋‹ค๋ฉด ํƒ์ƒ‰์˜ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ `O(1)` ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋งค์šฐ ๋น ๋ฅด๋‹ค.

ํ•˜์ง€๋งŒ, ํ•ด๋‹น ๊ฐ’๋“ค์€ ์ •๋ ฌ๋˜์–ด ์žˆ๋‹ค๋Š” ๊ฒƒ์ด ๋ณด์žฅ๋˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ๋งŒ์•ฝ Index๋กœ Hash Table์„ ์“ด๋‹ค๋ฉด ***ํ•ด๋‹น Key์˜ ์ž‘๊ฑฐ๋‚˜ ํฐ ๊ฐ’์ด ์–ด๋””์žˆ๋Š”์ง€ ์ฐพ๋Š” ๋ถ€๋ถ„์— ์žˆ์–ด์„œ O(1) ์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ๋ณด์žฅํ• ์ˆ˜๋„ ์—†์„ ๋ฟ๋”๋Ÿฌ ๋งค์šฐ ๋น„ํšจ์œจ์ ***์ด๋‹ค. ์ฆ‰, **๋ฒ”์œ„์— ๋Œ€ํ•œ ์—ฐ์‚ฐ์— ๋น„ํšจ์œจ์ ์ธ ์ž๋ฃŒ๊ตฌ์กฐ**์ด๋‹ค.

## ์™œ Binary Tree๊ฐ€ ์•„๋‹ˆ๋ผ B-Tree์ธ๊ฐ€

์ด ๋‘˜์˜ ๊ฐ€์žฅ ํฐ ์ฐจ์ด๋Š” `ํ•˜๋‚˜์˜ ๋…ธ๋“œ๊ฐ€ ๊ฐ€์ง€๋Š” ๋ฐ์ดํ„ฐ ๊ฐœ์ˆ˜` ์ด๋‹ค.

<img width="851" alt="image" src="https://github.com/hhhyunwoo/hhhyunwoo/assets/37402136/9b4ca507-5c2d-4b94-ac76-bcaeb269e61d">

ref. [https://helloinyong.tistory.com/296](https://helloinyong.tistory.com/296)

# Reference

[https://ko.wikipedia.org/wiki/B_ํŠธ๋ฆฌ](https://ko.wikipedia.org/wiki/B_%ED%8A%B8%EB%A6%AC)

[https://ko.wikipedia.org/wiki/B%2B_ํŠธ๋ฆฌ](https://ko.wikipedia.org/wiki/B%2B_%ED%8A%B8%EB%A6%AC)

[https://en.wikipedia.org/wiki/Hash_table](https://en.wikipedia.org/wiki/Hash_table)

[https://zorba91.tistory.com/293](https://zorba91.tistory.com/293)

[https://helloinyong.tistory.com/296](https://helloinyong.tistory.com/296)

0 comments on commit c415408

Please sign in to comment.