Title: Overview%20of%20Storage%20and%20Indexing
1Overview of Storage and Indexing
- Chapter 8
- Basics about file management
- Introduction to indexing
- First glimpse at indices and workloads
2Motivation
- 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-8KB). - Data on external storage devices
- Disks Can retrieve random page at fixed cost
- But reading several consecutive pages is
much cheaper than reading them in random order - Tapes Can only read pages in sequence
- Cheaper than disks used for archival storage
- Cost of page I/O dominates the cost of typical
database operations
3Structure of a DBMSLayered Architecture
These layers must consider concurrency control
and recovery
- 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.
4Data on External 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
5File Organizations
- 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.
6Indexes
- Index on file speeds up selections on search
key fields for index. - 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.
7Alternatives for Data Entry k in Index
- In a data entry k we can store
- Data record with key value k, or
- ltk, rid of data record with search key value kgt,
or - ltk, list of rids of data records with search key
kgt - 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
8Alternatives for Data Entries (Contd.)
- 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 -gt implies size
of auxiliary information in the index is also
large, typically.
9Alternatives for Data Entries (Contd.)
- Alternatives 2 (ltk, ridgt) and 3 (ltk, rid-listgt)
- Data entries typically much smaller than data
records.
- 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.
10Index Classification
- Primary vs. secondary If search key contains
primary key, then called primary index. - Careful about terminology!
- 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 !!
11Clustered vs. Unclustered Index
- 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.)
Index entries
UNCLUSTERED
direct search for
CLUSTERED
data entries
Data entries
Data entries
(Index File)
(Data file)
Data Records
Data Records
12B 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
P
P
K
m
0
1
2
1
m
2
13Example B Tree
Note how data entries in leaf level are sorted
Root
17
Entries lt 17
Entries gt 17
27
30
13
5
2
3
39
38
7
5
8
22
24
27
29
14
16
33
34
- Find 28? 29? All gt 15 and lt 30
- Insert/delete Find data entry in leaf, then
change it. Need to adjust parent sometimes or
even ancestors.
14Hash-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 search
key fields of r. - No need for index entries in this scheme.
15Cost 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/Os 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!
16Comparing File Organizations
- Heap files (random order insert at eof)
- Sorted files, sorted on ltage, salgt
- Clustered B tree file, Alternative (1), search
key ltage, salgt - Heap file with unclustered B tree index on
search key ltage, salgt - Heap file with unclustered hash index on search
key ltage, salgt
17Operations to Compare
- Scan Fetch all records from disk
- Equality search
- Range selection
- Insert a record
- Delete a record
18Assumptions 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 gt File size 1.25 data size
- Tree 67 occupancy (this is typical).
- Implies file size 1.5 data size
19Assumptions (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.
20Cost of Operations
- Several assumptions underlie these (rough)
estimates!
21Heap File
B The number of data pages R Number of
records per page D (Average) time to read or
write disk page
Scan Equality Range Insert Delete
BD 0.5BD BD 2D Search D
On average scan half the file. Uniform distribution. Record can appear anywhere in the file. Add at the end. Fetch last page add record write page back
22Sorted File
B The number of data pages R Number of
records per page D (Average) time to read or
write disk page
Scan Equality Range Insert Delete
BD Dlog2B D(log2Bpgs with match records) Search BD Search BD
Result is sorted. Equality selection matches the sort order. Binary search. Matching middle of file. Read later half and write. Same with insert.
23Clustered Files
B The number of data pages R Number of
records per page D (Average) time to read or
write disk page
Scan Equality Range Insert Delete
1.5BD DlogF1.5B D(logF1.5B pgs with match records) Search D Search D
Search one write same
- Pages in clusered file are 67 occupancy
- pages 1.5B
- F (fan out)
24Heap File with UnClustered Tree Index
B The number of data pages R Number of
records per page D (Average) time to read or
write disk page
Scan Equality Range Insert Delete
BD(R0.15) D(1 logF0.15B) D(logF0.15B pgs with match records) D(3 logF0.15B) Search 2D
Read data entry 0.15BD Fetch the employee record for each data entry in index(unclustered) Insert in heap file 2D Find right leaf page logF0.15B, add new data entry, write back D
- Pages in clusered file are 67 occupancy
- F (fan out)
- Index is a tenth the size of an employee data
record - pages 0.1(1.5B) 0.15B
25Heap File with UnClustered Hash Index
B The number of data pages R Number of
records per page D (Average) time to read or
write disk page
Scan Equality Range Insert Delete
BD(R0.125) 2D BD 4D Search 2D
Read data entry 0.15BD Fetch the employee record for each data entry in index(unclustered) Fetch data entry from index file Fetch data record from file Hash structure offers no help Scan entire heap file Insert record into heap file 2D Insert into index page and write back 2D Delete index D Delete data page D
- Pages in clusered file are 80 occupancy
- F (fan out)
- Index is a 10th the size of an employee data
record - pages 0.1(1.25B) 0.125B
26Cost of Operations
B The number of data pages R Number of
records per page D (Average) time to read or
write disk page
Search 2D
D(3 logF0.15B)
Search 2D
4D
- Several assumptions underlie these (rough)
estimates!
Conclusion No one file organization is uniformly
superior in all situations !!!
27Summary
- 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.
28Summary
- Data entries can be actual data records, ltkey,
ridgt pairs, or ltkey, rid-listgt 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, primary vs. secondary - Differences have important consequences for
utility/performance.