Lecture3 File Orgn
Lecture3 File Orgn
Lecture 3 Chapter 8 "If you don't find it in the index, look very carefully through the entire catalogue." -- Sears, Roebuck, and Co., Consumer's Guide, 1897
Cost of Operations
Scan all records Equality Search Range Search Insert Delete
B: The number of data pages R: Number of records per page D: (Average) time to read or write disk page Sorted File Clustered File
Heap File
Cost of Operations
Scan all records Equality Search Range Search Insert Delete BD
B: The number of data pages R: Number of records per page D: (Average) time to read or write disk page Sorted File BD Clustered File
Heap File
Cost of Operations
Scan all records Equality Search Range Search Insert Delete BD
B: The number of data pages R: Number of records per page D: (Average) time to read or write disk page Sorted File BD Clustered File
Heap File
0.5 BD
(log2 B) * D
Cost of Operations
Scan all records Equality Search Range Search Insert Delete BD
B: The number of data pages R: Number of records per page D: (Average) time to read or write disk page Sorted File BD Clustered File
Heap File
0.5 BD
(log2 B) * D
BD
Cost of Operations
Scan all records Equality Search Range Search Insert Delete BD
B: The number of data pages R: Number of records per page D: (Average) time to read or write disk page Sorted File BD Clustered File
Heap File
0.5 BD
BD
2D
B: The number of data pages R: Number of records per page D: (Average) time to read or write disk page Sorted File BD Clustered File
0.5 BD
(log2 B) * D
BD
2D 0.5BD + D
Indexes
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. doesnt have to be unique ID). An index contains a collection of data entries, and supports efficient retrieval of all records with a given search key value k.
Ranking queries (10 restaurants closest to Berkeley) 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 Clustered vs. Unclustered Indexes Single Key vs. Composite Indexes Tree-based, hash-based, other
<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.
UNCLUSTERED
Data entries
Data Records
Data Records
10
B: The number of data pages R: Number of records per page D: (Average) time to read or write disk page Sorted File BD Clustered File 1.5 BD
0.5 BD
(log2 B) * D
(logF 1.5B) * D
BD
2D 0.5BD + D
11
Data entries in index sorted by search key to support range queries. Lexicographic order Like the dictionary, but on fields, not letters!
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.
12
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 dense vs. sparse Differences have important consequences for utility/performance.
13