數(shù)據(jù)庫系統(tǒng)英文課件:ch12 Indexing and Hashing_第1頁
數(shù)據(jù)庫系統(tǒng)英文課件:ch12 Indexing and Hashing_第2頁
數(shù)據(jù)庫系統(tǒng)英文課件:ch12 Indexing and Hashing_第3頁
數(shù)據(jù)庫系統(tǒng)英文課件:ch12 Indexing and Hashing_第4頁
數(shù)據(jù)庫系統(tǒng)英文課件:ch12 Indexing and Hashing_第5頁
已閱讀5頁,還剩81頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、Chapter 12: Indexing and HashingChapter 12: Indexing and HashingBasic ConceptsOrdered Indices B+-Tree Index FilesB-Tree Index FilesStatic HashingDynamic Hashing Comparison of Ordered Indexing and Hashing Index Definition in SQLMultiple-Key AccessBasic ConceptsIndexing mechanisms used to speed up acc

2、ess to desired data.E.g., author catalog in librarySearch Key - attribute to set of attributes used to look up records in a file.An index file consists of records (called index entries) of the formIndex files are typically much smaller than the original file Two basic kinds of indices:Ordered indice

3、s 有序索引: search keys are stored in sorted orderHash indices 散列索引: search keys are distributed uniformly across “buckets” using a “hash function”. search-keypointerIndex Evaluation MetricsAccess types supported efficiently. E.g., records with a specified value in the attributeor records with an attrib

4、ute value falling in a specified range of values.Access timeInsertion timeDeletion timeSpace overheadOrdered Indices In an ordered index, index entries are stored sorted on the search key value. E.g., author catalog in library.Primary index: in a sequentially ordered file, the index whose search key

5、 specifies the sequential order of the file.Also called clustering index 聚集索引The search key of a primary index is usually but not necessarily the primary key.Secondary index 輔助索引: an index whose search key specifies an order different from the sequential order of the file. Also called non-clustering

6、 index.Index-sequential file 順序索引文件: ordered sequential file with a primary index.Indexing techniques evaluated on basis of:Dense Index FilesDense index Index record appears for every search-key value in the file. Sparse Index FilesSparse Index: contains index records for only some search-key values

7、.Applicable when records are sequentially ordered on search-keyTo locate a record with search-key value K we:Find index record with largest search-key value KSearch file sequentially starting at the record to which the index record pointsLess space and less maintenance overhead for insertions and de

8、letions.Generally slower than dense index for locating records.Good tradeoff: sparse index with an index entry for every block in file, corresponding to least search-key value in the block.Example of Sparse Index FilesMultilevel IndexIf primary index does not fit in memory, access becomes expensive.

9、To reduce number of disk accesses to index records, treat primary index kept on disk as a sequential file and construct a sparse index on it.outer index a sparse index of primary indexinner index the primary index fileIf even outer index is too large to fit in main memory, yet another level of index

10、 can be created, and so on.Indices at all levels must be updated on insertion or deletion from the file.Multilevel Index (Cont.)Index Update: DeletionIf deleted record was the only record in the file with its particular search-key value, the search-key is deleted from the index also.Single-level ind

11、ex deletion:Dense indices deletion of search-key is similar to file record deletion.Sparse indices if an entry for the search key exists in the index, it is deleted by replacing the entry in the index with the next search-key value in the file (in search-key order). If the next search-key value alre

12、ady has an index entry, the entry is deleted instead of being replaced.Index Update: InsertionSingle-level index insertion:Perform a lookup using the search-key value appearing in the record to be inserted.Dense indices if the search-key value does not appear in the index, insert it.Sparse indices i

13、f index stores an entry for each block of the file, no change needs to be made to the index unless a new block is created. If a new block is created, the first search-key value appearing in the new block is inserted into the index.Multilevel insertion (as well as deletion) algorithms are simple exte

14、nsions of the single-level algorithmsSecondary IndicesFrequently, one wants to find all the records whose values in a certain field (which is not the search-key of the primary index) satisfy some condition.Example 1: In the account relation stored sequentially by account number, we may want to find

