B+-Trees and Hashing Techniques for Storage and Index .ppt
《B+-Trees and Hashing Techniques for Storage and Index .ppt》由会员分享,可在线阅读,更多相关《B+-Trees and Hashing Techniques for Storage and Index .ppt(35页珍藏版)》请在麦多课文档分享上搜索。
1、,B+-Trees and Hashing Techniques for Storage and Index Structures,Covers Chapters 8, 10, 11 Third EditionLast updated: January 30, 2003,Last changed: February 6, 03, 3p,Alternative File Organizations,Many alternatives exist, each ideal for some situation , and not so good in others: Heap files: Suit
2、able when typical access is a file scan retrieving all records. Sorted Files: Best if records must be retrieved in some order, or only a range of records is needed. Hashed Files: Good for equality selections. File is a collection of buckets. Bucket = primary page plus zero or more overflow pages. Ha
3、shing function h: h(r) = bucket in which record r belongs. h looks at only some of the fields of r, called the search fields.,Index Classification,Primary vs. secondary: If search key contains primary key, then called primary index. Unique index: Search key contains a candidate key. Clustered vs. un
4、clustered: If order of data records is the same as, or close to, order of data entries, then called clustered index. Alternative 1 implies clustered, but not vice-versa. A file can be clustered on at most one search key. Cost of retrieving data records through index varies greatly based on whether i
5、ndex is clustered or not!,Clustered vs. Unclustered Index,Suppose that Alternative (2) is used for data entries, and that the data records are stored in a Heap file.To build clustered index, first sort the Heap file (with some free space on each page for future inserts). Overflow pages may be needed
6、 for inserts. (Thus, order of data recs is close to, but not identical to, the sort order.),Index entries,Data entries,direct search for,(Index File),(Data file),Data Records,data entries,Data entries,Data Records,CLUSTERED,UNCLUSTERED,Index Classification (Contd.),Dense vs. Sparse: If there is at l
7、east one data entry per search key value (in some data record), then dense. Alternative 1 always leads to dense index. Every sparse index is clustered! Sparse indexes are smaller; however, some useful optimizations are based on dense indexes.,Ashby, 25, 3000,Smith, 44, 3000,Ashby,Cass,Smith,22,25,30
8、,40,44,44,50,Sparse Index,on,Name,Data File,Dense Index,on,Age,33,Bristow, 30, 2007,Basu, 33, 4003,Cass, 50, 5004,Tracy, 44, 5004,Daniels, 22, 6003,Jones, 40, 6003,Index Classification (Contd.),Composite Search Keys: Search on a combination of fields. Equality query: Every field value is equal to a
9、constant value. E.g. wrt index: age=20 and sal =75 Range query: Some field value is not a constant. E.g.: age =20; or age=20 and sal 10 Data entries in index sorted by search key to support range queries. Lexicographic order, or Spatial order.,sue,13,75,bob,cal,joe,12,10,20,80,11,12,name,age,sal,12,
10、20,12,10,11,80,13,75,20,12,10,12,75,13,80,11,11,12,12,13,10,20,75,80,Data records sorted by name,Data entries in index sorted by ,Data entries sorted by ,Examples of composite key indexes using lexicographic order.,Physical Database Design for Relational Databases,Select Storage Structures (determin
11、e how the particular relation is physically stored) Select Index Structures (to speed up certain queries) Select to minimize the runtime for a certain workload (e.g a given set of queries),Introduction Indexing Techniques,As for any index, 3 alternatives for data entries k*:Data record with key valu
12、e kHash-based indexes are best for equality selections. Cannot support range searches. B+-trees are best for sorted access and range queries.,Static Hashing,# primary pages fixed, allocated sequentially, never de-allocated; overflow pages if needed. h(k) mod M = bucket to which data entry with key k
13、 belongs. (M = # of buckets),h(key) mod N,h,key,Primary bucket pages,Overflow pages,2,0,N-1,Static Hashing (Contd.),Buckets contain data entries. Hash fn works on search key field of record r. Must distribute values over range 0 . M-1. h(key) = (a * key + b) usually works well. a and b are constants
14、; lots known about how to tune h. Long overflow chains can develop and degrade performance. Two approaches: Global overflow area Individual overflow areas for each bucket (assumed in the following) Extendible and Linear Hashing: Dynamic techniques to fix this problem.,Range Searches,Find all student
15、s with gpa 3.0 If data is in sorted file, do binary search to find first such student, then scan to find others. Cost of binary search can be quite high. Simple idea: Create an index file.,Can do binary search on (smaller) index file!,Page 1,Page 2,Page N,Page 3,Data File,k2,kN,k1,Index File,B+ Tree
16、: The Most Widely Used Index,Insert/delete at log F N cost; keep tree height-balanced. (F = fanout, N = # leaf pages) Minimum 50% occupancy (except for root). Supports equality and range-searches efficiently.,Example B+ Tree (order p=5, m=4),Search begins at root, and key comparisons direct it to a
17、leaf (as in ISAM). Search for 5*, 15*, all data entries = 24* .,Based on the search for 15*, we know it is not in the tree!,Root,16,22,29,2*,3*,5*,7*,14*,16*,19*,20*,22*,24*,27*,29*,33*,34*,38*,39*,7,p=5 because tree can have at most 5 pointers in intermediate node; m=4 because at most 4 entries in
18、leaf node.,B+ Trees in Practice,Typical order: 200. Typical fill-factor: 67%. average fanout = 133 Typical capacities: Height 4: 1334 = 312,900,700 records Height 3: 1333 = 2,352,637 records Can often hold top levels in buffer pool: Level 1 = 1 page = 8 Kbytes Level 2 = 133 pages = 1 Mbyte Level 3 =
19、 17,689 pages = 133 MBytes,Inserting a Data Entry into a B+ Tree,Find correct leaf L. Put data entry onto L. If L has enough space, done! Else, must split L (into L and a new node L2) Redistribute entries evenly, copy up middle key. Insert index entry pointing to L2 into parent of L. This can happen
20、 recursively To split index node, redistribute entries evenly, but push up middle key. (Contrast with leaf splits.) Splits “grow” tree; root split increases height. Tree growth: gets wider or one level taller at top.,Inserting 4* into Example B+ Tree,Observe how minimum occupancy is guaranteed in bo
21、th leaf and intermediate node splits. Note difference between copy-up and push-up; be sure you understand the reasons for this.,2*,3*,5*,7*,4,Entry to be inserted in parent node.,(Note that 4 is,continues to appear in the leaf.),s copied up and,appears once in the index. Contrast,4*,Example B+ Tree
22、After Inserting 4*,Notice that root was split, leading to increase in height.,In this example, we can avoid split by re-distributing entries; however, this is usually not done in practice.,2*,3*,Root,16,22,29,14*,16*,19*,20*,22*,24*,27*,29*,33*,34*,38*,39*,7,4,5*,4*,7*,Deleting a Data Entry from a B
23、+ Tree,Start at root, find leaf L where entry belongs. Remove the entry. If L is at least half-full, done! If L has only d-1 entries, Try to re-distribute, borrowing from sibling (adjacent node with same parent as L). If re-distribution fails, merge L and sibling. If merge occurred, must delete entr
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- BTREESANDHASHINGTECHNIQUESFORSTORAGEANDINDEXPPT

链接地址:http://www.mydoc123.com/p-378784.html