Overview of Storage and Indexing Chapter 8 Database
Overview of Storage and Indexing Chapter 8 Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 1
Looking Under the Hood: How a DBMS works Query Optimization and Execution Discussed so far Relational Operators Files and Access Methods New topic Buffer Management Disk Space Management DB Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 2
Motivation Computer Science: apply systems, algorithms and data structures to query processing. v Companies need to build code for SQL systems. v Database administrators need to know how to configure a database for faster queries. v Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 3
Data on External Storage v Disks: Can retrieve random page at fixed cost § But reading several consecutive pages is much cheaper than reading them in random order v Tapes: Can read pages only in sequence § Cheaper than disks; used for archival 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 v Architecture: Buffer manager stages pages from external storage to main memory buffer pool. File and index layers make calls to the buffer manager. Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 4
FINDING RECORDS Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 5
Selection = Retrieving Records Selection (in the algebra sense) involves finding records that match a condition. v E. g. SELECT * FROM RESERVES WHERE day<8/9/94 AND bid=5 AND sid=3 v NNext chapter is about evaluating joins v Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 6
File Organization Schulte’s First Law of File Organization: v The more effort you invest in organizing your files, the faster it is to retrieve records! v For example, how can you organize § reserves file to find dates § players file to find players in cluster Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 7
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. Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 8
Indexes vs. Sorted Files Both speed up searches for records based on the values of search key fields (e. g. age, salary). v What are pros and cons of sorting vs. indexing? + Can have many indexes for the same records. Only one sort order. + Index updates are much faster. v Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 9
Indexes Data Entries Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 10
Motivation Indexes can be used to speed up searches for data records or for data entries. v An index for data entries is like an index for a (book) index. v In web search, an index helps find URLs (not directly web pages). v Web Search Web page URL Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke Index Analogy data record data entry 11
Sorted Data Entries v data entry = (term, URL) § e. g. (“database”, https: //en. wikipedia. org/wiki/Database) assume 1 M disk pages to store data entries Ø log_2(1 M) page reads, about 20 v assume each page read takes 15 mlsec v Search takes 20 x 15 = 0. 3 sec v Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 12
Index Terminology v 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. v Example of Index: Multi-Agent Systems v Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 14
Alternatives for Data Entry k* in Index v Three alternatives: 1. Data record with key value k aka Covering Index 2. <k, rid of data record with search key value k> 3. <k, list of rids of data records with search key k> Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 15
Alternatives for Data Entries (Contd. ) v 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. Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 16
Example of Alternative 1 Covering Index shape round square rectangle colour red red holes 2 4 8 round square rectangle blue 2 4 8 Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 17
Example of Alternative 2 File with data records shape round square rectangle colour red red round square rectangle blue Index File with 6 data entries holes Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 2 4 8 colour red red 2 4 8 blue location 1 3 2 6 4 5 18
Example of Alternative 3 File with data records Index File with 6 data entries shape round square rectangle colour red red holes 2 4 8 round square rectangle blue 2 4 8 Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke colour red blue locations 1, 2, 3 4, 5, 6 19
Alternatives for Data Entries (Contd. ) v 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. Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 20
Index Types Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 21
Index Classification v Primary vs. secondary: If search key contains primary key, then called primary index. § v 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! Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 23
Clustered vs. Unclustered Index v 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. CLUSTERED Index entries direct search for data entries Data entries UNCLUSTERED Data entries (Index File) (Data file) Data Records Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke Data Records 24
Index Illustrations v v v Hash Insertion: 4 D I/Os. v 2 to read/write data page, 2 to read/write index entry. Hash Index Illustration. Clustered Tree Index Illustration. Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 25
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. v If Alternative (1) is used, the buckets contain the data records. v With (2, 3) they contain <key, rid> or <key, ridlist> pairs. Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke v 26
B+ Tree Indexes Non-leaf Pages Leaf pages contain data entries, and are chained (prev & next) v Non-leaf pages contain index entries; they direct searches: v index entry P 0 K 1 P 1 K 2 Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke P 2 K m Pm 27
Example B+ Tree Root 17 Entries < 17 5 2* 3* Entries >= 17 27 13 5* 7* 8* 14* 16* 22* 24* 30 27* 29* 33* 34* 38* 39* Find 28*? 29*? All > 17* and < 30* v Insert/delete: Find data entry in leaf, then change it. Need to adjust parent sometimes. v § And change sometimes bubbles up the tree Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 28
Example B-Tree • Pointers are located between key values in each index node. Ø For each key value, there is a unique pointer to follow. Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 29
Tree Search Cost to find record id log. F(#leaves) where F = number of children, the fan-out v In practice, F > 100 v binary search vs. tree search: compare log 2(1 M) vs log 100(1 M) v Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 30
Efficiency Analysis When to use what index Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 31
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! Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 33
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> Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 34
Operations to Compare v v v Scan: Fetch all records from disk Equality search (e. g. , “age = 30”) Range selection (e. g. , “age > 30”) Insert a record Delete a record Parameters of the Analysis B = # data pages R= #records/page Typical value Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke D = disk page I/O time C = process single record H = apply Hash F = index tree function fan-out 15 mlsec 100 nanosec 100 35
Assumptions in Our Analysis v v v 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% = 8 R. § 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. 7 R. Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 36
Scanning Cost (with computation) v v 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. 5 B (D+RC). § Pages only 67% full. Unclustered Tree Index: >BR(D+C). Bad! • for each record (BR) • retrieve page and find record (D + C). Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 37
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. Parameters B = # data pages R = #records/page Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke D = disk page I/O time F = index tree fanout 38
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. Parameters B = # data pages R = #records/page Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke D = disk page I/O time F = index tree fanout 39
Cost of Operations * Several assumptions underlie these (rough) estimates! Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 40
I/O Cost of Operations Explanations Scan Equality Range Insert Delete Heap BD 0. 5 BD BD 2 D Fetch, write Search + D Sorted BD Dlog 2 B Dlog 2 B + # matches Find first record, subsequent matches Search + 2*0. 5 BD Fetch, write 0. 5 B pages Search + BD Clustered Tree Index Alt. 1 1. 5 BD 1. 5 B data pages Dlog F 1. 5 B Leaf pages = data pages Dlog F 1. 5 B + # matches Search +D Search + D Unclustered Tree index BD(R+0. 15) 0. 15 B*D (read leaf pages) + (BR)*D (read each record) D(1 + log F 0. 15 B) D* log F 0. 15 B (find leaf page) + read data page D(log F 0. 15 B + # matches) D(3 +log F 0. 15 B) insert record(2 D) + insert data entry. Search + 2 D Unclustered Hash index BD(R+0. 125) 1. 25/10 B*D (Find each data entry)+ (BR)*D (reach record) 2 D (find data entry + find read data page) BD (scan) 4 D insert record (2 D) + insert data entry. Search + 2 D Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 41
I/O Cost of Operations Results * Several assumptions underlie these (rough) estimates! *Order of magnitude results, omit R, C, H. Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 42
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 Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 43
Summary Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 44
Queries and File Organization Many alternative file organizations exist, each appropriate for different tasks. v If selection queries are frequent, sorting the file or building an index is important. v v 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. Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 45
Index Types v Data entries can be actual data records, <key, rid> pairs, or <key, rid-list> 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. v Indexes can be classified as clustered vs. unclustered, and primary vs. secondary. v Differences have important consequences for utility/performance. v Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 46
- Slides: 43