Overview of Storage and Indexing Chapter 8 Basics
Overview of Storage and Indexing Chapter 8 Basics about file management 2. Introduction to indexing First glimpse at indices and workloads 1. 3. 1
Motivation v v DBMS stores vast quantities of data Data is stored on external storage devices and fetched into main memory as needed for processing Page is the unit of information read from or written to disk. (often in DBMS, a page has size 4 -8 KB). Data on external storage devices : § Disks: Can retrieve random page at fixed cost But reading several consecutive pages is much cheaper than random order reading them in § Tapes: Can only read pages in sequence Cheaper than disks; used for archival storage v Cost of page I/O dominates the cost of typical database operations 2
Structure of a DBMS: Layered Architecture These layers must consider concurrency control and recovery Query Optimization and Execution v external storage access §Disk space manager manages persistent data §Buffer manager stages pages from external storage to main memory buffer pool. §File and index layers make calls to the buffer manager. Relational Operators Files and Access Methods Buffer Management Disk Space Management DB 3
Data on External Storage v 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 4
File Organizations v Alternatives (good for some ops, bad for 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 to optimize certain kinds of retrieval operations. • Like sorted files, they speed up searches for a subset of records, based on values in certain (“search key”) fields • Updates are much faster than in sorted files. 5
Indexes v Index on file speeds up selections on search key fields for index. § § v Search key is not the same as (primary) key Any attribute you want to search on could be a search key. Data Entry § Records stored in an index file § Given key value k, provide for efficient retrieval of all data entries k* with the value k. 6
Alternatives for Data Entry k* in Index v In a data entry k* we can store: § Data record with key value k, or § <k, rid of data record with search key value k>, or § <k, list of rids of data records with search key k> v Choice of alternative for data entries is orthogonal to indexing technique used to locate data entries with given key value k. § Examples of indexing techniques: B+ trees, hash-based structures § Typically, index contains auxiliary information that directs searches to the desired data entries 7
Alternatives for Data Entries (Contd. ) v Alternative 1: (Data record with key value k) § § § Index structure is the 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. (Otherwise, data records are duplicated, leading to redundant storage and potential inconsistency. ) If data records are very large -> implies size of auxiliary information in the index is also large, typically. 8
Alternatives for Data Entries (Contd. ) v Alternatives 2 (<k, rid>) and 3 (<k, rid-list>): Data entries typically much smaller than data records. § v Comparison : § Better than Alternative 1 with large data records, especially if search keys are small, as index would be much smaller. § Alternative 3 more compact than Alternative 2, but leads to variable sized data entries even if search keys are of fixed length. 9
Index Classification v Primary vs. secondary: If search key contains primary key, then called primary index. § Careful about terminology! v 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 !! 10
Clustered vs. Unclustered Index v Suppose that Alternative (2) is used for data entries, and data records are stored in 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. (Thus, order of data recs is `close to’, but not identical to, the sort order. ) CLUSTERED Index entries direct search for data entries Data entries UNCLUSTERED Data entries (Index File) (Data file) Data Records 11
B+ Tree Indexes Non-leaf Pages Leaf Pages (Sorted by search key) Leaf pages contain data entries, and are chained (prev & next) v Non-leaf pages have index entries; only used to direct searches: v index entry P 0 K 1 P 1 K 2 P 2 K m Pm 12
Example B+ Tree Note how data entries in leaf level are sorted Root 17 Entries <= 17 5 2* 3* v v Entries > 17 27 13 5* 7* 8* 14* 16* 22* 24* 30 27* 29* 33* 34* 38* 39* Find 28*? 29*? All > 15* and < 30* Insert/delete: Find data entry in leaf, then change it. Need to adjust parent sometimes or even ancestors. 13
Hash-Based Indexes v v Good for equality selections. Index is a collection of buckets. § Bucket = primary page plus zero or more overflow pages. § Buckets contain data entries. v Hashing function h: h(r) = bucket in which (data entry for) record r belongs. h looks at search key fields of r. § No need for “index entries” in this scheme. 14
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 § Note: § § • • Measuring number of page I/O’s ignores gains of pre-fetching a sequence of pages; thus, even I/O cost is only approximated. Average-case analysis; based on several simplistic assumptions. * Good enough to show the overall trends! 15
Comparing File Organizations v v v Heap files (random order; insert at eof) Sorted files, sorted on <age, sal> Clustered B+ tree file, Alternative (1), search key <age, sal> Heap file with unclustered B + tree index on search key <age, sal> Heap file with unclustered hash index on search key <age, sal> 16
Operations to Compare v v v Scan: Fetch all records from disk Equality search Range selection Insert a record Delete a record 17
Assumptions in Our Analysis v Heap Files: § v Sorted Files: § v Equality selection on key; exactly one match. Files compacted after deletions. Indexes: § Alt (2), (3): data entry size = 10% size of record § Hash: No overflow buckets. • § 80% page occupancy => File size = 1. 25 data size Tree: 67% occupancy (this is typical). • Implies file size = 1. 5 data size 18
Assumptions (contd. ) v Scans: § Leaf levels of a tree-index are chained. § Index data-entries plus actual file scanned for unclustered indexes. v Range searches: § We use tree indexes to restrict the set of data records fetched, but ignore hash indexes. 19
Cost of Operations * Several assumptions underlie these (rough) estimates! 20
B: The number of data pages R: Number of records per page D: (Average) time to read or write disk page Heap File Scan Equality Range Insert Delete BD 0. 5 BD BD 2 D Search + D On average scan half the file. Uniform distribution. Record can Add at the end. appear anywhere in the Fetch last page file. + add record + write page back 21
B: The number of data pages R: Number of records per page D: (Average) time to read or write disk page Sorted File Scan Equality BD Dlog 2 B Result is sorted. Equality selection matches the sort order. Range Insert Delete D(log 2 B+#p Search + BD gs with match records) Matching middle of file. Same with insert. Read later half and write. Binary search. 22
B: The number of data pages R: Number of records per page D: (Average) time to read or write disk page Clustered Files Scan Equality Range Insert Delete 1. 5 BD Dlog. F 1. 5 B D(log. F 1. 5 B +#pgs with match records) Search + D Search + one write same (1) Pages in clusered file are 67% occupancy (2) # pages = 1. 5 B (3) F (fan out) 23
B: The number of data pages R: Number of records per page D: (Average) time to read or write disk page Heap File with Un. Clustered Tree Index Scan Equality Range Insert Delete BD(R+0. 15) D(1+ log. F 0. 15 B) D(log. F 0. 15 B +#pgs with match records) D(3 + log. F 0. 15 B) Search + 2 D Read data entry: 0. 15 BD Fetch the employee record for each data entry in index(unclustered) Insert in heap file 2 D Find right leaf page log. F 0. 15 B, add new data entry, write back D (1) Pages in clusered file are 67% occupancy (2) F (fan out) (3) Index is a tenth the size of an employee data record (4) # pages = 0. 1(1. 5 B) = 0. 15 B 24
B: The number of data pages R: Number of records per page D: (Average) time to read or write disk page Heap File with Un. Clustered Hash Index Scan Equality Range Insert Delete BD(R+0. 125) 2 D BD 4 D Search + 2 D Fetch data entry from index file Hash structure offers no help Read data entry: 0. 15 BD Insert record into heap file 2 D Delete index D Delete data page D Fetch the employee record for each data entry in index(unclustered) Fetch data record from file Scan entire heap file Insert into index page and write back 2 D (1) Pages in clusered file are 80% occupancy (2) F (fan out) (3) Index is a 10 th the size of an employee data record (4) # pages = 0. 1(1. 25 B) = 0. 125 B 25
Cost of Operations B: The number of data pages R: Number of records per page D: (Average) time to read or write disk page D(3 + log. F 0. 15 B) 4 D Search + 2 D * Several assumptions underlie these (rough) estimates! Conclusion: No one file organization is uniformly superior in all situations !!! 26
Summary v v Many alternative file organizations exist, each appropriate in some situation. If selection queries are frequent, sorting the file or building an index is important. § § § v Hash-based indexes only good for equality search. Sorted files and tree-based indexes best for range search; also good for equality search. Files rarely kept sorted in practice; B+ tree index is better. Index is a collection of data entries plus a way to quickly find entries with given key values. 27
Summary v Data entries can be actual data records, <key, rid> pairs, or <key, rid-list> pairs. § v v v 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, primary vs. secondary Differences have important consequences for utility/performance. 28
- Slides: 28