15、all accounts in a particular branchExample 2: as above, but where we want to find all accounts with a specified balance or range of balancesWe can have a secondary index with an index record for each search-key value index record points to a bucket that contains pointers to all the actual records wi

16、th that particular search-key value.Secondary Index on balance field of accountPrimary and Secondary IndicesSecondary indices have to be dense. Why? Indices offer substantial benefits when searching for records.When a file is modified, every index on the file must be updated, Updating indices impose

17、s overhead on database modification.Sequential scan using primary index is efficient, but a sequential scan using a secondary index is expensive . Why? each record access may fetch a new block from diskB+-Tree Index FilesDisadvantage of indexed-sequential files: performance degrades as file grows, s

18、ince many overflow blocks get created. Periodic reorganization of entire file is required.Advantage of B+-tree index files: automatically reorganizes itself with small, local, changes, in the face of insertions and deletions. Reorganization of entire file is not required to maintain performance.Disa

19、dvantage of B+-trees: extra insertion and deletion overhead, space overhead.Advantages of B+-trees outweigh disadvantages, and they are used extensively.B+-tree indices are an alternative to indexed-sequential files.B+-Tree Index Files (Cont.)All paths from root to leaf are of the same lengthA leaf

20、node has between (n1)/2 (向上取整數(shù))and n1 valuesroot : If the root is not a leaf, it has at least 2 children.If the root is a leaf (that is, there are no other nodes in the tree), it can have between 0 and (n1) values.Each node except the root and the leaf has between n/2 (向上取整數(shù)) and n children.A B+-tre

21、e is a rooted tree satisfying the following properties:B+-Tree Node StructureTypical nodeKi are the search-key values Pi are pointers to children (for non-leaf nodes) or pointers to records or buckets of records (for leaf nodes).The search-keys in a node are ordered Leaf Nodes in B+-TreesFor i = 1,

22、2, . . ., n1, pointer Pi either points to a file record with search-key value Ki, or to a bucket of pointers to file records, each record having search-key value Ki. Only need bucket structure if search-key does not form a primary key.If Li, Lj are leaf nodes and i j, Lis search-key values are less

23、than Ljs search-key valuesPn points to next leaf node in search-key orderProperties of a leaf node:Non-Leaf Nodes in B+-TreesNon leaf nodes form a multi-level sparse index on the leaf nodes. For a non-leaf node with m pointers:All the search-keys in the subtree to which P1 points are less than K1For

24、 2 i n 1, all the search-keys in the subtree to which Pi points have values greater than or equal to Ki1 and less than Km1Example of a B+-treeB+-tree for account file (n = 3)Example of B+-treeLeaf nodes must have between 2 and 4 values (n1)/2 and n 1, with n = 5).Non-leaf nodes other than root must

25、have between 3 and 5 children (n/2 and n with n =5).Root must have at least 2 children.B+-tree for account file (n = 5)Observations about B+-treesSince the inter-node connections are done by pointers, “l(fā)ogically” close blocks need not be “physically” close.The non-leaf levels of the B+-tree form a h

26、ierarchy of sparse indices.The B+-tree contains a relatively small number of levels (logarithmic in the size of the main file), thus searches can be conducted efficiently.Insertions and deletions to the main file can be handled efficiently, as the index can be restructured in logarithmic time (as we

27、 shall see).Queries on B+-Trees比教材P492的清晰, 源自教材最新版本Queries on B+-Trees (Cont.)In processing a query, a path is traversed in the tree from the root to some leaf node.If there are K search-key values in the file, the path is no longer than logn/2(K).A node is generally the same size as a disk block, t

28、ypically 4 kilobytes, and n is typically around 100 (40 bytes per index entry).With 1 million 百萬search key values and n = 100, at most log50(1,000,000) = 4 nodes are accessed in a lookup.Contrast this with a balanced binary free with 1 million search key values around 20 nodes are accessed in a look

29、upabove difference is significant since every node access may need a disk I/O, costing around 20 milliseconds!Updates on B+-Trees: InsertionFind the leaf node in which the search-key value would appearIf the search-key value is already there in the leaf node, record is added to file and if necessary

