Download presentation
Presentation is loading. Please wait.
Published byDiana Hubbard Modified over 8 years ago
1
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Overview of Storage and Indexing Chapter 8
2
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke2 System Issues: How to Build a DBMS Query Optimization and Execution Relational Operators Files and Access Methods Buffer Management Disk Space Management DB Discussed so far New topic
3
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke3 Data on External Storage Disks: Can retrieve random page at fixed cost But reading several consecutive pages is much cheaper than reading them in random order Tapes: Can read pages only in sequence Cheaper than disks; used for archival storage File organization: Method of arranging a file of records on external storage. Record id (rid) is sufficient to physically locate record Indexes are data structures that allow us to find the record ids of records with given values in index search key fields Architecture: Buffer manager stages pages from external storage to main memory buffer pool. File and index layers make calls to the buffer manager.
4
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke4 Alternative File Organizations Many alternatives exist, each ideal for some situations, and not so good in others: Heap (random order) files: Suitable 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. Indexes: Data structures to organize records via trees or hashing.
5
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke5 Indexes vs. Sorted Files Both speed up searches for records based on the values of search key fields (e.g. age, salary). + Can have many indexes for the same records. Only one sort order. + Index updates are much faster.
6
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke6 Indexes Data Entries
7
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke7 Multiple Choice Question A data entry in an index a. is the same thing as a data record in a table. b. points to one or more data records in a table. true c. is longer than a data record. d. contains one or more record ids. Select all that apply.
8
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke8 Indexes An index on a file speeds up selections on the search key fields for the index. Any subset of the fields of a relation can be the search key for an index on the relation (e.g., age or colour). Search key is not the same as key (minimal set of fields that uniquely identify a record in a relation). An index contains a collection of data entries, and supports efficient retrieval of all data entries k* with a given key value k. Example of Index: Multi-Agent Systems Example of Index: Multi-Agent Systems
9
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke9 Alternatives for Data Entry k* in Index Three alternatives: 1. Data record with key value k aka Covering Index 2. 3.
10
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke10 Alternatives for Data Entries (Contd.) Alternative 1: Covering Index Index structure is a file organization for data records (instead of a Heap file or sorted file). At most one index on a given collection of data records can use Alternative 1. Why? If data records are very large, # of pages containing data entries is high. Implies size of auxiliary information in the index is also large, typically.
11
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke11 Example of Alternative 1 Covering Index shapecolourholes roundred2 squarered4 rectanglered8 roundblue2 squareblue4 rectangleblue8
12
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke12 Example of Alternative 2 shapecolourholes roundred2 squarered4 rectanglered8 roundblue2 squareblue4 rectangleblue8 File with data records colourlocation red1 3 2 blue6 4 5 Index File with 6 data entries
13
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke13 Example of Alternative 3 shapecolourholes roundred2 squarered4 rectanglered8 roundblue2 squareblue4 rectangleblue8 File with data records colourlocations red1,2,3 blue4,5,6 Index File with 6 data entries
14
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke14 Alternatives for Data Entries (Contd.) Alternatives 2 and 3: Data entries typically much smaller than data records. So, better than Alternative 1 with large data records, especially if search keys are small. Alternative 3 more compact than Alternative 2. But leads to variable sized data entries even if search keys are of fixed length.
15
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke15 Index Types
16
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke16 Binary Choice Question Consider a B+-tree index using index alternative organization (1), where data entry = data record. The index entries in the B+-tree contain complete information about the data record. True or False?
17
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke17 Index Classification Primary vs. secondary : If search key contains primary key, then called primary index. Unique index: Search key uniquely identifies record. Clustered vs. unclustered : If order of data records is the same as, or close to, order of data entries, then called clustered index. Alternative 1 implies clustered; in practice, clustered also implies Alternative 1 (since sorted files are rare). A file can be clustered on at most one search key. Cost of retrieving data records through index varies greatly based on whether index is clustered or not!
18
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke18 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 for inserts. Index entries Data entries direct search for (Index File) (Data file) Data Records data entries Data entries Data Records CLUSTERED UNCLUSTERED
19
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke19 Index Illustrations Hash Insertion: 4 D I/Os. 2 to read/write data page, 2 to read/write index entry. Hash Index Illustration. Hash Index Illustration. Clustered Tree Index Illustration. Clustered Tree Index Illustration.
20
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke20 Hash-Based Indexes Good for equality selections. Index is a collection of buckets. Bucket = primary page plus zero or more overflow pages. Hashing function h : h ( r ) = bucket in which record r belongs. h looks at the search key fields of r. If Alternative (1) is used, the buckets contain the data records. With (2,3) they contain or pairs.
21
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke21 B+ Tree Indexes Leaf pages contain data entries, and are chained (prev & next) Non-leaf pages contain index entries ; they direct searches: P 0 K 1 P 1 K 2 P 2 K m P m index entry Non-leaf Pages Leaf
22
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke22 Example B+ Tree Find 28*? 29*? All > 17* and < 30* Insert/delete: Find data entry in leaf, then change it. Need to adjust parent sometimes. And change sometimes bubbles up the tree 2*3* Root 17 30 14*16* 33*34* 38* 39* 135 7*5*8*22*24* 27 27*29* Entries < 17Entries >= 17
23
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke23 Example 2 Pointers are located between key values in each index node. For each key value, there is a unique pointer to follow.
24
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke24 Efficiency Analysis When to use what index
25
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke25 Multiple Choice Question The main cost factor for database query processing is 1. Disk page input/output operations. 2. #Records processed 3. Cost of sorting records. 4. Cost of maintaining an index. Choose one.
26
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke26 Cost Model for Our Analysis We ignore CPU costs, for simplicity: B: The number of data pages R: Number of records per page D: (Average) time to read or write disk page Average-case analysis; based on several simplistic assumptions. * Good enough to show the overall trends!
27
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke27 Comparing File Organizations Heap files (random order; insert at eof) Sorted files, sorted on Clustered B+ tree file, Alternative (1), search key Heap file with unclustered B + tree index on search key Heap file with unclustered hash index on search key
28
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke28 Operations to Compare Scan: Fetch all records from disk Equality search (e.g., “age = 30”) Range selection (e.g., “age > 30”) Insert a record Delete a record B = # data pages R = #records/p age D = disk page I/O time C = process single record H = apply Hash function F = index tree fan- out Typical value 15 mlsec100 nanosec 100 Parameters of the Analysis
29
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke29 Assumptions in Our Analysis Heap Files: Equality selection on key; exactly one match. Sorted Files: Files compacted after deletions. Clustered files: pages typically 67% full. ⇒ Total number pages needed = 1.5 B. Indexes: Alt (2), (3): data entry size = 10% size of record Hash: No overflow buckets. 80% page occupancy. ⇒ #Index pages = 1.25 B x 10% = 0.125 B. ⇒ #data entries/page = 10 R x 80% = 8R. Tree: 67% page occupancy of index pages (this is typical). ⇒ #leaf pages = (1.5 B) x 10% = 0.15 B. ⇒ #data entries/page = 10 R x 67% = 6.7R.
30
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke30 Scanning Cost (with computation) Heap file: B(D + RC). For each page (B) Read the page (D) For each record (R), process the record (C). Sorted File: B(D + RC). Have to go through all pages. Clustered File: 1.5B (D+RC). Pages only 67% full. Unclustered Tree Index: >BR(D+C). Bad! for each record (BR) retrieve page and find record (D + C).
31
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke31 Exercise for Group Work (no computation costs) 1.Estimate how long an equality search takes in (i) a heap file (ii) a sorted file. 2. Estimate how long an insertion takes in (i) a heap file (ii) a sorted file. Assume that insertion in a heap file is at the end, and that the sorted file has no empty slots. ParametersB = # data pages R = #records/page D = disk page I/O time F = index tree fan-out
32
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke32 Exercise Hash for Group Work (no computation costs) 1.Estimate how long an equality search takes in a hash file, hashed on the search key, with at most one record matching the search key (i.e., the search is on a key field). 2. Estimate how long an insertion takes in a hash file. ParametersB = # data pages R = #records/page D = disk page I/O time F = index tree fan-out
33
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke33 Cost of Operations * Several assumptions underlie these (rough) estimates!
34
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke34 I/O Cost of Operations Explanations ScanEqualityRangeInsert Delete Heap BD0.5BDBD2D Fetch, write Search + D Sorted BDDlog 2BDlog 2 B + # matches Find first record, subsequent matches Search + 2*0.5BD Fetch,write 0.5B pages Search + BD Clustered Tree Index Alt.1 1.5BD 1.5B data pages Dlog F 1.5B Leaf pages = data pages Dlog F 1.5B + # matches Search + D Search + D Unclustered Tree index BD(R+0.15) 0.15B*D (read leaf pages) + (BR)*D (read each record) D(1 + log F 0.15B) D* log F 0.15B (find leaf page) + read data page D(log F 0.15B + # matches) D(3 +log F 0.15B) insert record(2D) + insert data entry. Search + 2D Unclustered Hash index BD(R+0.125) 1.25/10B*D (Find each data entry)+ (BR)*D (reach each record) 2D (find data entry + find read data page) BD (scan) 4D insert record (2D) + insert data entry. Search + 2D
35
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke35 I/O Cost of Operations Results * Several assumptions underlie these (rough) estimates! * Order of magnitude results, omit R,C, H.
36
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke36 Informal Summary For heap file: pretty much need to scan for most operations. Except for inserts, at end of file is fine. For sorted file: binary search for equality search for matching record. Cost on order of log(#disk pages). For index: 1.need to find data entry (index page). a.constant cost for hash index. b.height of tree for B+-tree index. 2.need to find records that match data entries. a.for unclustered index: #pages = #matching records for clustered index: #matching pages
37
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke37 Summary
38
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke38 Queries and File Organization Many alternative file organizations exist, each appropriate for different tasks. If selection queries are frequent, sorting the file or building an index is important. Index is a collection of data entries plus a way to quickly find entries with given key values. Hash-based indexes only good for equality search. Sorted files and tree-based indexes best for range search; also good for equality search.
39
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke39 Index Types Data entries can be actual data records, pairs, or pairs. Choice orthogonal to indexing technique used to locate data entries with a given key value. Can have several indexes on a given file of data records, each with a different search key. Indexes can be classified as clustered vs. unclustered, and primary vs. secondary. Differences have important consequences for utility/performance.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.