0% found this document useful (0 votes)
15 views5 pages

Ch08 Storage Indexing Overview

Uploaded by

tpo.bvce
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views5 pages

Ch08 Storage Indexing Overview

Uploaded by

tpo.bvce
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Data on External Storage

™ Disks: Can retrieve random page at fixed cost


ƒ But reading several consecutive pages is much cheaper than
reading them in random order

Overview of Storage and Indexing ™ Tapes: Can only read pages in sequence
ƒ Cheaper than disks; used for archival storage
™ File organization: Method of arranging a file of records
on external storage.
Chapter 8 ƒ 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
“How index-learning turns no student pale
™ Architecture: Buffer manager stages pages from external
Yet holds the eel of science by the tail.”
storage to main memory buffer pool. File and index
-- Alexander Pope (1688-1744) layers make calls to the buffer manager.

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 2

Alternative File Organizations Indexes


Many alternatives exist, each ideal for some
situations, and not so good in others: ™ An index on a file speeds up selections on the
ƒ Heap (random order) files: Suitable when typical search key fields for the index.
access is a file scan retrieving all records. ƒ Any subset of the fields of a relation can be the
ƒ Sorted Files: Best if records must be retrieved in search key for an index on the relation.
some order, or only a `range’ of records is needed. ƒ Search key is not the same as key (minimal set of
ƒ Indexes: Data structures to organize records via fields that uniquely identify a record in a relation).
trees or hashing. ™ An index contains a collection of data entries,
• Like sorted files, they speed up searches for a subset of and supports efficient retrieval of all data
records, based on values in certain (“search key”) fields
• Updates are much faster than in sorted files. entries k* with a given key value k.

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 3 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 4

Alternatives for Data Entries (Contd.)


Alternatives for Data Entry k* in Index
™ Three alternatives: ™ Alternative 1:
ƒ Data record with key value k ƒ If this is used, index structure is a file organization
for data records (instead of a Heap file or sorted
ƒ <k, rid of data record with search key value k> file).
ƒ <k, list of rids of data records with search key k> ƒ At most one index on a given collection of data
™ Choice of alternative for data entries is records can use Alternative 1. (Otherwise, data
records are duplicated, leading to redundant
orthogonal to the indexing technique used to storage and potential inconsistency.)
locate data entries with a given key value k. ƒ If data records are very large, # of pages
ƒ Examples of indexing techniques: B+ trees, hash- containing data entries is high. Implies size of
based structures auxiliary information in the index is also large,
ƒ Typically, index contains auxiliary information that typically.
directs searches to the desired data entries
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 5 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 6
Alternatives for Data Entries (Contd.) Index Classification
™ Alternatives 2 and 3: ™ Primary vs. secondary: If search key contains
ƒ Data entries typically much smaller than data primary key, then called primary index.
records. So, better than Alternative 1 with large ƒ Unique index: Search key contains a candidate key.
data records, especially if search keys are small.
(Portion of index structure used to direct search,
™ Clustered vs. unclustered: If order of data records
which depends on size of data entries, is much is the same as, or `close to’, order of data entries,
smaller than with Alternative 1.) then called clustered index.
ƒ Alternative 3 more compact than Alternative 2, but ƒ Alternative 1 implies clustered; in practice, clustered
leads to variable sized data entries even if search also implies Alternative 1 (since sorted files are rare).
keys are of fixed length. ƒ 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 3ed, R. Ramakrishnan and J. Gehrke 7 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 8

Clustered vs. Unclustered Index Hash-Based Indexes