30、 a pointer is inserted into the bucket.If the search-key value is not there, then add the record to the main file and create a bucket if necessary. Then:If there is room in the leaf node, insert (key-value, pointer) pair in the leaf nodeOtherwise, split the node (along with the new (key-value, point

31、er) entry) as discussed in the next slide.Updates on B+-Trees: Insertion (Cont.)Splitting a node:take the n(search-key value, pointer) pairs (including the one being inserted) in sorted order. Place the first n/2 in the original node, and the rest in a new node.let the new node be p, and let k be th

32、e least key value in p. Insert (k,p) in the parent of the node being split. If the parent is full, split it and propagate the split further up. 這個上移的k, 是這n個值的中間值.The splitting of nodes proceeds upwards till a node that is not full is found. In the worst case the root node may be split increasing the

33、 height of the tree by 1. Updates on B+-Trees: Insertion (Cont.)B+-Tree before and after insertion of “Clearview”Insertion in B+-Trees (Cont.)Read pseudocode in book!糾錯:Page 495, Figure 12.13: In procedure insert in leaf, “Let Ki be the least value in L that is less than K”“Let Ki be the highest val

34、ue in L that is less than K”.Updates on B+-Trees: DeletionFind the record to be deleted, and remove it from the main file and from the bucket (if present)Remove (search-key value, pointer) from the leaf node if there is no bucket or if the bucket has become emptyIf the node has too few entries due t

35、o the removal, and the entries in the node and a sibling fit into a single node, then 合并Insert all the search-key values in the two nodes into a single node (the one on the left), and delete the other node.Delete the pair (Ki1, Pi), where Pi is the pointer to the deleted node, from its parent, recur

36、sively using the above procedure.Updates on B+-Trees: DeletionOtherwise, if the node has too few entries due to the removal, and the entries in the node and a sibling can not fit into a single node, thenRedistribute the pointers between the node and a sibling such that both have more than the minimu

37、m number of entries.Update the corresponding search-key value in the parent of the node.The node deletions may cascade upwards till a node which has n/2 or more pointers is found. If the root node has only one pointer after deletion, it is deleted and the sole child becomes the root. O(h)Examples of

38、 B+-Tree DeletionThe removal of the leaf node containing “Downtown” did not result in its parent having too little pointers. So the cascaded deletions stopped with the deleted leaf nodes parent.Before and after deleting “Downtown”Examples of B+-Tree Deletion (Cont.)Node with “Perryridge” becomes und

39、erfull (actually empty, in this special case) and merged with its sibling.As a result “Perryridge” nodes parent became underfull, and was merged with its sibling (and an entry was deleted from their parent)Root node then had only one child, and was deleted and its child became the new root nodeDelet

40、ion of “Perryridge” from result of previous exampleExample of B+-tree Deletion (Cont.)Parent of leaf containing Perryridge became underfull, and borrowed a pointer from its left siblingSearch-key value in the parents parent changes as a resultBefore and after deletion of “Perryridge” from earlier ex

41、ampleB+-Tree File OrganizationIndex file degradation 退化 problem is solved by using B+-Tree indices. Data file degradation problem is solved by using B+-Tree File Organization.The leaf nodes in a B+-tree file organization store records, instead of pointers.Since records are larger than pointers, the

42、maximum number of records that can be stored in a leaf node is less than the number of pointers in a nonleaf node.Leaf nodes are still required to be half full.Insertion and deletion are handled in the same way as insertion and deletion of entries in a B+-tree index.B+-Tree File Organization (Cont.)

43、Good space utilization important since records use more space than pointers. To improve space utilization, involve more sibling nodes in redistribution during splits and mergesInvolving 2 siblings in redistribution (to avoid split / merge where possible) results in each node having at least entriesE

44、xample of B+-tree File OrganizationIndexing StringsVariable length strings as keysVariable fanout: 不同的節(jié)點(diǎn)孩子數(shù)差異巨大Use space utilization as criterion for splitting, not number of pointersPrefix compressionKey values at internal nodes can be prefixes of full keyKeep enough characters to distinguish entri

45、es in the subtrees separated by the key valueE.g. “Silas” and “Silberschatz” can be separated by “Silb”B-Tree Index FilesSimilar to B+-tree, but B-tree allows search-key values to appear only once; eliminates redundant storage of search keys.Search keys in nonleaf nodes appear nowhere else in the B-

