14. Indexing
- Indexing(์ธ๋ฑ์ฑ): ์ํ๋ ๋ฐ์ดํฐ์ ๋ํ ์ ๊ทผ ์๋๋ฅผ ๋์ด๋ ๋ฐ ์ฌ์ฉ๋๋ ๋ฉ์ปค๋์ฆ
- Search Key(๊ฒ์ ํค): ํ์ผ์์ ๋ ์ฝ๋๋ฅผ ์ฐพ๋ ๋ฐ ์ฌ์ฉ๋๋ ์์ฑ ๋๋ ์์ฑ ์งํฉ
- Index file(์ธ๋ฑ์ค ํ์ผ)์ search-key์ pointer(s)() ํํ์ ๋ ์ฝ๋(index entries(์ธ๋ฑ์ค ์ํธ๋ฆฌ)๋ผ ๋ถ๋ฆผ)๋ก ๊ตฌ์ฑ
- ์ธ๋ฑ์ค ํ์ผ์ ์ผ๋ฐ์ ์ผ๋ก ์๋ณธ ๋ฐ์ดํฐ ํ์ผ๋ณด๋ค ํจ์ฌ ์์
- ๋ ๊ฐ์ง ๊ธฐ๋ณธ ์ธ๋ฑ์ค ์ข
๋ฅ
- Ordered indices(์์ ์ธ๋ฑ์ค): search key๊ฐ ์ ๋ ฌ๋ ์์๋ก ์ ์ฅ
- Hash indices(ํด์ ์ธ๋ฑ์ค): search key๊ฐ hash function(ํด์ ํจ์)์ ์ฌ์ฉํ์ฌ "buckets(๋ฒํท)"์ ๊ท ์ผํ๊ฒ ๋ถ์ฐ
- Index evaluation metrics(์ธ๋ฑ์ค ํ๊ฐ ์งํ)
- ํจ์จ์ ์ผ๋ก ์ง์๋๋ Access types(์ ๊ทผ ์ ํ)
- Point queries(ํฌ์ธํธ ์ฟผ๋ฆฌ): search key์ ๋ํด ์ง์ ๋ ๊ฐ์ ๊ฐ๋ ๋ ์ฝ๋
- Range queries(๋ฒ์ ์ฟผ๋ฆฌ): search key ๊ฐ์ด ์ง์ ๋ ๋ฒ์ ๋ด์ ์๋ ๋ ์ฝ๋
- ๋ฐ์ดํฐ ๋ ์ฝ๋์ ๋ํ Access/Insertion/Deletion times(์ ๊ทผ/์ฝ์ /์ญ์ ์๊ฐ)
- Space overhead(๊ณต๊ฐ ์ค๋ฒํค๋)
- ํจ์จ์ ์ผ๋ก ์ง์๋๋ Access types(์ ๊ทผ ์ ํ)
Ordered Indices
- Ordered index(์์ ์ธ๋ฑ์ค): index entries๊ฐ search key ๊ฐ์ ๋ฐ๋ผ ์ ๋ ฌ๋์ด ์ ์ฅ
- Clustering index(ํด๋ฌ์คํฐ๋ง ์ธ๋ฑ์ค)
- Sequentially ordered data file(์์ฐจ์ ์ผ๋ก ์ ๋ ฌ๋ ๋ฐ์ดํฐ ํ์ผ)์์, search key๊ฐ ๋ฐ์ดํฐ ํ์ผ์ ์์ฐจ์ ์์๋ ์ ์ํ๋ ์ธ๋ฑ์ค
- Primary index(๊ธฐ๋ณธ ์ธ๋ฑ์ค)๋ผ๊ณ ๋ ํจ
- ๊ธฐ๋ณธ ์ธ๋ฑ์ค์ search key๋ ์ผ๋ฐ์ ์ผ๋ก primary key(๊ธฐ๋ณธ ํค)์ด์ง๋ง, ํ์๋ ์๋
- Secondary index(๋ณด์กฐ ์ธ๋ฑ์ค)
- search key๊ฐ ๋ฐ์ดํฐ ํ์ผ์ ์์ฐจ์ ์์์ ๋ค๋ฅธ ์์๋ฅผ ์ง์ ํ๋ ์ธ๋ฑ์ค
- Nonclustering index(๋นํด๋ฌ์คํฐ๋ง ์ธ๋ฑ์ค)๋ผ๊ณ ๋ ํจ
- Index-sequential file(์ธ๋ฑ์ค-์์ฐจ ํ์ผ)
- search key๋ฅผ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌ๋ Sequential data file์, search key์ ๋ํ clustering index๊ฐ ์๋ ํ์ผ
Dense Index
- Dense index(๋ฐ์ง ์ธ๋ฑ์ค): ๋ฐ์ดํฐ ํ์ผ์ ๋ชจ๋ search-key ๊ฐ์ ๋ํด index entry๊ฐ ๋ํ๋จ
Sparse Index
- Sparse Index(ํฌ์ ์ธ๋ฑ์ค): ์ผ๋ถ search-key ๊ฐ์ ๋ํด์๋ง index entries๋ฅผ ํฌํจ
- ๋ ์ฝ๋๊ฐ search key๋ฅผ ๊ธฐ์ค์ผ๋ก ์์ฐจ์ ์ผ๋ก ์ ๋ ฌ๋์ด ์์ ๋ ์ ์ฉ ๊ฐ๋ฅ
- search-key ๊ฐ ๋ฅผ ๊ฐ์ง ๋ ์ฝ๋๋ฅผ ์ฐพ์ผ๋ ค๋ฉด
- ๋ณด๋ค ์๊ฑฐ๋ ๊ฐ์ search-key ๊ฐ ์ค ๊ฐ์ฅ ํฐ ๊ฐ์ ๊ฐ์ง index entry๋ฅผ ์ฐพ์
- ํด๋น index record๊ฐ ๊ฐ๋ฆฌํค๋ ๋ ์ฝ๋์์๋ถํฐ ํ์ผ ์์ฐจ ๊ฒ์
- Sparse Index(Cont.)
- Dense indices์ ๋น๊ต
- ๋ฐ์ดํฐ ๋ ์ฝ๋์ insertion(์ฝ์ ) ๋ฐ deletion(์ญ์ )์ ๋ํด Less space and less maintenance overhead(๋ ์ ์ ๊ณต๊ฐ๊ณผ ๋ ์ ์ ์ ์ง ๊ด๋ฆฌ ์ค๋ฒํค๋)
- ์ผ๋ฐ์ ์ผ๋ก ๋ ์ฝ๋๋ฅผ ์ฐพ๋ ๋ฐ Dense index๋ณด๋ค slower(๋๋ฆผ)
- Access time(์ ๊ทผ ์๊ฐ)๊ณผ space overhead(๊ณต๊ฐ ์ค๋ฒํค๋) ์ฌ์ด์ trade-off(๊ท ํ)
- Good compromise(์ข์ ์ ์ถฉ์)
- Clustering index์ ๊ฒฝ์ฐ: ํ์ผ์ ๋ชจ๋ block(๋ธ๋ก)์ ๋ํด index entry๋ฅผ ๊ฐ์ง sparse index. ํด๋น block์์ least search-key value(๊ฐ์ฅ ์์ search-key ๊ฐ)์ ํด๋น
- Note) Query processing(์ฟผ๋ฆฌ ์ฒ๋ฆฌ)์ ์ฃผ์ ๋น์ฉ์ block I/O time(๋ธ๋ก I/O ์๊ฐ); ๋ฉ๋ชจ๋ฆฌ ๋ด ๋ธ๋ก ์ค์บ๋ ์๊ฐ์ negligible(๋ฌด์ํ ์ ์์)
- ์ด ๋ฐฉ์์ dense index์ ๋์ผํ ์์ block I/O๋ฅผ ๊ฐ์ง๋ฉด์๋ space overhead๊ฐ ํจ์ฌ ์ ์
- Dense indices์ ๋น๊ต
Multilevel Index
- Index๊ฐ ๋ฉ๋ชจ๋ฆฌ์ ๋ง์ง ์์ผ๋ฉด, access๊ฐ expensive(๋น์ฉ์ด ๋ง์ด ๋ฆ)
- Solution(ํด๊ฒฐ์ฑ
): Multilevel Index(๋ค๋จ๊ณ ์ธ๋ฑ์ค)
- ๋์คํฌ์ ๋ณด๊ด๋ ์ธ๋ฑ์ค๋ฅผ sequential file(์์ฐจ ํ์ผ)๋ก ์ทจ๊ธํ๊ณ ๊ทธ ์์ sparse index๋ฅผ ๊ตฌ์ถ
- Outer index(์ธ๋ถ ์ธ๋ฑ์ค): basic index(๊ธฐ๋ณธ ์ธ๋ฑ์ค)์ sparse index
- Inner index(๋ด๋ถ ์ธ๋ฑ์ค): basic index file
- Outer index์กฐ์ฐจ ๋๋ฌด ์ปค์ ๋ฉ์ธ ๋ฉ๋ชจ๋ฆฌ์ ๋ง์ง ์์ผ๋ฉด, ๋ ๋ค๋ฅธ level์ index ์์ฑ ๊ฐ๋ฅ
- ๋ชจ๋ level์ indices๋ ํ์ผ์์์ insertion ๋๋ deletion ์์ updated(๊ฐฑ์ )๋์ด์ผ ํจ
Index Update: Insertion
- Index๋ database modification(๋ฐ์ดํฐ๋ฒ ์ด์ค ์์ )์ ์ค๋ฒํค๋๋ฅผ ๋ถ๊ณผ
- ๋ ์ฝ๋๊ฐ ์ฝ์ ๋๋ ์ญ์ ๋ ๋, ๊ด๊ณํ์ ๋ชจ๋ ์ธ๋ฑ์ค๊ฐ ๊ฐฑ์ ๋์ด์ผ ํจ
- ๋ ์ฝ๋๊ฐ ๊ฐฑ์ ๋ ๋, ๊ฐฑ์ ๋ ์์ฑ์ ๋ํ ๋ชจ๋ ์ธ๋ฑ์ค๊ฐ ๊ฐฑ์ ๋์ด์ผ ํจ
- Index update upon insertion:~1) Dense indices
- ์ฝ์ ๋๋ ๋ ์ฝ๋์ search-key ๊ฐ์ ์ฌ์ฉํ์ฌ lookup(์กฐํ) ์ํ
- search-key ๊ฐ์ด ์ธ๋ฑ์ค์ ๋ํ๋์ง ์์ผ๋ฉด, insert(์ฝ์ )
- search-key ๊ฐ์ด ์ธ๋ฑ์ค์ ๋ํ๋๋ฉด
- ์ธ๋ฑ์ค ์ํธ๋ฆฌ๊ฐ ๋์ผํ search-key ๊ฐ์ ๊ฐ์ง ๋ชจ๋ ๋ ์ฝ๋์ ๋ํ pointers๋ฅผ ์ ์ฅํ๋ ๊ฒฝ์ฐ, ์๋ก์ด ๋ ์ฝ๋์ ๋ํ pointer๋ฅผ add(์ถ๊ฐ)
- ๊ทธ๋ ์ง ์์ ๊ฒฝ์ฐ, ์ธ๋ฑ์ค ์ํธ๋ฆฌ๊ฐ ํด๋น search-key ๊ฐ์ ๊ฐ์ง ์ฒซ ๋ฒ์งธ ๋ ์ฝ๋์ ๋ํ pointer๋ง ์ ์ฅ. ์๋ก์ด ๋ ์ฝ๋๋ฅผ ๋์ผํ search-key ๊ฐ์ ๊ฐ์ง ๋ค๋ฅธ ๋ ์ฝ๋๋ค ๋ค์ ๋ฐฐ์น
- Indices๋ sequential files๋ก ์ ์ง ๊ด๋ฆฌ ๋จ ์๋ก์ด ์ํธ๋ฆฌ๋ฅผ ์ํ space๋ฅผ ์์ฑํด์ผ ํ๋ฉฐ, overflow blocks(์ค๋ฒํ๋ก ๋ธ๋ก)์ด ํ์ํ ์ ์์
- Index update upon insertion: 2) Sparse indices
- ์ฝ์ ๋๋ ๋ ์ฝ๋์ search-key ๊ฐ์ ์ฌ์ฉํ์ฌ lookup ์ํ
- ์ธ๋ฑ์ค๊ฐ ํ์ผ์ ๊ฐ ๋ธ๋ก์ ๋ํ ์ํธ๋ฆฌ๋ฅผ ์ ์ฅํ๋ ๊ฒฝ์ฐ, unless(๋ค์์ ๊ฒฝ์ฐ๊ฐ ์๋๋ผ๋ฉด) ์ธ๋ฑ์ค๋ฅผ ์์ ํ ํ์๊ฐ ์์
- ์๋ก์ด block์ด ์์ฑ๋๋ ๊ฒฝ์ฐ(๊ธฐ์กด ๋ธ๋ก์ด ์ด๋ฏธ ๊ฐ๋ ์ฐผ๊ธฐ ๋๋ฌธ): ์๋ก์ด ๋ธ๋ก์ first search-key value(์ฒซ ๋ฒ์งธ search-key ๊ฐ)์ด ์ธ๋ฑ์ค์ ์ฝ์ ๋จ
- ์๋ก์ด ๋ ์ฝ๋๊ฐ ๋ธ๋ก์์ least search-key value(๊ฐ์ฅ ์์ search-key ๊ฐ)๋ฅผ ๊ฐ์ง๋ ๊ฒฝ์ฐ: ๋ธ๋ก์ ๊ฐ๋ฆฌํค๋ index entry๋ฅผ update(๊ฐฑ์ )
Index Update: Deletion
- Index update upon deletion:~1) Dense indices -(Case~1) ์ญ์ ๋ ๋ ์ฝ๋๊ฐ ํด๋น search-key ๊ฐ์ ๊ฐ์ง ํ์ผ ๋ด ์ ์ผํ ๋ ์ฝ๋์ธ ๊ฒฝ์ฐ, ์ธ๋ฑ์ค์์๋ search-key๊ฐ deleted(์ญ์ )๋จ
- ๊ทธ๋ ์ง ์์ ๊ฒฝ์ฐ
- ์ธ๋ฑ์ค ์ํธ๋ฆฌ๊ฐ ๋์ผํ search-key ๊ฐ์ ๊ฐ์ง ๋ชจ๋ ๋ ์ฝ๋์ ๋ํ pointers๋ฅผ ์ ์ฅํ๋ ๊ฒฝ์ฐ, ์ญ์ ๋ ๋ ์ฝ๋์ ๋ํ pointer๋ฅผ delete(์ญ์ )
- ๊ทธ๋ ์ง ์์ ๊ฒฝ์ฐ, ์ธ๋ฑ์ค ์ํธ๋ฆฌ๊ฐ ํด๋น search-key ๊ฐ์ ๊ฐ์ง ์ฒซ ๋ฒ์งธ ๋ ์ฝ๋์ ๋ํ pointer๋ง ์ ์ฅ -(Case 2) ์ญ์ ๋ ๋ ์ฝ๋๊ฐ ํด๋น search-key ๊ฐ์ ๊ฐ์ง ์ฒซ ๋ฒ์งธ ๋ ์ฝ๋์ธ ๊ฒฝ์ฐ, ์ธ๋ฑ์ค ์ํธ๋ฆฌ๋ฅผ updateํ์ฌ ๋ค์ ๋ ์ฝ๋๋ฅผ ๊ฐ๋ฆฌํค๋๋ก ํจ -(Case 3) ๊ทธ๋ ์ง ์์ ๊ฒฝ์ฐ, ์ธ๋ฑ์ค ๊ฐฑ์ ์ required(์๊ตฌ๋์ง ์์)
- ๊ทธ๋ ์ง ์์ ๊ฒฝ์ฐ
- Index update upon deletion: 2) Sparse indices -(Case~1) ์ธ๋ฑ์ค๊ฐ ์ญ์ ๋ ๋ ์ฝ๋์ search-key ๊ฐ๊ณผ ์ผ์นํ๋ index entry๋ฅผ ํฌํจํ์ง ์๋ ๊ฒฝ์ฐ, do nothing(์๋ฌด๊ฒ๋ ํ์ง ์์)
- ๊ทธ๋ ์ง ์์ ๊ฒฝ์ฐ -(Case 2) ์ญ์ ๋ ๋ ์ฝ๋๊ฐ ํด๋น search-key๋ฅผ ๊ฐ์ง ์ ์ผํ ๋ ์ฝ๋์ธ ๊ฒฝ์ฐ, index entry๋ฅผ ํ์ผ์์ next search-key value(๋ค์ search-key ๊ฐ)๋ก replace(๋์ฒด) - ๋ค์ search-key ๊ฐ์ด ์ด๋ฏธ index entry๋ฅผ ๊ฐ์ง๊ณ ์๋ค๋ฉด, ํด๋น ์ํธ๋ฆฌ๋ deleted๋จ -(Primary index for non-key attribute(๋น-ํค ์์ฑ์ ๋ํ ๊ธฐ๋ณธ ์ธ๋ฑ์ค)) ๊ทธ๋ ์ง ์์ ๊ฒฝ์ฐ, search-key ๊ฐ์ ๋ํ index entry๊ฐ ์ญ์ ๋ ๋ ์ฝ๋๋ฅผ ๊ฐ๋ฆฌํค๋ ๊ฒฝ์ฐ, ์ธ๋ฑ์ค ์ํธ๋ฆฌ๋ฅผ updateํ์ฌ ๋์ผํ search-key ๊ฐ์ ๊ฐ์ง next record(๋ค์ ๋ ์ฝ๋)๋ฅผ ๊ฐ๋ฆฌํค๋๋ก ํจ
Secondary Indices: An example
instructorํ์ผ์salaryํ๋์ ๋ํ Secondary index(nonunique search key(๊ณ ์ ํ์ง ์์ search key))- Index record๋ ํด๋น ํน์ search-key ๊ฐ์ ๊ฐ์ง ๋ชจ๋ ์ค์ ๋ ์ฝ๋์ ๋ํ pointers๋ฅผ ํฌํจํ๋ bucket์ ๊ฐ๋ฆฌํด
- Secondary indices๋ dense(๋ฐ์ง)ํด์ผ ํจ
- Index update ๊ณผ์ ์ clustering index์ dense index ๊ฒฝ์ฐ์ ๋์ผ
- Secondary(nonclustering) index๋ฅผ ์ฌ์ฉํ Sequential scan(์์ฐจ ์ค์บ)์ HDD์์ expensive(๋น์ฉ์ด ๋ง์ด ๋ฆ)
- ๊ฐ ๋ ์ฝ๋ ์ ๊ทผ ์ ๋์คํฌ์์ ์๋ก์ด block์ ๊ฐ์ ธ์์ผ ํ ์ ์์
Indices on Multiple Keys
- Composite search key(๋ณตํฉ search key): ๋ ๊ฐ ์ด์์ ์์ฑ์ ํฌํจํ๋ search key
- E.g.,
instructorrelation์ ์์ฑ(name, ID)์ ๋ํ ์ธ๋ฑ์ค
- E.g.,
- ๊ฐ์ lexicographically(์ฌ์ ์์ผ๋ก) ์ ๋ ฌ๋จ
- E.g. $ (\text{John},~12121) <(\text{John},~13514)$ ๋ฐ $ (\text{John},~13514) <(\text{Peter},~11223)$
name๋ง์ผ๋ก ์ฟผ๋ฆฌํ๊ฑฐ๋,(name, ID)๋ก ์ฟผ๋ฆฌ ๊ฐ๋ฅ
B -Tree(and B-Tree) Index Files
B -Tree Index Files
- Index-sequential file organization์ Disadvantage(๋จ์ )
- ํ์ผ์ด ์ปค์ง์ ๋ฐ๋ผ overflow blocks์ด ๋ง์ด ์์ฑ๋์ด ์ฑ๋ฅ ์ ํ(์ธ๋ฑ์ค ์กฐํ ๋ฐ ์์ฐจ ์ค์บ ๋ชจ๋)
- ๋น์ฉ์ด ๋ง์ด ๋ค๊ณ ์ฃผ๊ธฐ์ ์ธ ์ ์ฒด ํ์ผ reorganization(์ฌ๊ตฌ์ฑ)์ด ํ์
- B -tree index structure์ Advantage(์ฅ์ )
- Insertion(์ฝ์ ) ๋ฐ deletion(์ญ์ ) ์ small, local changes(์๊ณ ๊ตญ์์ ์ธ ๋ณ๊ฒฝ)๋ก ์๋์ผ๋ก self-reorganizes(์์ฒด ์ฌ๊ตฌ์ฑ)
- ์ฑ๋ฅ ์ ์ง๋ฅผ ์ํด ์ ์ฒด ํ์ผ์ reorganization์ด ํ์ํ์ง ์์
- B -trees์(Minor) disadvantage((์ฌ์ํ) ๋จ์ )
- Extra insertion and deletion overhead, space overhead(์ถ๊ฐ์ ์ธ ์ฝ์ ๋ฐ ์ญ์ ์ค๋ฒํค๋, ๊ณต๊ฐ ์ค๋ฒํค๋)
- B -trees์ ์ฅ์ ์ด ๋จ์ ๋ณด๋ค ํฌ๊ธฐ ๋๋ฌธ์ extensively(๊ด๋ฒ์ํ๊ฒ) ์ฌ์ฉ๋จ
- B -tree๋ ๋ค์ ์์ฑ์ ๋ง์กฑํ๋ rooted tree(๋ฃจํธ๊ฐ ์๋ ํธ๋ฆฌ)
- Root์์ leaf(๋ฆฌํ)๊น์ง์ ๋ชจ๋ ๊ฒฝ๋ก๋ ๊ธธ์ด๊ฐ ๊ฐ์: Balanced tree(๊ท ํ ํธ๋ฆฌ)
- Root๋ leaf๊ฐ ์๋ ๊ฐ node(i.e., internal node(๋ด๋ถ ๋
ธ๋))๋ ์ ์ฌ์ด์ children(์์)์ ๊ฐ์ง(์ ํน์ ํธ๋ฆฌ์ ๋ํด ๊ณ ์ )
- At least and at most children(pointers)
- Leaf node๋ ์ ์ฌ์ด์ values(๊ฐ)์ ๊ฐ์ง
- At least and at most values(not pointers)
- Special cases(ํน์ ๊ฒฝ์ฐ)
- Root๊ฐ leaf๊ฐ ์๋ ๊ฒฝ์ฐ, at least 2 children์ ๊ฐ์ง
- Root๊ฐ leaf์ธ ๊ฒฝ์ฐ(์ฆ, ํธ๋ฆฌ์ ๋ค๋ฅธ ๋ ธ๋๊ฐ ์๋ ๊ฒฝ์ฐ), 0๊ณผ ์ฌ์ด์ values๋ฅผ ๊ฐ์ง ์ ์์
- B -tree๋ multilevel index(๋ค๋จ๊ณ ์ธ๋ฑ์ค)์ด์ง๋ง, multilevel index-sequential file๊ณผ๋ ๋ค๋ฅธ ๊ตฌ์กฐ๋ฅผ ๊ฐ์ง
B -Tree Node Structure
- Typical node(์ผ๋ฐ์ ์ธ ๋
ธ๋)
... - ๋ search-key values
- ๋ children(non-leaf nodes์ ๊ฒฝ์ฐ) ๋๋ records/buckets of records(leaf nodes์ ๊ฒฝ์ฐ)์ ๋ํ pointers
- Note: ์ต๋ ๊ฐ์ pointers์ ๊ฐ์ key values๊ฐ ์์ ์ ์์
- ๋ ธ๋ ๋ด์ search-keys๋ ordered(์์๊ฐ ์ง์ ๋จ): (์ด๊ธฐ์๋ ์ค๋ณต ํค๊ฐ ์๋ค๊ณ ๊ฐ์ )
Leaf Nodes in B -Trees
- Leaf node์ Properties(์์ฑ)
- ์ ๋ํด, pointer ๋ search-key value ๋ฅผ ๊ฐ์ง file record๋ฅผ ๊ฐ๋ฆฌํด
- ์ ๊ฐ leaf nodes์ด๊ณ ์ธ ๊ฒฝ์ฐ(๊ฐ ํธ๋ฆฌ์์ ์ ์ผ์ชฝ์ ์์), ์ search-key values๋ ์ search-key values๋ณด๋ค ์์
- ์ search-key order๋ก next leaf node(๋ค์ leaf node)๋ฅผ ๊ฐ๋ฆฌํด
- ์์ฐจ ์ฒ๋ฆฌ๋ฅผ ์ ์ํ๊ฒ ํ๊ธฐ ์ํด ๋ชจ๋ leaf nodes๋ฅผ search-key order๋ก Chain together(์ฐ๊ฒฐ)
- B -tree index๊ฐ dense index(์ผ๋ฐ์ ์ธ ๊ฒฝ์ฐ)๋ก ์ฌ์ฉ๋๋ ๊ฒฝ์ฐ, ๋ชจ๋ search-key value๊ฐ ์ผ๋ถ leaf node์ ๋ํ๋์ผ ํจ. ๊ทธ๋ฌ๋ non-leaf node์ ๋ํ๋๋ search-key๋ ๋ ์ฝ๋ ์ญ์ ๋ก ์ธํด leaf node์ ๋ํ๋์ง ์์ ์ ์์(๋์ค์ ํ์ธ)
Non-Leaf Nodes in B -Trees
- Non-leaf nodes๋ leaf nodes์ ๋ํ multi-level sparse index๋ฅผ ํ์ฑ
- ๊ฐ์ pointers๋ฅผ ๊ฐ์ง non-leaf node์ ๊ฒฝ์ฐ
- ์ด ๊ฐ๋ฆฌํค๋ subtree์ All search-keys๋ ๋ณด๋ค ์์
- ์ ๋ํด, ๊ฐ ๊ฐ๋ฆฌํค๋ subtree์ All search-keys๋ ๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ๊ณ ๋ณด๋ค ์์
- ์ด ๊ฐ๋ฆฌํค๋ subtree์ All search-keys๋ ๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ์
- Note: Non-leaf nodes๋ ๊ทธ๋ค ์ฌ์ด์ duplicate search-key values๋ฅผ ๊ฐ์ง์ง ์์
- General structure
Observations about B -trees
- Inter-node connections(๋ ธ๋ ๊ฐ ์ฐ๊ฒฐ)์ด pointers๋ก ์ด๋ฃจ์ด์ง๊ธฐ ๋๋ฌธ์, "logically(๋ ผ๋ฆฌ์ ์ผ๋ก)" ๊ฐ๊น์ด blocks์ด "physically(๋ฌผ๋ฆฌ์ ์ผ๋ก)" ๊ฐ๊น์ธ ํ์๋ ์์
- B -tree์ non-leaf levels์ hierarchy of sparse indices(ํฌ์ ์ธ๋ฑ์ค์ ๊ณ์ธต ๊ตฌ์กฐ)๋ฅผ ํ์ฑ
- B -tree๋ ์๋์ ์ผ๋ก small number of levels(์ ์ ์์ ๋ ๋ฒจ)์ ํฌํจ
- Root ์๋ ๋ ๋ฒจ์ at least values
- ๋ค์ ๋ ๋ฒจ์ at least values
- ํ์ผ์ ๊ฐ์ search-key values๊ฐ ์๋ ๊ฒฝ์ฐ, tree height(ํธ๋ฆฌ ๋์ด)๋ ๋ฅผ ์ด๊ณผํ์ง ์์
- ๋ฐ๋ผ์ searches(๊ฒ์)๊ฐ ํจ์จ์ ์ผ๋ก ์ํ๋ ์ ์์
- Index๊ฐ logarithmic time(๋ก๊ทธ ์๊ฐ)์ผ๋ก ์ฌ๊ตฌ์ฑ๋ ์ ์์ผ๋ฏ๋ก, main file(๋ฉ์ธ ํ์ผ)์ ๋ํ insertions ๋ฐ deletions๋ ํจ์จ์ ์ผ๋ก ์ฒ๋ฆฌ๋ ์ ์์
Queries on B -Trees: Point Query
function find(v)
1. Set C = root node
2. while(C is not a leaf node) begin
Let i = smallest number s.t. v โค C.Ki
if there is no such number i then
/* v is larger than every key in C */
Set C = the node pointed by the last non-null pointer in C
else if(v = C.Ki ) Set C = C.Pi +1
else set C = C.Pi /* v < C.Ki */
end
/* Now, C is a leaf node */
3. if for some i, Ki = v then return C.Pi
4. else return null /* no record with search-key value v exists */
Queries on B -Trees: Range Query
- Range queries: ์ฃผ์ด์ง ๋ฒ์ ๋ด์ search key ๊ฐ์ ๊ฐ์ง all records(๋ชจ๋ ๋ ์ฝ๋)๋ฅผ ์ฐพ์
function findRange(lb, ub)๋ ์ธ search key value ๋ฅผ ๊ฐ์ง ๋ชจ๋ ๋ ์ฝ๋ ์งํฉ์ ๋ฐํ -~1. =$ \text{lb} $๊ฐ ๋ํ๋ leaf node๋ฅผ ์ฐพ์(find(v)์์ ๋ฅผ ์ฐพ๋ ๊ฒ๊ณผ ๋์ผ)- ์์ ์ธ smallest value(๊ฐ์ฅ ์์ ๊ฐ)
while()
- ๋ฅผ results์ Add
- (if more records in) or move to the next leaf node setting
- Real implementations(์ค์ ๊ตฌํ)์ ์ผ๋ฐ์ ์ผ๋ก
next()ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ์ผ์นํ๋ ๋ ์ฝ๋๋ฅผ one at a time(ํ ๋ฒ์ ํ๋์ฉ) ๊ฐ์ ธ์ค๋ iterator interface(๋ฐ๋ณต์ ์ธํฐํ์ด์ค)๋ฅผ ์ ๊ณต
Queries on B -Trees: Cost Analysis
- ํ์ผ์ ๊ฐ์ search-key values๊ฐ ์๋ ๊ฒฝ์ฐ, ํธ๋ฆฌ์ height๋ ๋ฅผ ์ด๊ณผํ์ง ์์
- Node๋ ์ผ๋ฐ์ ์ผ๋ก disk block(๋์คํฌ ๋ธ๋ก)๊ณผ ๊ฐ์ ํฌ๊ธฐ, ์ผ๋ฐ์ ์ผ๋ก 4KB
- ์ ์ผ๋ฐ์ ์ผ๋ก ์ฝ~100(40 Bytes/์ธ๋ฑ์ค ์ํธ๋ฆฌ = 32 Bytes/search key + 8 Bytes/disk block pointer)
- Search-key size๊ฐ~12 Bytes์ธ ๊ฒฝ์ฐ(20 Bytes/์ํธ๋ฆฌ ํฌ๊ธฐ), ์ ์ฝ 200 -~1 million search key values ๋ฐ ์ธ ๊ฒฝ์ฐ
- Root์์ leaf๊น์ง์ index lookup(์ธ๋ฑ์ค ์กฐํ)์ ๋ํด At most nodes accessed(์ ๊ทผ) -~1 million search key values๋ฅผ ๊ฐ์ง balanced binary tree(๊ท ํ ์ด์ง ํธ๋ฆฌ)์ ๋น๊ต: ์กฐํ ์ ์ฝ 20 nodes accessed
- ๋ชจ๋ node access๋ disk I/O๋ฅผ ํ์๋ก ํ ์ ์์ผ๋ฉฐ, ์ฝ 20 milliseconds์ ๋น์ฉ์ด ๋ค๊ธฐ ๋๋ฌธ์ ์์ ์ฐจ์ด๋ significant(์ค์)
- Index๋ฅผ traverse(์ํ)ํ ํ, ์ผ์นํ๋ ๋ ์ฝ๋๋ฅผ fetch(๊ฐ์ ธ์ค๊ธฐ) ์ํด one more(random) I/O๊ฐ ํ์
Non-Unique Keys
- Search key ๊ฐ not unique(๊ณ ์ ํ์ง ์์ ๊ฒฝ์ฐ), ๋์ unique(๊ณ ์ )ํ composite key $ (a_i, A_{\text{pp}})$์ ๋ํ ์ธ๋ฑ์ค๋ฅผ ์์ฑ
- ๋ primary key, record ID ๋๋ uniqueness๋ฅผ ๋ณด์ฅํ๋ ๊ธฐํ attribute(์์ฑ)์ผ ์ ์์
- Search for ๋ composite key์ ๋ํ range search(๋ฒ์ ๊ฒ์)์ผ๋ก ๊ตฌํ ๊ฐ๋ฅ
- Range $ (v, -\infty) (v, +\infty)$
- But more I/O operations(๋ ๋ง์ I/O ์์
)์ด ์ค์ ๋ ์ฝ๋๋ฅผ fetchํ๋ ๋ฐ ํ์
- Index๊ฐ clustering์ธ ๊ฒฝ์ฐ, ๋ชจ๋ access๋ sequential(์์ฐจ์ )
- Index๊ฐ non-clustering์ธ ๊ฒฝ์ฐ, ๊ฐ record access๋ I/O operation์ ํ์๋ก ํ ์ ์์
Updates on B -Trees: Insertion
- Record๊ฐ data file์ already added(์ด๋ฏธ ์ถ๊ฐ)๋์๋ค๊ณ ๊ฐ์
- ๋ฐ๋ ๊ฐ๊ฐ record์ ๋ํ pointer ๋ฐ search key value
- search-key value๊ฐ ๋ํ๋ leaf node ์ Find(์ฐพ์)
- ์ room(๊ณต๊ฐ)์ด ์๋ ๊ฒฝ์ฐ, ์์ ์ insert(Note: leaf node์๋ ์ต๋ ์)
- ๊ทธ๋ ์ง ์์ ๊ฒฝ์ฐ, node๋ฅผ split(์๋ก์ด $ (v, P_r)$ ์ํธ๋ฆฌ๋ฅผ ํฌํจํ์ฌ)
- Splitting a leaf node :
- ์ ๋ ฌ๋ ์์๋ก ๊ฐ์(search-key, pointer) ์์ ์ทจํจ(์ฝ์ ๋๋ ์ ํฌํจ). ์ฒซ ๋ฒ์งธ ๋ฅผ original node $ (L)$์ ๋ฐฐ์นํ๊ณ , ๋๋จธ์ง๋ฅผ new node $ (L')$์ ๋ฐฐ์น
- ๋ฅผ ์ least key value๋ผ๊ณ ํจ. $ (k, L')$๋ฅผ split๋๋ node์ parent(๋ถ๋ชจ)์ insert
- Parent๊ฐ full(๊ฐ๋ ์ฐฌ ๊ฒฝ์ฐ), parent๋ฅผ splitํ๊ณ split์ ๋ ์๋ก propagate(์ ํ)
- Splitting of nodes๋ full์ด ์๋ node๋ฅผ ์ฐพ์ ๋๊น์ง ์๋ก ์งํ
- ์ต์ ์ ๊ฒฝ์ฐ root node๊ฐ split๋์ด ํธ๋ฆฌ์ height๊ฐ~1 increase(์ฆ๊ฐ)ํ ์ ์์
- Splitting a leaf node :
- Splitting a non-leaf node: ์ด๋ฏธ full์ธ internal node ์ $ (k, L')$๋ฅผ ์ฝ์
ํ ๋
- ์ pointers์keys๋ฅผ ์ํ ๊ณต๊ฐ์ด ์๋ in-memory area์ผ๋ก Copy(๋ณต์ฌ)
- $ (k, L')$๋ฅผ ์ Insert
- ์์ ๋ฅผ ๋ค์ node ์ผ๋ก Copy
- ์์ ๋ฅผ ์๋ก ํ ๋น๋ node ์ผ๋ก Copy
- $ (K_{\lceil(n+1)/2 \rceil}, N')$๋ฅผ ์ parent์ Insert
- Note: leaf node๋ฅผ splitํ๋ ๊ฒ๊ณผ๋ ๋ฌ๋ฆฌ, search-key๋ 'copied'๋์ง ์๊ณ parent node๋ก 'moved'๋จ(i.e., no duplication!)
Updates on B -Trees: Deletion
- Record๊ฐ file์์ already deleted(์ด๋ฏธ ์ญ์ )๋์๋ค๊ณ ๊ฐ์ . ๋ record์ search key value์ด๊ณ , ์ record์ ๋ํ pointer
- $ (P_r, v)$๋ฅผ leaf node์์ Remove(์ ๊ฑฐ)
- If(๋ง์ฝ) leaf node๊ฐ ์ ๊ฑฐ๋ก ์ธํด too few entries()๋ฅผ ๊ฐ์ง๊ณ , node์ entries์ sibling(ํ์ )์ entries๊ฐ single node()์ fit(๋ง๋ ๊ฒฝ์ฐ), merge siblings(ํ์ ํฉ์น๊ธฐ)
- ๋ node์ ๋ชจ๋ entries๋ฅผ left node์ Insertํ๊ณ ๋ค๋ฅธ node๋ฅผ delete
- ์ญ์ ๋ node๋ฅผ ๊ฐ๋ฆฌํค๋ pointer๊ฐ ์ธ ๊ฒฝ์ฐ, ์ $ (K_{i-1}, P_i)$๋ฅผ ๊ทธ parent๋ก๋ถํฐ recursively(์ฌ๊ท์ ์ผ๋ก) delete
- Otherwise(๊ทธ๋ ์ง ์์ ๊ฒฝ์ฐ), node๊ฐ ์ ๊ฑฐ๋ก ์ธํด too few entries๋ฅผ ๊ฐ์ง๊ณ , node์ entries์ sibling์ entries๊ฐ single node์ fitํ์ง ์๋ ๊ฒฝ์ฐ, redistribute pointers(ํฌ์ธํฐ ์ฌ๋ถ๋ฐฐ)
- Node์ sibling ์ฌ์ด์ pointers๋ฅผ redistributeํ์ฌ ๋ ๋ค minimum number of entries()๋ณด๋ค more(๋ ๋ง์ด) ๊ฐ์ง๋๋ก ํจ
- Node์ parent์์ corresponding search-key value(ํด๋น search-key ๊ฐ)๋ฅผ Update
- Node deletions๋ ์ญ์ ํ ๊ฐ ์ด์์ pointers๋ฅผ ๊ฐ์ง node๋ฅผ ์ฐพ์ ๋๊น์ง cascade upwards(์๋ก ์ ํ)๋ ์ ์์(Note: internal node๋ at least pointers๋ฅผ ๊ฐ์ ธ์ผ ํจ)
- Root node๊ฐ ์ญ์ ํ only one pointer(ํ๋์ ํฌ์ธํฐ)๋ง ๊ฐ์ง๋ ๊ฒฝ์ฐ, deleted๋๊ณ sole child(์ ์ผํ ์์)๊ฐ root๊ฐ ๋จ
Complexity of B -Tree Updates
- Insertion ๋ฐ deletion of a single entry์ Cost(I/O ์์ ์ ์ธก๋ฉด์์)๋ ํธ๋ฆฌ์ height์ proportional(๋น๋ก)
- ๊ฐ์ entries์ ์ต๋ fanout(๋ถ๊ธฐ์) ์ด ์๋ ๊ฒฝ์ฐ, entry์ insert/delete์ worst-case complexity๋
- In practice(์ค์ ๋ก๋), I/O ์์
์๋ less(๋ ์ ์)
- Internal nodes๋ buffer(๋ฒํผ)์ ์๋ ๊ฒฝํฅ์ด ์์
- Splits/merges๋ rare(๋๋ฌผ๊ณ ), ๋๋ถ๋ถ์ insert/delete ์์ ์ only affect a leaf node(leaf node๋ง ์ํฅ)
- Average node occupancy(ํ๊ท ๋
ธ๋ ์ ์ ์จ)๋ insertion order์ depends on(๋ฌ๋ ค ์์)
- Random insertion(๋ฌด์์ ์ฝ์ ) ์
- Sorted order(์ ๋ ฌ๋ ์์)๋ก ์ฝ์ ์
B -Tree File Organization
- B -Tree 'File' Organization
- B -tree file organization์ Leaf nodes๋ pointers ๋์ records(๋ ์ฝ๋)๋ฅผ ์ ์ฅ
- Insertion/deletion/updates๊ฐ ์์ ๋๋ data records๋ฅผ clustered(ํด๋ฌ์คํฐ๋ง)๋ ์ํ๋ก ์ ์งํ๋ ๋ฐ ๋์ data file degradation problem(overflow blocks) ํด๊ฒฐ
- Leaf nodes๋ ์ฌ์ ํ half full(์ ๋ฐ) ์ํ๊ฐ ์๊ตฌ๋จ
- Records๊ฐ pointers๋ณด๋ค ํฌ๊ธฐ ๋๋ฌธ์, leaf node์ ์ ์ฅํ ์ ์๋ records์ maximum number๋ non-leaf node์ pointers ์๋ณด๋ค less(์ ์)
- Good space utilization(์ข์ ๊ณต๊ฐ ํ์ฉ)์ด ์ค์
- Splits ๋ฐ merges ๋์ redistribution(์ฌ๋ถ๋ฐฐ)์ more sibling nodes(๋ ๋ง์ ํ์ ๋ ธ๋)๋ฅผ ํฌํจ์ํด
- Redistribution์ 2 siblings๋ฅผ ํฌํจ์ํค๋ฉด(split/merge๋ฅผ ํผํ๊ธฐ ์ํด), ๊ฐ node๋ at least entries๋ฅผ ๊ฐ์ง๊ฒ ๋จ
- Insertion ๋ฐ deletion์ B -tree index์์ entries์ insertion ๋ฐ deletion๊ณผ ๋์ผํ ๋ฐฉ์์ผ๋ก ์ฒ๋ฆฌ
B-Tree Index Files
B -tree์ ์ ์ฌํ์ง๋ง, B-tree๋ search-key values๊ฐ only once(ํ ๋ฒ๋ง) ๋ํ๋๋๋ก ํ์ฉ. Redundant storage of search keys(search key์ ์ค๋ณต ์ ์ฅ)๋ฅผ ์ ๊ฑฐ
Non-leaf nodes์ search keys๋ B-tree์ ์ด๋์๋ ๋ํ๋์ง ์์. Non-leaf node์ ๊ฐ search key์ ๋ํด additional pointer field(์ถ๊ฐ ํฌ์ธํฐ ํ๋)๊ฐ ํฌํจ๋์ด์ผ ํจ
Generalized B-tree leaf node
Non-leaf node: pointers ๋ bucket ๋๋ file record pointers
Advantages of B-Tree indices
- Corresponding B -Tree๋ณด๋ค May use less tree nodes(๋ ์ ์ ์์ ํธ๋ฆฌ ๋ ธ๋๋ฅผ ์ฌ์ฉํ ์ ์์)
- ๋๋๋ก leaf node์ ๋๋ฌํ๊ธฐ ์ ์ search-key value๋ฅผ find(์ฐพ๋ ๊ฒ์ด ๊ฐ๋ฅ)
Disadvantages of B-Tree indices
- Only small fraction(์์ ๋ถ๋ถ)์ search-key values๋ง ์ผ์ฐ ๋ฐ๊ฒฌ๋จ
- Non-leaf nodes๋ larger(๋ ํฌ๋ฏ๋ก), fan-out์ด reduced(๊ฐ์) B-Trees๋ ์ผ๋ฐ์ ์ผ๋ก corresponding B -Tree๋ณด๋ค greater depth(๋ ํฐ ๊น์ด)๋ฅผ ๊ฐ์ง
- Insertion ๋ฐ deletion์ด B -Trees๋ณด๋ค more complicated(๋ ๋ณต์ก)
- Implementation(๊ตฌํ)์ด B -Trees๋ณด๋ค harder(๋ ์ด๋ ค์)
- Typically, B-Trees์ ์ฅ์ ์ ๋จ์ ๋ณด๋ค out weigh(ํฌ์ง ์์)
Other Issues in Indexing
- Record relocation(๋ ์ฝ๋ ์ฌ๋ฐฐ์น) and secondary indices
- Record๊ฐ moves(์ด๋)ํ๋ฉด, record pointers๋ฅผ ์ ์ฅํ๋ all secondary indices(๋ชจ๋ ๋ณด์กฐ ์ธ๋ฑ์ค)๊ฐ have to be updated(๊ฐฑ์ ๋์ด์ผ ํจ)
- B -tree file organizations์์ leaf node splits๊ฐ very expensive(๋งค์ฐ ๋น์ธ์ง)
- Solution: secondary index์์ record pointer ๋์ B -tree file organization์ search key๋ฅผ ์ฌ์ฉ
- Record๋ฅผ ์ฐพ๊ธฐ ์ํด file organization์ Extra traversal(์ถ๊ฐ ์ํ)
- Queries์ ๋ํ Higher cost(๋ ๋์ ๋น์ฉ), but node splits are cheap(๋ ธ๋ ๋ถํ ์ ์ ๋ ดํจ)
- Indexing strings(๋ฌธ์์ด ์ธ๋ฑ์ฑ)
- Variable length strings(๊ฐ๋ณ ๊ธธ์ด ๋ฌธ์์ด) as keys
- Variable fanout(๊ฐ๋ณ ๋ถ๊ธฐ์)
- Pointers์ ์๊ฐ ์๋ space utilization(๊ณต๊ฐ ํ์ฉ)์ splitting(๋ถํ )์ criterion(๊ธฐ์ค)์ผ๋ก ์ฌ์ฉ
- Prefix compression(์ ๋์ฌ ์์ถ)
- Internal nodes์ key values๋ prefixes(์ ๋์ฌ) of full key์ผ ์ ์์ fanout increases(์ฆ๊ฐ)
- Key value๋ก ๋ถ๋ฆฌ๋ subtrees์ entries๋ฅผ distinguish(๊ตฌ๋ณ)ํ๊ธฐ์ ์ถฉ๋ถํ ๋ฌธ์๋ฅผ ์ ์ง
- E.g., "Silas"์ "Silberschatz"๋ "Silb"๋ก ๋ถ๋ฆฌ๋ ์ ์์
- Variable length strings(๊ฐ๋ณ ๊ธธ์ด ๋ฌธ์์ด) as keys
Other Issues in Indexing: Bulk Loading & Bottom-Up Build
- Entries๋ฅผ one-at-a-time(ํ ๋ฒ์ ํ๋์ฉ) B -tree์ ์ฝ์
ํ๋ฉด entry๋น ๊ฐ ํ์ํ ์ ์์
- ์ต์ ์ ๊ฒฝ์ฐ, ํธ๋ฆฌ์ height์ ๋น๋ก
- A large number of entries๋ฅผ ํ ๋ฒ์ ์ฝ์ ํ๋ ๊ฒฝ์ฐ(bulk loading(๋๋ ๋ก๋ฉ)) very inefficient(๋งค์ฐ ๋นํจ์จ์ )
- B -tree index๊ฐ large relation(ํฐ ๊ด๊ณ)์ ๊ตฌ์ถ๋ ๋ bulk loading์ด ํ์
- Efficient alternative~1(ํจ์จ์ ์ธ ๋์~1)
- Efficient external sorting algorithms(ํจ์จ์ ์ธ ์ธ๋ถ ์ ๋ ฌ ์๊ณ ๋ฆฌ์ฆ)์ ์ฌ์ฉํ์ฌ index entries๋ฅผ Sort(์ ๋ ฌ)
- Insert in sorted order(์ ๋ ฌ๋ ์์๋ก ์ฝ์ )
- ํน์ leaf node๋ก ๊ฐ๋ ๋ชจ๋ entries๋ consecutively(์ฐ์์ ์ผ๋ก) ๋ํ๋จ leaf node๋ only once(ํ ๋ฒ๋ง) written out(๊ธฐ๋ก)ํ๋ฉด ๋จ
- Much improved IO performance(ํจ์ฌ ํฅ์๋ IO ์ฑ๋ฅ), but most leaf nodes half full
- Efficient alternative 2(ํจ์จ์ ์ธ ๋์ 2): Bottom-up B -tree construction(์ํฅ์ B -tree ๊ตฌ์ถ)
- ์ด์ ๊ณผ ๊ฐ์ด entries๋ฅผ sort
- ๊ทธ๋ฆฌ๊ณ leaf level๋ถํฐ ์์ํ์ฌ layer-by-layer(๊ณ์ธต๋ณ๋ก) tree๋ฅผ create(์์ฑ)
- ์ ๋ ฌ๋ entries๋ฅผ block์ fitํ ์ ์๋ ๋งํผ ๋ง์ entries๋ฅผ ์ ์งํ๋ฉด์ blocks๋ก Break up(๋๋) resulting blocks(๊ฒฐ๊ณผ ๋ธ๋ก)์ด leaf level์ ํ์ฑ
- ๊ฐ block์ minimum value์ block์ ๋ํ pointer๋ next level์ entries๋ฅผ ์์ฑํ๋ ๋ฐ ์ฌ์ฉ
- Most database systems(๋๋ถ๋ถ์ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์์คํ )์์ bulk-load utility(๋๋ ๋ก๋ ์ ํธ๋ฆฌํฐ)์ ์ผ๋ถ๋ก ๊ตฌํ
Hash Indices
- Bucket: index entries๋ฅผ ํฌํจํ๋ storage unit(์ ์ฅ ๋จ์)(์ผ๋ฐ์ ์ผ๋ก disk block)
- Entry์ search-key value์์ hash function์ ์ฌ์ฉํ์ฌ entry์ bucket์ Obtain(์ป์)
- Hash function : ๋ชจ๋ search-key values ์งํฉ ์์ ๋ชจ๋ bucket addresses ์งํฉ ๋ก์ ํจ์
- Hash function์ access, insertion, deletion์ ์ํ entries๋ฅผ locate(์ฐพ๋ ๋ฐ) ์ฌ์ฉ
- Different search-key values๋ฅผ ๊ฐ์ง entries๋ same bucket(๊ฐ์ ๋ฒํท)์ ๋งคํ๋ ์ ์์. ๋ฐ๋ผ์ entry๋ฅผ ์ฐพ๊ธฐ ์ํด entire bucket(์ ์ฒด ๋ฒํท)์ sequentially(์์ฐจ์ ์ผ๋ก) searched(๊ฒ์)ํด์ผ ํจ
Static Hashing
- In a hash index(ํด์ ์ธ๋ฑ์ค), buckets์ records์ ๋ํ pointers๋ฅผ ๊ฐ์ง entries๋ฅผ ์ ์ฅ(i.e., buckets store index entries)
- In a hash file-organization(ํด์ ํ์ผ ๊ตฌ์ฑ), buckets์ records๋ฅผ ์ ์ฅ
- Bucket overflow(๋ฒํท ์ค๋ฒํ๋ก)๋ ๋ค์์ผ๋ก ์ธํด ๋ฐ์ํ ์ ์์
- Insufficient buckets(๋ถ์ถฉ๋ถํ ๋ฒํท)
- Records์ distribution(๋ถํฌ)์์์ Skew(์๊ณก). ๋ ๊ฐ์ง ์ด์ ๋ก ๋ฐ์ ๊ฐ๋ฅ
- Multiple records(์ฌ๋ฌ ๋ ์ฝ๋)๊ฐ same search-key value(๋์ผํ search-key ๊ฐ)๋ฅผ ๊ฐ์ง
- Chosen hash function(์ ํ๋ ํด์ ํจ์)์ด key values์ non-uniform distribution(๋น๊ท ์ผ ๋ถํฌ)๋ฅผ ์์ฑ
- Bucket overflow์ probability(ํ๋ฅ )๋ ์ค์ผ ์ ์์ง๋ง, eliminated(์ ๊ฑฐ)ํ ์ ์์. overflow buckets(์ค๋ฒํ๋ก ๋ฒํท)์ ์ฌ์ฉํ์ฌ ์ฒ๋ฆฌ
Hash Functions
- Hash functions์ uniform(๊ท ์ผ) and random(๋ฌด์์)์ด ์๊ตฌ๋จ
- Uniform: ๊ฐ bucket์ all theoretically possible values(๋ชจ๋ ์ด๋ก ์ ์ผ๋ก ๊ฐ๋ฅํ ๊ฐ) ์งํฉ์์ same number์ search-key values๊ฐ ํ ๋น๋จ
- Random: ๊ฐ bucket์ ํ์ผ ๋ด search-key values์ actual distribution(์ค์ ๋ถํฌ)์ irrespective(๊ด๊ณ์์ด), same number์ entries๊ฐ ํ ๋น๋จ
- Typical hash functions(์ผ๋ฐ์ ์ธ ํด์ ํจ์)๋ search-key์ internal binary representation(๋ด๋ถ ์ด์ง ํํ)์ ๋ํ computation(๊ณ์ฐ)์ ์ํ
- E.g., string search-key์ ๊ฒฝ์ฐ, string์ ๋ชจ๋ characters(๋ฌธ์)์ binary representations๋ฅผ addedํ๊ณ , sum modulo the number of buckets(ํฉ๊ณ๋ฅผ ๋ฒํท ์๋ก ๋๋ ๋๋จธ์ง)๋ฅผ returned(๋ฐํ)
Handling of Bucket Overflows
- Overflow chaining(์ค๋ฒํ๋ก ์ฒด์ธ): ์ฃผ์ด์ง bucket์ overflow buckets์ linked list(์ฐ๊ฒฐ ๋ฆฌ์คํธ)๋ก chained together(ํจ๊ป ์ฐ๊ฒฐ)
- Overflow chaining์ ์ฌ์ฉํ๋ Hash indexing: closed addressing(ํ์ ์ฃผ์ ์ง์ )(or closed hashing(ํ์ ํด์ฑ))
- Overflow buckets์ ์ฌ์ฉํ์ง ์๋ alternative(๋์)์ธ open addressing(๊ฐ๋ฐฉ ์ฃผ์ ์ง์ )(or open hashing(๊ฐ๋ฐฉ ํด์ฑ))์ database applications์ not suitable(์ ํฉํ์ง ์์)
Deficiencies of Static Hashing
- Static hashing์์, ํจ์ ๋ search-key values๋ฅผ fixed set()์ bucket addresses๋ก ๋งคํ. Databases๋ ์๊ฐ์ด ์ง๋จ์ ๋ฐ๋ผ grow or shrink(์ปค์ง๊ฑฐ๋ ์ค์ด๋ฆ)
- Initial number of buckets๊ฐ too small์ด๊ณ , ํ์ผ์ด grows(์ปค์ง๋ฉด), too much overflows(๋๋ฌด ๋ง์ ์ค๋ฒํ๋ก)๋ก ์ธํด ์ฑ๋ฅ์ด degrade(์ ํ)๋จ
- Anticipated growth(์์๋๋ ์ฑ์ฅ)์ ์ํด space๊ฐ ํ ๋น๋๋ฉด, initially(์ด๊ธฐ์) ์๋นํ ์์ space๊ฐ wasted(๋ญ๋น)๋จ(and buckets will be underfull(๋ ์ฑ์์ง))
- Database๊ฐ shrinks(์ค์ด๋ค๋ฉด), again space๊ฐ wasted๋จ
- One solution(ํ๋์ ํด๊ฒฐ์ฑ
)
- New hash function์ผ๋ก ํ์ผ์ Periodic re-organization(์ฃผ๊ธฐ์ ์ธ ์ฌ๊ตฌ์ฑ)
- Expensive(๋น์ฉ์ด ๋ง์ด ๋ค๊ณ ), disrupts normal operations(์ ์์ ์ธ ์์ ์ ๋ฐฉํด)
- Better solution(๋ ๋์ ํด๊ฒฐ์ฑ
): Dynamic Hashing(๋์ ํด์ฑ)
- Buckets์ number๋ฅผ dynamically(๋์ ์ผ๋ก) modify(์์ )ํ ์ ์๋ Techniques(๊ธฐ์ )
- Size๊ฐ grows and shrinksํ๋ database์ Good
- Hash function์ dynamically modifyํ ์ ์๋๋ก Allows(ํ์ฉ)
- Extendable hashing(ํ์ฅ ๊ฐ๋ฅ ํด์ฑ): ๋์ ํด์ฑ์ ํ ํํ(๊ต์ฌ Section 24.5.2 ์ฐธ์กฐ)
Comparison of Ordered Indexing and Hashing
- Periodic re-organization์ Cost
- Insertions and deletions์ Relative frequency(์๋ ๋น๋)
- Is it desirable to optimize average access time at the expense of worst-case access time?(์ต์ ์ ๊ฒฝ์ฐ ์ ๊ทผ ์๊ฐ์ ํฌ์ํ์ฌ ํ๊ท ์ ๊ทผ ์๊ฐ์ ์ต์ ํํ๋ ๊ฒ์ด ๋ฐ๋์งํ๊ฐ?)
- Expected type of queries(์์๋๋ ์ฟผ๋ฆฌ ์ ํ)
- Hashing์ ์ผ๋ฐ์ ์ผ๋ก key์ specified value๋ฅผ ๊ฐ์ง records๋ฅผ retrieving(๊ฒ์)ํ๋ ๋ฐ better(๋ ์ข์)
- Range queries๊ฐ common(ํํ ๊ฒฝ์ฐ), ordered indices๊ฐ preferred(์ ํธ๋จ)
- In practice(์ค์ ๋ก๋)
- PostgreSQL์ hash indices๋ฅผ ์ง์ํ์ง๋ง, poor performance(์ ์กฐํ ์ฑ๋ฅ)๋ก ์ธํด discourages use(์ฌ์ฉ์ ๊ถ์ฅํ์ง ์์)
- Oracle์ static hash organization์ ์ง์ํ์ง๋ง, not hash indices(ํด์ ์ธ๋ฑ์ค๋ ์๋)
- SQLServer๋ only B -trees๋ฅผ ์ง์