™ 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 ™ Good for equality selections.
some free space on each page for future inserts). • Index is a collection of buckets. Bucket = primary
ƒ Overflow pages may be needed for inserts. (Thus, order of page plus zero or more overflow pages.
data recs is `close to’, but not identical to, the sort order.) • Hashing function h: h(r) = bucket in which
record r belongs. h looks at the search key fields
Index entries
UNCLUSTERED
of r.
CLUSTERED direct search for
data entries
™ If Alternative (1) is used, the buckets contain
the data records; otherwise, they contain <key,
Data entries
(Index File)
Data entries
rid> or <key, rid-list> pairs.
(Data file)

Data Records Data Records


Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 9 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 10

B+ Tree Indexes
Example B+ Tree
Root

17
Non-leaf
Pages
Entries <= 17 Entries > 17

5 13 27 30
Leaf
Pages

™ Leaf pages contain data entries, and are chained (prev & next) 2* 3* 5* 7* 8* 14* 16* 22* 24* 27* 29* 33* 34* 38* 39*

™ Non-leaf pages contain index entries and direct searches:


™ Find 28*? 29*? All > 15* and < 30*
index entry
™ Insert/delete: Find data entry in leaf, then
P0 K 1 P1 K 2 P 2 K m Pm
change it. Need to adjust parent sometimes.
ƒ And change sometimes bubbles up the tree
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 11 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 12
Cost Model for Our Analysis Comparing File Organizations
We ignore CPU costs, for simplicity:
™ Heap files (random order; insert at eof)
ƒ B: The number of data pages
ƒ R: Number of records per page ™ Sorted files, sorted on <age, sal>
ƒ D: (Average) time to read or write disk page ™ Clustered B+ tree file, Alternative (1), search
ƒ Measuring number of page I/O’s ignores gains of key <age, sal>
pre-fetching a sequence of pages; thus, even I/O ™ Heap file with unclustered B + tree index on
cost is only approximated. search key <age, sal>
ƒ Average-case analysis; based on several simplistic
assumptions.
™ Heap file with unclustered hash index on
search key <age, sal>
* Good enough to show the overall trends!
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 13 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 14

Operations to Compare Assumptions in Our Analysis


™ Heap Files:
ƒ Equality selection on key; exactly one match.
™ Scan: Fetch all records from disk
™ Equality search ™ Sorted Files:
ƒ Files compacted after deletions.
™ Range selection
™ Indexes:
™ Insert a record
ƒ Alt (2), (3): data entry size = 10% size of record
™ Delete a 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

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 15 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 16

Cost of Operations Cost of Operations


(a) Scan (b) (c ) Range (d) Insert (e) Delete (a) Scan (b) Equality (c ) Range (d) Insert (e) Delete
Equality
(1) Heap BD 0.5BD BD 2D Search
(1) Heap
+D
(2) Sorted (2) Sorted BD Dlog 2B
Dlog 2 B + Search Search
# matches + BD +BD
(3) Clustered (3) Clustered 1.5BD Dlog F 1.5B Dlog F 1.5B Search Search
+ # matches +D +D
(4) Unclustered
(4) Unclustered BD(R+0.15) D(1 + Dlog F 0.15B D(3 + Search
Tree index Tree index log F 0.15B) + # matches log F 0.15B) + 2D
(5) Unclustered (5) Unclustered BD(R+0.1 2D BD 4D Search
Hash index Hash index 25) + 2D

* Several assumptions underlie these (rough) estimates! * Several assumptions underlie these (rough) estimates!
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 17 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 18
Understanding the Workload Choice of Indexes
™ For each query in the workload: ™ What indexes should we create?
ƒ Which relations does it access? ƒ Which relations should have indexes? What field(s)
should be the search key? Should we build several
ƒ Which attributes are retrieved? indexes?
ƒ Which attributes are involved in selection/join conditions?
How selective are these conditions likely to be?
™ For each index, what kind of an index should it
be?
™ For each update in the workload:
ƒ Clustered? Hash/tree?
ƒ Which attributes are involved in selection/join conditions?
How selective are these conditions likely to be?
ƒ The type of update (INSERT/DELETE/UPDATE), and the
attributes that are affected.

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 19 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 20

Choice of Indexes (Contd.) Index Selection Guidelines


™ Attributes in WHERE clause are candidates for index keys.
™ One approach: Consider the most important queries ƒ Exact match condition suggests hash index.
in turn. Consider the best plan using the current ƒ Range query suggests tree index.
• Clustering is especially useful for range queries; can also help on
indexes, and see if a better plan is possible with an equality queries if there are many duplicates.
additional index. If so, create it. ™ Multi-attribute search keys should be considered when a
WHERE clause contains several conditions.
ƒ Obviously, this implies that we must understand how a
ƒ Order of attributes is important for range queries.
DBMS evaluates queries and creates query evaluation plans!
ƒ Such indexes can sometimes enable index-only strategies for
ƒ For now, we discuss simple 1-table queries. important queries.
• For index-only strategies, clustering is not important!
™ Before creating an index, must also consider the
™ Try to choose indexes that benefit as many queries as
impact on updates in the workload! possible. Since only one index can be clustered per relation,
ƒ Trade-off: Indexes can make queries go faster, updates choose it based on important queries that would benefit the
slower. Require disk space, too. most from clustering.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 21 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 22

Examples of Clustered Indexes


SELECT E.dno
Indexes with Composite Search Keys
FROM Emp E
™ B+ tree index on E.age can be WHERE E.age>40 ™ Composite Search Keys: Search Examples of composite key
indexes using lexicographic order.
used to get qualifying tuples. on a combination of fields.
ƒ How selective is the condition? SELECT E.dno, COUNT (*) ƒ Equality query: Every field
11,80 11
FROM Emp E value is equal to a constant
ƒ Is the index clustered? value. E.g. wrt <sal,age> index:
12,10 12
WHERE E.age>10 12,20 name age sal 12
™ Consider the GROUP BY query. GROUP BY E.dno
• age=20 and sal =75 13,75 bob 12 10 13
ƒ Range query: Some field value <age, sal> cal 11 80 <age>
ƒ If many tuples have E.age > 10, using is not a constant. E.g.: joe 12 20
E.age index and sorting the retrieved • age =20; or age=20 and sal > 10 10,12 sue 13 75 10
tuples may be costly. ™ Data entries in index sorted 20,12 Data records 20
75,13 sorted by name 75
ƒ Clustered E.dno index may be better! by search key to support 80,11 80
™ Equality queries and duplicates: SELECT E.dno range queries. <sal, age> <sal>

ƒ Clustering on E.hobby helps! FROM Emp E ƒ Lexicographic order, or Data entries in index Data entries
sorted by <sal,age> sorted by <sal>
WHERE E.hobby=Stamps ƒ Spatial order.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 23 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 24
SELECT D.mgr
Composite Search Keys Index-Only Plans FROM Dept D, Emp E
<E.dno> WHERE D.dno=E.dno
™ To retrieve Emp records with age=30 AND sal=4000, ™ A number of <E.dno,E.eid> SELECT D.mgr, E.eid
an index on <age,sal> would be better than an index queries can be Tree index!
FROM Dept D, Emp E
on age or an index on sal. WHERE D.dno=E.dno
answered
ƒ Choice of index key orthogonal to clustering etc. SELECT E.dno, COUNT(*)
without <E.dno> FROM Emp E
™ If condition is: 20<age<30 AND 3000<sal<5000: retrieving any GROUP BY E.dno
ƒ Clustered tree index on <age,sal> or <sal,age> is best. tuples from one SELECT E.dno, MIN(E.sal)
™ If condition is: age=30 AND 3000<sal<5000: or more of the <E.dno,E.sal> FROM Emp E
ƒ Clustered <age,sal> index much better than <sal,age> relations Tree index! GROUP BY E.dno
index! involved if a <E. age,E.sal> SELECT AVG(E.sal)
™ Composite indexes are larger, updated more often. suitable index or FROM Emp E
is available. <E.sal, E.age> WHERE E.age=25 AND
Tree! E.sal BETWEEN 3000 AND 5000
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 25 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 26

Index-Only Plans (Contd.) Summary


™ Index-only plans SELECT E.dno, COUNT (*)
are possible if the FROM Emp E ™ Many alternative file organizations exist, each
key is <dno,age> WHERE E.age=30 appropriate in some situation.
or we have a tree GROUP BY E.dno
™ If selection queries are frequent, sorting the
index with key file or building an index is important.
<age,dno>
SELECT E.dno, COUNT (*) ƒ Hash-based indexes only good for equality search.
ƒ Which is better?
ƒ What if we FROM Emp E ƒ Sorted files and tree-based indexes best for range
consider the WHERE E.age>30 search; also good for equality search. (Files rarely
second query? GROUP BY E.dno 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.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 27 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 28

Summary (Contd.)
Summary (Contd.)
™ Understanding the nature of the workload for the
application, and the performance goals, is essential
™ Data entries can be actual data records, <key, to developing a good design.
rid> pairs, or <key, rid-list> pairs. ƒ What are the important queries and updates? What
ƒ Choice orthogonal to indexing technique used to attributes/relations are involved?
locate data entries with a given key value. ™ Indexes must be chosen to speed up important
™ Can have several indexes on a given file of queries (and perhaps some updates!).
data records, each with a different search key. ƒ Index maintenance overhead on updates to key fields.
ƒ Choose indexes that can help many queries, if possible.
™ Indexes can be classified as clustered vs.
ƒ Build indexes to support index-only strategies.
unclustered, primary vs. secondary, and
ƒ Clustering is an important decision; only one index on a
dense vs. sparse. Differences have important given relation can be clustered!
consequences for utility/performance. ƒ Order of fields in composite index key can be important.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 29 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 30

You might also like