File Organizations and Indexing Chapter 8

Slides:



Advertisements
Similar presentations
Database Management Systems, R. Ramakrishnan and J. Gehrke1 File Organizations and Indexing Chapter 8 How index-learning turns no student pale Yet holds.
Advertisements

Overview of Storage and Indexing
File Organizations and Indexing Lecture 4 R&G Chapter 8 "If you don't find it in the index, look very carefully through the entire catalogue." -- Sears,
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.
Tree-Structured Indexes
B+-Trees and Hashing Techniques for Storage and Index Structures
Introduction to Database Systems1 Records and Files Storage Technology: Topic 3.
Indexes and B-Trees Lecture 9 R & G Chapters 8 & 9
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Overview of Storage and Indexing Chapter 8 “How index-learning turns no student pale Yet.
File Organizations and Indexing Lecture 5 R&G Chapter 8 "If you don't find it in the index, look very carefully through the entire catalogue." -- Sears,
1 Overview of Storage and Indexing Chapter 8 (part 1)
1 File Organizations and Indexing Module 4, Lecture 2 “How index-learning turns no student pale Yet holds the eel of science by the tail.” -- Alexander.
1 Overview of Storage and Indexing Yanlei Diao UMass Amherst Feb 13, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
File Organizations and Indexing R&G Chapter 8 "If you don't find it in the index, look very carefully through the entire catalogue." -- Sears, Roebuck,
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Overview of Storage and Indexing Chapter 8 “How index-learning turns no student pale Yet.
File Organizations and Indexing Lecture 4 R&G Chapter 8 "If you don't find it in the index, look very carefully through the entire catalogue." -- Sears,
1 Overview of Storage and Indexing Chapter 8 1. Basics about file management 2. Introduction to indexing 3. First glimpse at indices and workloads.
DBMS Internals: Storage February 27th, Representing Data Elements Relational database elements: A tuple is represented as a record CREATE TABLE.
Tree-Structured Indexes. Range Searches ``Find all students with gpa > 3.0’’ –If data is in sorted file, do binary search to find first such student,
Layers of a DBMS Query optimization Execution engine Files and access methods Buffer management Disk space management Query Processor Query execution plan.
Storage and Indexing February 26 th, 2003 Lecture 19.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 File Organizations and Indexing Chapter 8.
Indexing - revisited CS 186, Fall 2012 R & G Chapter 8.
Overview of File Organizations and Indexing Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY courtesy of Joe Hellerstein for some slides.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Overview of Storage and Indexing Chapter 8.
File Organizations and Indexing Lecture 4 R&G Chapter 8 "If you don't find it in the index, look very carefully through the entire catalogue." -- Sears,
1 IT420: Database Management and Organization Storage and Indexing 14 April 2006 Adina Crăiniceanu
Database Management Systems, R. Ramakrishnan and J. Gehrke1 File Organizations and Indexing Chapter 8 “How index-learning turns no student pale Yet holds.
1 Overview of Storage and Indexing Chapter 8 (part 1)
Storage and Indexing1 Overview of Storage and Indexing.
1 Overview of Storage and Indexing Chapter 8 “How index-learning turns no student pale Yet holds the eel of science by the tail.” -- Alexander Pope ( )
DATABASE MANAGEMENT SYSTEMS TERM B. Tech II/IT II Semester UNIT-VIII PPT SLIDES Text Books: (1) DBMS by Raghu Ramakrishnan (2) DBMS by Sudarshan.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Overview of Storage and Indexing Chapter 8.
1 Overview of Storage and Indexing Chapter 8. 2 Data on External Storage  Disks: Can retrieve random page at fixed cost  But reading several consecutive.
Overview of Storage and Indexing Content based on Chapter 4 Database Management Systems, (Third Edition), by Raghu Ramakrishnan and Johannes Gehrke. McGraw.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Overview of Storage and Indexing Chapter 8 “How index-learning turns no student pale Yet.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Overview of Storage and Indexing Chapter 8 “If you don’t find it in the index, look very.
File Organizations and Indexing
Storage and Indexing. How do we store efficiently large amounts of data? The appropriate storage depends on what kind of accesses we expect to have to.
Indexing. 421: Database Systems - Index Structures 2 Cost Model for Data Access q Data should be stored such that it can be accessed fast q Evaluation.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Overview of Storage and Indexing Chapter 8.
1 Clustered vs. Unclustered Index Index entries Data entries direct search for (Index File) (Data file) Data Records data entries Data entries Data Records.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2007.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 File Organizations and Indexing Chapter 8 Jianping Fan Dept of Computer Science UNC-Charlotte.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Overview of Storage and Indexing Chapter 8.
1 Overview of Storage and Indexing Chapter 8. 2 Review: Architecture of a DBMS  A typical DBMS has a layered architecture.  The figure does not show.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Overview of Storage and Indexing Chapter 8.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Overview of Storage and Indexing Chapter 8 “If you don’t find it in the index, look very.
CS222: Principles of Data Management Lecture #4 Catalogs, Buffer Manager, File Organizations Instructor: Chen Li.
CS522 Advanced database Systems
Record Storage, File Organization, and Indexes
Pertemuan <<6>> Tempat Penyimpanan Data dan Indeks
CS222P: Principles of Data Management Notes #6 Index Overview and ISAM Tree Index Instructor: Chen Li.
Lecture 12 Lecture 12: Indexing.
Introduction to Database Systems File Organization and Indexing
Overview of Storage and Indexing
File Organizations and Indexing
File Organizations and Indexing
Overview of Storage and Indexing
Overview of Storage and Indexing
Indexing 1.
CS222/CS122C: Principles of Data Management Notes #6 Index Overview and ISAM Tree Index Instructor: Chen Li.
Storage and Indexing.
CS222p: Principles of Data Management Lecture #4 Catalogs, File Organizations Instructor: Chen Li.
General External Merge Sort
Files and access methods
Overview of Storage and Indexing
CS222/CS122C: Principles of Data Management UCI, Fall 2018 Notes #05 Index Overview and ISAM Tree Index Instructor: Chen Li.
File Organizations and Indexing
Overview of Storage and Indexing
Presentation transcript:

