0 ratings0% found this document useful (0 votes) 18 views9 pagesDBMS Unit-4
Database Management System Notes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
UNIT - 4
UAAARAAR AA RAPA BR RPUNIT—4
Representing Data Elements & Index Structures
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
‘Tapes: Can only read pages in sequence
* Cheaper than disks; used for archival storage.
File organization and Indexing:
Eile 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
Architecture: Buffer manager stages pages from external storage to main memory buffer
pool. File and index layers make calls to the buffer manager.
Primary and secondary Indexes:
Primary vs. secondary: If'search key contains primary key, then called primary index.
Unique index: Search key contains a candidate key.
Clustered and u tered:
Clustered ys. unclustered: If order of data records is the same as, or ‘close to’, order of data
entries, then called clustered index.
j-——+—Altemative-+ implies clustered; in practice; clustered also implies Alternative {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!Fenn oie tencertaperoniied troupe ocreerency ie ces cs.
Clustered vs, Unclustered Index
* Suppose that Alternative (2) 18 used for data entries, and that the data records are stored
ina Heap file.
‘To build clustered index, first sort the Heap file (with some free Space on cach page for
future inserts).
UNCLUSTERED.
Overflow pages may be needed for inserts. (Thus, order of data recs is “close to", but not
identical to, the sort order.)
Index Data Structures:
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.
* Search key is not the same as hey (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-
* Given data entry k*, we can find record with key k in at most one disk VO.
(Details soon ...)
B+ Tree Indexes
Example B+ Tree
Note how data entries |
level are sorted |
Coe
1 Find 28*? 29*? All > 15* and < 30*
2. Insert/delete: Find data entry in leaf, then change it Need to adjust parent sometimes.
‘© And change sometimes bubbles up the treeOeeety
CeCe ee oeeny:
A
RUE
RANK
AARARN
Hash-Based Indexing:
« 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 W.h(r) = bucket in which (data entry for) record r belongs. h looks
atthe search key fields of r
« Noneed for “index entries” in this scheme.
Altematives for Data Entry k* in Index
Ina data entry k* we can store:
«Data record with key value k, or
~ , or
¥
‘© Choice of alternative for data entries is orthogonal to the indexing technique used to
locate data entries with a given key value k
Tree Based Indexing:
— Examples of indexing techniques: B+ trees, hash-based structures
~ Typically, index contains auxiliary information that directs searches to the desired data
entries
Alternative 1:
__=__If this is used, index structure is a file organization for data records (i e
file). {instead of a fileor
= At most one index on a given collection of data records can use Altemative 1. (Otherwise,
data records are duplicated, leading to redundant storage and potential inconsistency.)
~ Ifdata records are very large, # of pages containing data entries is high.
Implies size of auxiliary information in the index 1s also large, typically‘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
Measuring number of page VO’s ignores gains of pre-fetching a sequence of pages, thus,
even VO cost is only approximated
~ _Average-case analysis; based on several simplistic assumptions
Choice of Indexes
1 ‘What indexes should we create?
Which relations should have indexes? What field(s) should be the search key?
‘Should we build several indexes?
For each index, what kind of an index should it be?
Clustered? Hash/tree?
1. Oneapproach: Consider the most important queries in tu. Consider the best plan using
the current indexes, and see if a better plan is possible with an additional index.
Iso, create it
~ Obviously, this implies that we must understand how & DBMS evaluates queries and creates
‘query evaluation plans?
= For now, we discuss simple 1-table queries.
Before creating an index, must also consider the impact on updates in the workload!
~ Trade-off. Indexes can make queries go faster, updates slower. Require disk space. too.Index Selection Guidelines
Attributes in WHERE clause are candidates for index keys.
ct match condition suggests hash index.
Range query suggests tree index
Clustering is especially useful for range queries, can also help on equality quenes if there are
many duplicates.
Multi-attribute search keys should be considered when a WHERE clause contains several
conditions.
Order of attributes is important for range queries.
~ Such indexes can sometimes enable index-only strategies for important queries.
For index-only strategies, clustering is not important!
Bt Tri
B+ Tree: Most Widely Used Index. InserV/delete at log ¢ N cost; keep tree height-balanced. (F
* fanout, N = # leaf pages) Minimum 50% occupancy (except for root). Each node contains d <=
m <= 2d entries, The parameter d is called the onder of the tree. Supports equality and
range-searches efficiently.
Example B+ Tree
1, Search begins at toot, and key comparisons direct it o a leaf (as in ISAM).
2. Search for S*, 15%, all data entries >= 24* .
B+ Trees in Practice
‘Typical order: 100. Typical fill-factor: 67%,
= average fanout * 133
‘Typical capacities
eS Height 4: 1334 « 312,900,700 records
Height 3: 133° 2,352,637 records
Can often hold top levels in buffer poot:
an~ Level = Epage= & Kbytes
- Level2= 133 pages= 1 Mbyte
- Level 3 = 17,689 pages = 133 MBytes
Inserting a Data Entry into a B+ Tree
Find correct leaf 1.
Put data entry omo L.
~ If L has enough space, done!
= Else, must split L (into L and a new node 1.2)
+ Redistribute entries evenly. copy up middie key.
+ Insert index entry pointing 10 L? into parent of L.
This can happen recursively
~~ Tossplit index node, redistribute entries evenly, but push up middle key. (Contrast with leaf
splits.) Splits “grow” tree; root split mereases height
~ Tree growth: gets wider or one level taller at top.
Inserting 8* into Example B+ Tree
Observe bow minimum occupancy ts guaranteed in both leaf and index pg splits.
Note difference between copy-up and push-up. be sure you understand the reasons for
this.
Example B+ Tree After Inserting 8*
ee Deleting « Data Entry from a B+ Tree 3
2 Start at root, find leaf £. where entry belongs,
3. Remove the entry.~ ene full, done!
~ IfLhas onty d-f entries, ——
«Try to redistribute, borrowing from sibling (adjacent node with same parent as 1.)
‘+ Ifre-distribution fails, merge L and sibling.
If merge occurred, must delete entry (pointing to L or sibling) from parent of L. Merge could
Propagate to root, decreasing height.
Example Tree After (Inserting 8*, Then) Deleting 19* and 20° ...
Deleting 19° is easy.
Deleting 20° is done with re-distribution. Notice how middle key is copied up... And
Then Deleting 24*
Must merge.
Observe “sass” of index entry (on right), and ‘pull down’ of index entry (below).
Hash Based Indexing:
Bucket: Hash file stores data in bucket format. Bucket is considered a unit of storage. Bucket
typically stores one complete disk block, which in turn can store one or more records
‘Hash Function: A hash function h, is a mapping function that maps all set of search-keys K to
the address where actual records are placed. It is a function from search keyto bucket addresses.
—_—_-