{"componentChunkName":"component---src-templates-post-template-js","path":"/211207-database-indexing/","result":{"data":{"allMarkdownRemark":{"edges":[{"node":{"id":"dfe5f359-6c4f-5bec-a7a7-cfa06d940fab","html":"<blockquote>\n<p>Indexes(색인)는 특정 검색 조건에 대한 응답으로 레코드 검색 속도를 높이는 데 사용된다.</p>\n</blockquote>\n<blockquote>\n<p>Index structures는 secondary access paths를 제공하는 “디스크의 추가 파일”이다. 이는 디스크의 기본 데이터 파일에 있는 레코드의 물리적 배치에 영향을 주지 않고 레코드에 access할 수 있는 대체 방법을 제공하며, indexes를 구성하는 데 사용되는 indexing 필드를 기반으로 레코드에 <u>효율적으로 access</u>할 수 있다.</p>\n</blockquote>\n<p><strong>인덱스를 만드는 데 모든 필드를 사용할 수 있다.</strong></p>\n<ul>\n<li>\n<p>서로 다른 필드에 여러 인덱스를 구성할 수 있다.</p>\n</li>\n<li>\n<p>여러 필드에 대한 (multi-level) 인덱스를 구성할 수 있다.</p>\n<ul>\n<li><strong>동일한</strong> 파일에 대한 두 가지 유형의 인덱스</li>\n</ul>\n</li>\n</ul>\n<p><strong>인덱싱 필드의 검색 조건을 기반으로 데이터 파일에서 레코드를 찾으려면?</strong></p>\n<ul>\n<li>인덱스 파일이 검색되어 필요한 레코드가 있는 데이터 파일의 하나 이상의 <strong>디스크 블록에 대한 포인터를 안내한다.</strong></li>\n</ul>\n<p><strong>index를 구성하려면?</strong></p>\n<ul>\n<li>대부분의 인덱스는 정렬된 파일(single-level indexes)을 기반으로 하며 tree 데이터 구조(multi-level indexes, ex. B+ tree)를 사용한다.</li>\n</ul>\n<br />\n<h1>Types of Single-Level Ordered Indexes</h1>\n<hr />\n<h2>Ordered Index (정렬 색인)</h2>\n<p><strong>Indexing field (attribute)</strong></p>\n<ul>\n<li>\n<p>인덱스 구조가 일반적으로 정의되는 <u>single field of a file</u></p>\n</li>\n<li>\n<p><strong>인덱스 필드의 각 값은 해당 필드 값을 가진 레코드를 포함하는 모든 디스크 블록에 대한 포인터 목록과 함께 인덱스 파일에 저장된다.</strong></p>\n</li>\n</ul>\n<br />\n<p><strong>인덱스의 값은 “ordered” 또는 “sorted”이다.</strong></p>\n<ul>\n<li>Why? <strong>인덱스에서 binary search(linear search가 아닌)을 수행한다.</strong></li>\n</ul>\n<p><strong>인덱스 파일은 데이터 파일보다 훨씬 작다.</strong></p>\n<ul>\n<li>Why? <strong>key와 pointer만 보유하기 때문이다.</strong></li>\n</ul>\n<br />\n<h2>Several Types of Ordered Index</h2>\n<h3>Primary index</h3>\n<ul>\n<li>정렬된 레코드 파일의 <strong>ordering key</strong> 필드에 지정된다.</li>\n</ul>\n<h3>Clustering index</h3>\n<ul>\n<li>\n<p><strong>non-key ordering</strong> 필드에 지정된다. (여러 레코드의 필드에 대해 <strong>동일한 값을 가질 수 있다.</strong>)</p>\n<ul>\n<li>\n<p>즉, 중복 가능한 필드로 만든 index이다.</p>\n</li>\n<li>\n<p>(물리적) 데이터 파일을 <strong>clustered 파일</strong>이라고 한다.</p>\n</li>\n</ul>\n</li>\n<li>\n<p><strong>파일은 최대 하나의 물리적 순서 필드를 가질 수 있으므로</strong> <strong>최대 하나의 primary 인덱스 또는 하나의 clustering 인덱스를 가질 수 있지만 둘 다 가질 수는 없다.</strong></p>\n</li>\n</ul>\n<h3>Secondary index</h3>\n<ul>\n<li>\n<p><strong>non-ordering</strong> 필드에 지정된다.</p>\n<ul>\n<li>정렬 상관없이 지정한 필드에 대한 모든 index이다.</li>\n</ul>\n</li>\n<li>\n<p>데이터 파일에는 여러 개의 secondary 인덱스가 있을 수 있다.</p>\n</li>\n</ul>\n<br />\n<h2>Primary Indexes (주요 색인)</h2>\n<br />\n<div style=\"text-align:center;\">\n  <img src=\"https://user-images.githubusercontent.com/33220404/144964117-c26b81ce-c96d-436a-a567-8abcdb41cb00.png\" width=\"500px\">\n</div>\n<br />\n<ul>\n<li>\n<p>Index file은 <strong>각 block의 첫번째 record에 대한</strong> <strong>primary key value</strong>와 <strong>block pointer</strong>를 가진다.</p>\n<ul>\n<li><strong>이미 primary key field가 정렬되어 있기 때문에, 첫번째 value만 알면 나머지는 쉽게 찾을 수 있다.</strong></li>\n</ul>\n</li>\n<li>\n<p>Index file의 key를 이용하여 전체를 <strong>binary search</strong>한 후, 연결된 block 안에서도 <strong>binary search</strong>를 하여 search key를 발견한다.</p>\n</li>\n</ul>\n<blockquote>\n<blockquote>\n<p>Index는 <strong>dense</strong>하거나 <strong>sparse</strong>하다. <strong>Dense index</strong>에는 데이터 파일의 <strong>모든 검색 키 값(모든 레코드)</strong> 에 대한 인덱스 항목이 있다. <strong>Sparse index</strong>에는 <strong>일부</strong> 검색 값에 대한 항목만 있다.</p>\n</blockquote>\n</blockquote>\n<p>➡️ <strong>Primary index는 sparse하다.</strong> (field의 first record에 대한 index 항목만 가지기 때문)</p>\n<br />\n<p><strong>Primary 인덱스의 인덱스 파일은 데이터 파일보다 훨씬 작은 공간을 차지한다.</strong></p>\n<ul>\n<li>\n<ol>\n<li>데이터 파일에 있는 레코드보다 인덱스 항목이 더 적다.</li>\n</ol>\n</li>\n<li>\n<ol start=\"2\">\n<li>각 인덱스 항목은 두 개의 필드 또는 (key, block address)만 있기 때문에 일반적으로 데이터 레코드보다 크기가 작다.</li>\n</ol>\n</li>\n</ul>\n<p>➡️ <strong>따라서 인덱스 파일에 대한 binary search은 데이터 파일에 대한 binary search보다 더 적은 수의 블록 액세스가 필요하다.</strong></p>\n<br />\n<h3>Priblems</h3>\n<p>주요 문제: <strong>records의 insertion 및 deletion</strong></p>\n<ul>\n<li>\n<p>데이터 파일의 “올바른” 위치에 레코드를 삽입하려고 하면,</p>\n<ul>\n<li>\n<p>새 레코드를 위한 공간을 만들기 위해 <strong>레코드를 이동</strong>할 뿐만 아니라</p>\n</li>\n<li>\n<p>또한 일부 <strong>index entries을 변경</strong>해야 한다.</p>\n</li>\n<li>\n<p>레코드를 이동하면 일부 블록의 블록 anchors가 변경된다.</p>\n</li>\n</ul>\n</li>\n</ul>\n<p><strong>Insertion 문제를 해결하는 방법</strong></p>\n<ul>\n<li>\n<p><strong>unordered overflow file</strong>을 사용한다.</p>\n</li>\n<li>\n<p>데이터 파일의 각 블록에 대해 <strong>overflow 레코드의 linked list</strong>을 사용한다.</p>\n<ul>\n<li>\n<p>hashing 기술에서 overflow 레코드를 처리하는 것과 유사하다.</p>\n</li>\n<li>\n<p>검색 시간을 단축하려면 각 블록과 해당 overflow linked list 내의 레코드를 정렬한다.</p>\n</li>\n</ul>\n</li>\n</ul>\n<p><strong>Deletion 문제를 해결하는 방법</strong></p>\n<ul>\n<li><strong>deletion markers</strong>를 사용한다.</li>\n</ul>\n<br />\n<h2>Clustering Indexes (군집 색인)</h2>\n<br />\n<div style=\"text-align:center;\">\n  <img src=\"https://user-images.githubusercontent.com/33220404/144967953-d1d3ea5d-0379-4235-b8d8-1898804b559d.png\" width=\"500px\">\n</div>\n<br />\n<ul>\n<li>\n<p>non-key field에 대해 정렬한 후, <strong>distinct한 값 즉, 각기 다른 값들 가지로 index file을 만든다.</strong></p>\n<ul>\n<li>Index 파일의 record는 <strong>데이터 파일의 discticnt한 vlaue를 가지고 있는 최초의 record가 속하는 block에 대한 Clustring field value와 Block pointer만을 가진다.</strong></li>\n</ul>\n</li>\n<li>\n<p>만든 index record를 정렬시켜 binary search를 적용한다.</p>\n</li>\n</ul>\n<p>➡️ <strong>따라서 Clustering Index의 record들은 unique하다.</strong></p>\n<br />\n<h3>Problems</h3>\n<p>주요 문제: <strong>records의 insertion 및 deletion</strong></p>\n<ul>\n<li><strong>Physically ordering</strong> 때문에</li>\n</ul>\n<p><strong>Insertion/Deletion 문제를 해결하는 방법</strong></p>\n<br />\n<div style=\"text-align:center;\">\n  <img src=\"https://user-images.githubusercontent.com/33220404/144968469-2b975e3a-defc-4e74-8cd2-7c4fdbbbcae5.png\" width=\"500px\">\n</div>\n<ul>\n<li>\n<p>overflow block list로 record를 추가하고, pointer를 업데이트한다.</p>\n</li>\n<li>\n<p>이렇게 하면 고유한 값을 가진 모든 레코드가 (동일한) 블록(또는 블록 클러스터)에 배치된다.</p>\n<ul>\n<li>Insertion/Deletion가 비교적 간단해진다.</li>\n</ul>\n</li>\n</ul>\n<br />\n<h2>Secondary Indexes (보조 색인)</h2>\n<p><strong>데이터 파일에 액세스하는 보조 (추가적인) 수단을 제공한다.</strong></p>\n<ul>\n<li>\n<p>일부 기본 액세스가 존재한다.</p>\n</li>\n<li>\n<p>데이터 레코드를 정렬하는 것은 중요하지 않다.</p>\n</li>\n</ul>\n<p><strong>모든 레코드에 고유한 값이 있는 키 필드</strong> 및 <strong>중복 값이 있는 키가 아닌 필드</strong>에 생성할 수 있다. 또한, 같은 데이터 파일에 대해 여러 Secondery 인덱스를 생성할 수 있다.</p>\n<br />\n<h3>Secondary Index on a Key Field</h3>\n<br />\n<div style=\"text-align:center;\">\n  <img src=\"https://user-images.githubusercontent.com/33220404/144969169-03c1a534-d479-4c81-aade-90ee987be74f.png\" width=\"500px\">\n</div>\n<br />\n<p><strong>Key Field: 모든 레코드에 대해 고유한 값을 가진다.</strong></p>\n<ul>\n<li>\n<p><strong>secondary key</strong>라고 한다. 이 경우 <strong>데이터 파일의 각 레코드에 대해 하나의 인덱스 항목이 있다.</strong></p>\n</li>\n<li>\n<p>Index 파일의 record는 각각 <strong>record field의 값과 record를 포함하는 block 또는 record 자체에 대한 pointer를 가진다.</strong></p>\n</li>\n<li>\n<p><strong>dense</strong>하다.</p>\n</li>\n</ul>\n<br />\n<h3>Problems</h3>\n<p><strong>Secondary 인덱스는 일반적으로 Primary 인덱스보다 더 많은 저장 공간과 더 긴 검색 시간이 필요하다.</strong></p>\n<ul>\n<li>대신 독단적인 레코드에 대한 검색 시간이 개선된다. (아무 field에 대해 만들 수 있기 때문에 query가 general하다.)</li>\n</ul>\n<br />\n<h2>Summary of Index Types</h2>\n<br />\n<div style=\"text-align:center;\">\n  <img src=\"https://user-images.githubusercontent.com/33220404/144970082-5a26ac33-8f25-43c7-9690-1dbb1fccb64e.png\" width=\"800px\">\n</div>\n<br />\n<h1>Multilevel Indexes</h1>\n<hr />\n<blockquote>\n<p>검색 시 남은 검색 공간을 크게 줄이도록 설계한다. 인덱스 블록에서 blocking factor (fanout)을 증가시킨다.</p>\n</blockquote>\n<div style=\"text-align:center;\">\n  <img src=\"https://user-images.githubusercontent.com/33220404/144971209-60a8d09e-4b60-4c4d-a402-8cf8d59c1976.png\" width=\"500px\">\n</div>\n<br />\n<ul>\n<li>\n<p><strong>A two-level primary index</strong></p>\n<ul>\n<li>static structure의 <strong>ISAM(Indexed Sequential Access Method)</strong></li>\n</ul>\n</li>\n</ul>\n<br />\n<h1>Dynamic Multilevel Indexes</h1>\n<hr />\n<h2>Why Dynamic?</h2>\n<p><strong>ISAM: 여전히 인덱스 삽입 및 삭제를 처리하는 문제에 직면</strong></p>\n<ul>\n<li>\n<ol>\n<li>모든 인덱스 수준은 물리적으로 정렬된 파일이며 <strong>삽입/삭제 시 구조가 이후에 변경되지 않는다.</strong></li>\n</ol>\n</li>\n<li>\n<ol start=\"2\">\n<li><strong>오버플로 페이지로 인한 성능이 저하된다.</strong></li>\n</ol>\n</li>\n</ul>\n<p><strong>이러한 inflexibility(경직성)을 해결하기 위해,</strong></p>\n<ul>\n<li>\n<p><strong>새 항목을 삽입하기 위해 각 인덱스 블록에 약간의 공간을 남겨 둔다.</strong></p>\n</li>\n<li>\n<p>데이터 파일이 늘어나고 줄어들 때 새 인덱스 블록을 생성/삭제하기 위해 <strong>적절한 삽입/삭제 알고리즘을 사용한다.</strong></p>\n</li>\n</ul>\n<p><strong>➡️ 따라서, Tree 데이터 구조를 기반으로 Dynamic Multilevel Index를 이용한다.</strong></p>\n<br />\n<h2>Review of Tree Data Structure</h2>\n<br />\n<div style=\"text-align:center;\">\n  <img src=\"https://user-images.githubusercontent.com/33220404/144971775-3739f86f-d013-4f50-943e-13dd7cf0d7a1.png\" width=\"600px\">\n</div>\n<br />\n<ul>\n<li>\n<p><strong>unbalanced</strong> 트리를 보여주는 트리 데이터 구조</p>\n<ul>\n<li>\n<p>어느 부분은 빠르고, 어느 부분은 느린 현상이 발생한다. (<strong>Search Performance guarantee 되지 않는다.</strong>)</p>\n</li>\n<li>\n<p><strong>모든 경로의 코스를 똑같이 만들어 어느 경로를 접근하더라도 Leaf Node만 도달할 수 있으면 성능을 높일 수 있다.</strong></p>\n</li>\n</ul>\n</li>\n</ul>\n<br />\n<h2>B+ Tree Structures</h2>\n<ul>\n<li>\n<p><strong>삽입/삭제는 log_p N 비용으로 수행 가능하다.</strong></p>\n<ul>\n<li>(<strong>p</strong> = fanout (한 노드 안의 몇개의 index entriesfmf 넣을 수 있는가), <strong>N</strong> = # leaf pages)</li>\n</ul>\n</li>\n<li>\n<p><strong>트리 height-balanced를 유지한다.</strong></p>\n</li>\n<li>\n<p><strong>fanout을 최소 50% 점유해야 한다</strong>(루트 제외). - Rull!</p>\n</li>\n<li>\n<p>각 노드는 d &#x3C;= m &#x3C;= 2d 항목을 포함한다.</p>\n<ul>\n<li>매개변수 d를 트리의 차수라고 한다.</li>\n</ul>\n</li>\n<li>\n<p><strong>equality</strong> 및 <strong>range-seraches</strong>을 효율적으로 지원한다.</p>\n</li>\n</ul>\n<blockquote>\n<blockquote>\n<p>Search를 Guide하는 node를 <strong>Internal nodes</strong>라 하며, pointer를 통해 실제 record에 접근 가능한 node를 <strong>Leaf nodes</strong>라 한다.</p>\n</blockquote>\n</blockquote>\n<ul>\n<li>Leaf nodes는 각기 연결되어 있는데, 이로 인해 Internal node를 다시 search할 필요가 없다.</li>\n</ul>\n<br />\n<div style=\"text-align:center;\">\n  <img src=\"https://user-images.githubusercontent.com/33220404/144972778-f1adc83e-8904-4595-94a5-b3b8de917ca0.png\" width=\"600px\">\n</div>\n<br />\n<h2>Inserting an Index Entry into a B+ Tree</h2>\n<br />\n<div style=\"text-align:center;\">\n  <img src=\"https://user-images.githubusercontent.com/33220404/144972917-ebfd5b53-77a5-4506-a34c-cfce9f9a81f8.png\" width=\"600px\">\n</div>\n<div style=\"text-align:center;\">\n  <img src=\"https://user-images.githubusercontent.com/33220404/144973031-69a7fb15-383e-4652-bd6f-12ad89f30def.png\" width=\"600px\">\n</div>\n<p>(위 이미지는 8* 을 inserting하는 과정이다.)</p>\n<ul>\n<li>\n<p>먼저 올바른 Leaf L을 찾은 후, 인덱스 항목을 L에 넣는다.</p>\n</li>\n<li>\n<p>이때, 공간이 충분하면 완료되지만, 그렇지 않으면 L을 <strong>Spilt</strong>해야 한다. (L과 새 노드 L2로)</p>\n<ul>\n<li>\n<p>entries를 균등하게 재분배하고 middle key를 <strong>Copy Up</strong> 한다.</p>\n</li>\n<li>\n<p>그리고 L2를 가리키는 인덱스 항목을 L의 부모에 삽입한다.</p>\n</li>\n<li>\n<p>이것은 <strong>재귀적</strong>으로 발생할 수 있다.</p>\n</li>\n</ul>\n</li>\n<li>\n<p>split은 트리를 “grow”하게 한다. 더 넓어지거나 높아진다.</p>\n<ul>\n<li>\n<p>이때, <strong>re-distribiting을 통해 분할을 피할 수 있다.</strong></p>\n<ul>\n<li>\n<p>hight를 높이지 않고 기존에 옆에 있는 sibiling을 찾아 비어있는 공간을 채운다.</p>\n</li>\n<li>\n<p>이로 인해, 평균 점유을이 향상된다.</p>\n</li>\n<li>\n<p>하지만, 이것은 일반적으로 실제 수행되지 않는다.</p>\n<ul>\n<li><strong>빈 공간을 채우는 것은 새로운 disk block에 대한 access가 필요하므로, 차라리 height를 높이는 것이 I/O가 증가하는 것보다 효율적이다.</strong></li>\n</ul>\n</li>\n</ul>\n</li>\n</ul>\n</li>\n</ul>\n<div style=\"text-align:center;\">\n  <img src=\"https://user-images.githubusercontent.com/33220404/144974167-c28a21bd-8974-47d3-a4e1-f769414783cf.png\" width=\"600px\">\n</div>\n<br />\n<h2>Deleting an Index Entry from a B+ Tree</h2>\n<br />\n<div style=\"text-align:center;\">\n  <img src=\"https://user-images.githubusercontent.com/33220404/144974307-199f66ee-d592-4bf2-8a4f-29293ddfcee8.png\" width=\"600px\">\n</div>\n<p>(위 이미지는 19*와 20*을 삭제한 전/후 결과이다.)</p>\n<ul>\n<li>\n<p>먼저 루트에서 시작하여 entry가 속한 leaf L을 찾아 제거한다.</p>\n</li>\n<li>\n<p>이때, 제거한 L이 최소 절반이 차있으면 완료되지만, 그렇지 않은 경우 (L에 d-1 항목만 찬 경우) <strong>sibling에서 빌려 re-distribute(재배포)한다.</strong></p>\n</li>\n</ul>\n<br />\n<div style=\"text-align:center;\">\n  <img src=\"https://user-images.githubusercontent.com/33220404/144975412-f6856aab-4763-4d5b-b4f2-82a63799912d.png\" width=\"600px\">\n</div>\n<p>(위 이미지는 19*와 20*, 24*을 삭제한 전/후 결과이다.)</p>\n<ul>\n<li>\n<p><strong>재배포에 실패하면 L과 sibling을 merge한다.</strong></p>\n<ul>\n<li>\n<p><strong>merge가 발생하면 L의 상위 항목을 삭제해야 한다.</strong></p>\n</li>\n<li>\n<p>merge가 루트로 전파되어 <strong>높이가 감소</strong>될 수 있다.</p>\n<ul>\n<li>\n<p>루트 노드가 가리키는게 잘못되어버림으로, <strong>Pull down</strong>하게 된다.</p>\n</li>\n<li>\n<p>search cost가 감소된다.</p>\n</li>\n</ul>\n</li>\n<li>\n<p>이는 상당한 I/O가 발생한다.</p>\n</li>\n</ul>\n</li>\n</ul>","frontmatter":{"title":"[Database] Indexing Structures for Files","date":"2021.12.07.","category":"Data Science","categoryColor":"seagreen","tags":["DB"]}}}]}},"pageContext":{"slug":"/211207-database-indexing/","next":{"frontmatter":{"title":"[Database] Physical Database Design"},"fields":{"slug":"/211206-database-physical-design/"}},"previous":{"frontmatter":{"title":"[Database] Transaction"},"fields":{"slug":"/211208-database-transaction/"}}}},"staticQueryHashes":[]}