46、tree; an additional pointer field for each search key in a nonleaf node must be included.Generalized B-tree leaf nodeNonleaf node pointers Bi are the bucket or file record pointers.B-Tree Index File ExampleB-tree (above) and B+-tree (below) on same dataB-Tree Index Files (Cont.)Advantages of B-Tree

47、indices:May use less tree nodes than a corresponding B+-Tree.Sometimes possible to find search-key value before reaching leaf node.Disadvantages of B-Tree indices:Only small fraction of all search-key values are found early Non-leaf nodes are larger, so fan-out is reduced. Thus, B-Trees typically ha

48、ve greater depth than corresponding B+-TreeInsertion and deletion more complicated than in B+-Trees Implementation is harder than B+-Trees.Typically, advantages of B-Trees do not out weigh disadvantages. Multiple-Key AccessUse multiple indices for certain types of queries.Example: select account_num

49、berfrom accountwhere branch_name = “Perryridge” and balance = 1000Possible strategies for processing query using indices on single attributes:1.Use index on branch_name to find accounts with balances of $1000; test branch_name = “Perryridge”. 2.Use index on balance to find accounts with balances of

50、$1000; test branch_name = “Perryridge”.3.Use branch_name index to find pointers to all records pertaining to the Perryridge branch. Similarly use index on balance. Take intersection of both sets of pointers obtained.Indices on Multiple KeysComposite search keys are search keys containing more than o

51、ne attributeE.g. (branch_name, balance)Lexicographic ordering: (a1, a2) (b1, b2) if either a1 a2, or a1=a2 and a2 b2Indices on Multiple Attributes With the where clause where branch_name = “Perryridge” and balance = 1000the index on (branch_name, balance) can be used to fetch only records that satis

52、fy both conditions.Using separate indices in less efficient we may fetch many records (or pointers) that satisfy only one of the conditions.Can also efficiently handle where branch_name = “Perryridge” and balance 1000But cannot efficiently handle where branch_name “Perryridge” and balance = 1000May

53、fetch many records that satisfy the first but not the second conditionSuppose we have an index on combined search-key(branch_name, balance).Non-Unique Search Keys 大致了解Multiple records matches the same sk.Alternatives: Buckets= List of tuple pointers with each keyKeep buckets in leaf nodes.Extra code

54、 to handle long listsDeletion of a tuple can be expensiveLow space overhead, no extra cost for queriesMake search key unique by adding a record-identifierExtra storage overhead for keysSimpler code for insertion/deletionWidely usedOther Issues大致了解Covering indices 覆蓋索引Add extra attributes to index so

55、 (some) queries can avoid fetching the actual recordsParticularly useful for secondary indices Why?Can store extra attributes only at leafRecord relocation and secondary indicesIf a record moves, all secondary indices that store record pointers have to be updated Node splits in B+-tree file organiza

56、tions become very expensiveSolution: in secondary index, replace each pointer (disk address) with a value of primary-index search key. Extra traversal of primary index to locate recordHigher cost for queries, but node splits are cheapAdd record-id if primary-index search key is non-uniqueStatic Hash

57、ingA bucket is a unit of storage containing one or more records (a bucket is typically a disk block) . In a hash file organization, we obtain the bucket of a record directly from its search-key value using a hash function.Hash function h is a function from the set of all search-key values K to the s

58、et of all bucket addresses B.Hash function is used to locate records for access, insertion as well as deletion.Records with different search-key values may be mapped to the same bucket; thus entire bucket has to be searched sequentially to locate a record. Example of Hash File Organization (Cont.)Th

59、ere are 10 buckets,The binary representation of the ith character is assumed to be the integer i.The hash function returns the sum of the binary representations of the characters modulo 10E.g. h(Perryridge) = 5 h(Round Hill) = 3 h(Brighton) = 3Hash file organization of account file, using branch_nam

60、e as key (See figure in next slide.)Example of Hash File Organization Hash file organization of account file, using branch_name as key (see previous slide for details).Hash FunctionsWorst hash function maps all search-key values to the same bucket; this makes access time proportional to the number o

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論