DINLect1.pptx
DINLect1.pptx
Database Internals
Based on Ch12:
Silberschatz, Korth and Sudarchan Database System Concepts, 7th Edition, McGraw-Hill, 200?
1
Learning Outcome: At the completion of this course,
students should have
1. explain the basic ACID properties how it is
implemented in Databases
2. describe the basic fundamental concepts in
transaction processing
3. apply the knowledge of database internals to
improve the performance of a database system
4. demonstrate the use of an appropriate database
solution for an application that requires complex
data storage and manipulation.
3
■ Pre-requisites
⚪ Basics of Database Systems
⚪ Relational Model and Relational Algebra
⚪ Structured Query Language (SQL)
⚪ Database Design Process and E-R model
⚪ Relational Database Design,
Normalization
⚪ Application Programming with Databases
4
Main Topics
5
Review
■ Basics of Databases
⚪ A database is a collection of interrelated
data
⚪ A database management system (DBMS)
consists of a database and a set of
programs (software) designed for
accessing, maintaining and utilizing
database.
⚪ Advantages of DBMS over file-processing
systems
⚪ Levels of abstraction: physical level, logical
level, view level 6
Review
10
Indexing and Hashing
■ Basic Concepts
■ Ordered Indices
■ B+-Tree Index Files
■ B-Tree Index Files
■ Static Hashing
■ Dynamic Hashing
■ Comparison of Ordered Indexing and
Hashing
■ Index Definition in SQL
■ Multiple-Key Access 11
Basic Concepts
■ Indexing mechanisms are used to speed up access to
desired data.
⚪ E.g., author catalog in library
Index files are typically much smaller than the original file
■ Two basic kinds of indices:
⚪ Ordered indices: search keys are stored in sorted
order
⚪ Hash indices: search keys are distributed uniformly
12
across “buckets” using a “hash function”.
Index Evaluation Metrics
■ Access types supported efficiently.
E.g.,
⚪ records with a specified value in the
attribute
⚪ or records with an attribute value falling in
a specified range of values.
■ Access time
■ Insertion time
■ Deletion time
■ Space overhead
13
Ordered Indices
■ In an ordered index, index entries are stored sorted on
the search key value. E.g., author catalog in library.
■ Primary index: in a sequentially ordered file, the index
whose search key specifies the sequential order of the file.
⚪ Also called clustering index
15
Sparse Index Files
■ Sparse Index: contains index records for only
some search-key values.
⚪ Applicable when records are sequentially
ordered on search-key
■ To locate a record with search-key value K we:
⚪ Find index record with largest search-key value
<K
⚪ Search file sequentially starting at the record to
which the index record points
16
Sparse Index Files (Cont.)
■ Compared to dense indices:
⚪ Less space and less maintenance overhead for
insertions and deletions.
⚪ Generally slower than dense index for locating
records.
■ Good tradeoff: sparse index with an index entry for
every block in file, corresponding to least search-key
value in the block.
17
Multilevel Index
■ If primary index does not fit in memory, access
becomes expensive.
■ Solution: treat primary index kept on disk as a
sequential file and construct a sparse index on it.
⚪ outer index – a sparse index of primary index
18
Multilevel Index (Cont.)
19
Index Update: Insertion
■ Single-level index insertion:
⚪ Perform a lookup using the search-key value
appearing in the record to be inserted.
⚪ Dense indices – if the search-key value does not
appear in the index, insert it.
⚪ Sparse indices – if index stores an entry for each
block of the file, no change needs to be made to the
index unless a new block is created.
■ If a new block is created, the first search-key value
appearing in the new block is inserted into the index.
■ Multilevel insertion (as well as deletion) algorithms are
simple extensions of the single-level algorithms
20
Index Update: Deletion
■ If deleted record was the only record in the file with its
particular search-key value, the search-key is deleted from
the index also.
■ Single-level index deletion:
⚪ Dense indices – deletion of search-key: similar to file
record deletion.
⚪ Sparse indices –
21
Secondary Indices
■ Frequently, one wants to find all the records whose
values in a certain field (which is not the
search-key of the primary index) satisfy some
condition.
⚪ Example 1: In the account relation stored
sequentially by account number, we may want
to find all accounts in a particular branch
⚪ Example 2: as above, but where we want to find
all accounts with a specified balance or range of
balances
■ We can have a secondary index with an index
record for each search-key value
22
Secondary Indices Example
25
+
B -Tree Index Files (Cont.)
A B+-tree is a rooted tree satisfying the following
properties:
■ All paths from root to leaf are of the same length
■ Each node that is not a root or a leaf has between
⎡n/2⎤ and n children.
■ A leaf node has between ⎡(n–1)/2⎤ and n–1
values
■ Special cases:
⚪ If the root is not a leaf, it has at least 2
children.
⚪ If the root is a leaf (that is, there are no other
nodes in the tree), it can have between 0 and
(n–1) values.
26
B+-Tree Node Structure
■ Typical node
28
Non-Leaf Nodes in B+-Trees
■ Non leaf nodes form a multi-level sparse index on
the leaf nodes. For a non-leaf node with m pointers:
⚪ All the search-keys in the subtree to which P
1
points are less than K1
⚪ For 2 ≤ i ≤ n – 1, all the search-keys in the
subtree to which Pi points have values greater
than or equal to Ki–1 and less than Ki
⚪ All the search-keys in the subtree to which P
n
points have values greater than or equal to Kn–1
29
Example of a B+-tree
30
Example of B+-tree
32
Updates on B+-Trees: Insertion
1. Find the leaf node in which the search-key value would
appear
2. If the search-key value is already present in the leaf
node
1. Add record to the file
34
Result of splitting node containing Brighton and Downtown on inserting Clearview
Next step: insert entry with (Downtown, pointer-to-new-node) into parent
Updates on B+-Trees: Insertion (Cont.)
35
Updates on B+-Trees: Deletion
■ Find the record to be deleted, and remove it from the
main file and from the bucket (if present)
■ Remove (search-key value, pointer) from the leaf node
if there is no bucket or if the bucket has become empty
■ If the node has too few entries due to the removal, and
the entries in the node and a sibling fit into a single
node, then merge siblings:
⚪ Insert all the search-key values in the two nodes into
a single node (the one on the left), and delete the
other node.
⚪ Delete the pair (K , P ), where P is the pointer to
i–1 i i
the deleted node, from its parent, recursively using
the above procedure.
36
Updates on B+-Trees: Deletion
38
Examples of B+-Tree Deletion (Cont.)
40
B-Tree Index File Example
41
B-Tree Index Files (Cont.)
■ Advantages of B-Tree indices:
+
⚪ May use less tree nodes than a corresponding B -Tree.
44
Hashing
45
Static Hashing
■ A bucket is a unit of storage containing one or more
records (a bucket is typically a disk block).
■ In a hash file organization we obtain the bucket of
a record directly from its search-key value using a
hash function.
■ Hash function h is a function from the set of all
search-key values K to the set of all bucket
addresses B.
■ Hash function is used to locate records for access,
insertion as well as deletion.
■ Records with different search-key values may be
mapped to the same bucket; thus entire bucket has
to be searched sequentially to locate a record.
46
Example of Hash File Organization
Hash file organization of account file, using branch_name as key
(See figure in next slide.)
⚪ h(Round Hill) = 3
⚪ h(Brighton) = 3
47
Example of Hash File Organization
48
Hash Functions
■ Worst hash function maps all search-key values to the same
bucket; this makes access time proportional to the number of
search-key values in the file.
■ An ideal hash function is uniform, i.e., each bucket is assigned the
same number of search-key values from the set of all possible
values.
■ Ideal hash function is random, so each bucket will have the same
number of records assigned to it irrespective of the actual
distribution of search-key values in the file.
■ Typical hash functions perform computation on the internal binary
representation of the search-key.
⚪ For example, for a string search-key, the binary
representations of all the characters in the string could be
added and the sum modulo the number of buckets could be
returned.
49
Handling of Bucket Overflows
■ Bucket overflow can occur because of
⚪ Insufficient buckets
51
Deficiencies of Static Hashing
■ In static hashing, function h maps search-key values to a fixed
set of B of bucket addresses. Databases grow or shrink with
time.
⚪ If initial number of buckets is too small, and file grows,
performance will degrade due to too much overflows.
⚪ If space is allocated for anticipated growth, a significant
amount of space will be wasted initially (and buckets will be
underfull).
⚪ If database shrinks, again space will be wasted.
■ One solution: periodic re-organization of the file with a new hash
function
⚪ Expensive, disrupts normal operations
■ Better solution: allow the number of buckets to be modified
dynamically.
52
Dynamic Hashing
■ Good for database that grows and shrinks in size
■ Allows the hash function to be modified dynamically
■ Extendable hashing – one form of dynamic hashing
⚪ Hash function generates values over a large range —
typically b-bit integers, with b = 32.
⚪ At any time use only a prefix of the hash function to index
into a table of bucket addresses.
⚪ Let the length of the prefix be i bits, 0 ≤ i ≤ 32.
■ Bucket address table size = 2i. Initially i = 0
■ Value of i grows and shrinks as the size of the database
grows and shrinks.
⚪ Multiple entries in the bucket address table may point to a
bucket
⚪ Thus, actual number of buckets is < 2i
■ The number of buckets also changes dynamically due
to coalescing and splitting of buckets.
53
General Extendable Hash Structure
59
Example (Cont.)
Hash structure after insertion of Mianus record
60
Example (Cont.)
62
Extendable Hashing vs. Other Schemes
■ Benefits of extendable hashing:
⚪ Hash performance does not degrade with growth of file
⚪ Minimal space overhead
■ Disadvantages of extendable hashing
⚪ Extra level of indirection to find desired record
⚪ Bucket address table may itself become very big (larger
than memory)
■ Cannot allocate very large contiguous areas on disk
either
■ Solution: B+-tree structure to locate desired record in
bucket address table
⚪ Changing size of bucket address table is an expensive
operation
■ Linear hashing is an alternative mechanism
⚪ Allows incremental growth of its directory (equivalent to
bucket address table)
⚪ At the cost of more bucket overflows
Comparison of Ordered Indexing and Hashing