File Organizations and Indexing Chapter 8

Review: Memory, Disks, & Files Everything won’t fit in RAM (usually) Hierarchy of storage, RAM, disk, tape “Block” - unit of storage in RAM, on disk Allocate space on disk for fast access Buffer pool management Frames in RAM to hold blocks Policy to move blocks between RAM & disk Storing records within blocks

Today: File Storage How to keep blocks of records on disk but must support operations: scan all records search for a record id “RID” insert new records delete old records

Alternative File Organizations Many alternatives exist, each good for some situations, and not so good in others: Heap files: Suitable when typical access is a file scan retrieving all records. Sorted Files: Best for retrieval in search key order, or only a `range’ of records is needed. Clustered Files (with Indexes): Coming soon… Unclustered Files (with Indexes): Coming soon…

Cost Model for Analysis We ignore CPU costs, for simplicity: B: The number of data blocks (pages) R: Number of records per block D: (Average) time to read or write disk block Measuring number of block I/O’s ignores gains of pre-fetching and sequential access; thus, even I/O cost is only loosely approximated. Average-case analysis; based on several simplistic assumptions. Good enough to show the overall trends!

Some Assumptions in the Analysis Single record insert and delete. Equality selection - exactly one match (what if more or less???). Heap Files: Insert always appends to end of file. Sorted Files: Files compacted after deletions. Selections on search key.

Cost of Operations B: The number of data pages R: Number of records per page D: (Average) time to read or write disk page Cost of Operations Heap File Sorted File Clustered File Scan all records Equality Search Range Search Insert Delete

Cost of Operations B: The number of data pages R: Number of records per page D: (Average) time to read or write disk page Cost of Operations Heap File Sorted File Clustered File Scan all records BD Equality Search Range Search Insert Delete

Cost of Operations B: The number of data pages R: Number of records per page D: (Average) time to read or write disk page Cost of Operations Heap File Sorted File Clustered File Scan all records BD Equality Search 0.5 BD (log2 B) * D Range Search Insert Delete

Cost of Operations B: The number of data pages R: Number of records per page D: (Average) time to read or write disk page Cost of Operations Heap File Sorted File Clustered File Scan all records BD Equality Search 0.5 BD (log2 B) * D Range Search [(log2 B) + #match pg]*D Insert Delete

Cost of Operations B: The number of data pages R: Number of records per page D: (Average) time to read or write disk page Cost of Operations Heap File Sorted File Clustered File Scan all records BD Equality Search 0.5 BD (log2 B) * D Range Search [(log2 B) + #match pg]*D Insert 2D ((log2B)+B)D (because R,W 0.5) Delete

Cost of Operations B: The number of data pages R: Number of records per page D: (Average) time to read or write disk page Cost of Operations Heap File Sorted File Clustered File Scan all records BD Equality Search 0.5 BD (log2 B) * D Range Search [(log2 B) + #match pg]*D Insert 2D ((log2B)+B)D Delete 0.5BD + D ((log2B)+B)D (because R,W 0.5)

Indexes Find all students in the “CS” department Sometimes, we want to retrieve records by specifying the values in one or more fields, e.g., Find all students in the “CS” department Find all students with a gpa > 3 An index on a file is a disk-based data structure that 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. Search key is not the same as key (e.g. doesn’t have to be unique ID).

Indexes An index contains a collection of data entries, and supports efficient retrieval of all data entries k* with a given key value k. Given data entry k*, we can find record with key k in possibly one disk I/O. (Details soon …) 7

First Question to Ask About Indexes What kinds of selections do they support? Selections of form field <op> constant Equality selections (op is =) Range selections (op is one of <, >, <=, >=, BETWEEN) More exotic selections: 2-dimensional ranges (“west of Boston and east of Brighton and North of South Boston and South of Summerville”) Or n-dimensional 2-dimensional distances (“within 2 miles of Soda Hall”) Ranking queries (“10 restaurants closest to LOVE BUILDING”) Regular expression matches, genome string matches, etc. One common n-dimensional index: R-tree Supported in Oracle and Informix

Index Classification What selections does it support Representation of data entries in index i.e., what kind of info is the index actually storing? 3 alternatives here Primary vs. Secondary Indexes Clustered vs. Unclustered Indexes Single Key vs. Composite Indexes Tree-based, hash-based, other

B+ Tree Indexes Non-leaf Pages Leaf Pages (Sorted by search key) Leaf pages contain data entries, and are chained (prev & next) Non-leaf pages have index entries; only used to direct searches: index entry P K P K 1 2 P K P 1 2 m m 4

Example B+ Tree Find 28*? 29*? All > 15* and < 30* Note how data entries in leaf level are sorted Root 17 17 Entries <= 17 Entries > 17 17 Pointers to Actual Data Pages (rid) 5 13 27 30 2* 3* 5* 7* 8* 14* 16* 22* 24* 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. And change sometimes bubbles up the tree 15

Hash-Based Indexes Good for equality selections. Index is a collection of buckets. Bucket = primary page plus zero or more overflow pages. Buckets contain data entries. Hashing function h: h(r) = bucket in which (data entry for) record r belongs. h looks at the search key fields of r. No need for “index entries” in this scheme. 2

Index Classification Primary vs. secondary: If search key contains primary key, then called primary index. Unique index: Search key contains a candidate key. 11

Alternatives for Data Entry k* in Index Three alternatives: Actual data record (with key value k) <k, rid of matching data record> <k, list of rids of matching data records> Choice is orthogonal to the indexing technique. Examples of indexing techniques: B+ trees, hash-based structures, R trees, … Typically, index contains auxiliary information that directs searches to the desired data entries Can have multiple (different) indexes per file. E.g. file sorted by age, with a hash index on salary and a B+tree index on name.

Alternatives for Data Entries (Contd.) Alternative 1: Actual data record (with key value k) If this is used, index structure is a file organization for data records (like Heap files or sorted files). At most one index on a given collection of data records can use Alternative 1. This alternative saves pointer lookups but can be expensive to maintain with insertions and deletions.

Alternatives for Data Entries (Contd.) <k, rid of matching data record> and Alternative 3 <k, list of rids of matching data records> Easier to maintain than Alt 1. If more than one index is required on a given file, at most one index can use Alternative 1; rest must use Alternatives 2 or 3. Alternative 3 more compact than Alternative 2, but leads to variable sized data entries even if search keys are of fixed length. Even worse, for large rid lists the data entry would have to span multiple blocks!

Index Classification Clustered vs. unclustered: If order of data records is the same as, or `close to’, order of index data entries, then called clustered index. 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! Alternative 1 implies clustered, but not vice-versa.

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 block for future inserts). Overflow blocks may be needed for inserts. (Thus, order of data recs is `close to’, but not identical to, the sort order.) Index entries UNCLUSTERED CLUSTERED direct search for data entries Data entries Data entries (Index File) (Data file) Data Records Data Records

Unclustered vs. Clustered Indexes What are the tradeoffs???? Clustered Pros Efficient for range searches May be able to do some types of compression Possible locality benefits (related data?) ??? Clustered Cons Expensive to maintain (on the fly or sloppy with reorganization)

Clustered Files We usually refer a clustered Index using Alternative 1 as clustered files Pages are usually about 67 percent occupancy No. of physical data pages is about 1.5B (if B pages is required for storing all the data)

Cost of Operations B: The number of data pages R: Number of records per page D: (Average) time to read or write disk page Cost of Operations Heap File Sorted File Clustered File Scan all records BD 1.5 BD Equality Search 0.5 BD (log2 B) * D (logF 1.5B) * D Range Search [(log2 B) + #match pg]*D [(logF 1.5B) + #match pg]*D Insert 2D ((log2B)+B)D ((logF 1.5B)+1) * D Delete 0.5BD + D ((log2B)+B)D (because R,W 0.5)

Composite Search Keys Search on a combination of fields. Equality query: Every field value is equal to a constant value. E.g. wrt <age,sal> 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 Like the dictionary, but on fields, not letters! Examples of composite key indexes using lexicographic order. 11,80 11 12,10 12 name age sal 12,20 12 13,75 bob 12 10 13 <age, sal> cal 11 80 <age> joe 12 20 10,12 sue 13 75 10 20,12 Data records sorted by name 20 75,13 75 80,11 80 <sal, age> <sal> Data entries in index sorted by <sal,age> Data entries sorted by <sal>

Assumptions in Our Analysis Heap Files: Equality selection on key; exactly one match. Sorted Files: 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 0.125B no. of pages for data entries Tree: 67% occupancy (this is typical). Implies file size = 1.5 data size 0.15B no. of pages for data entries. 4

Assumptions (contd.) Scans: Leaf levels of a tree-index are chained. Index data-entries plus actual file scanned for unclustered indexes. Range searches: We use tree indexes to restrict the set of data records fetched, but ignore hash indexes.

Cost of Operations Several assumptions underlie these (rough) estimates! 5

Summary Many alternative file organizations exist, each appropriate in some situation. If selection queries are frequent, sorting the file or building an index is important. 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.

Summary (Contd.) Data entries in index can be actual data records, <key, rid> pairs, or <key, rid-list> pairs. Choice orthogonal to indexing structure (i.e. tree, hash, etc.). Usually have several indexes on a given file of data records, each with a different search key. Indexes can be classified as clustered vs. unclustered Differences have important consequences for utility/performance.