GNR-18 DBMS Unit-5
GNR-18 DBMS Unit-5
GNR-18 DBMS Unit-5
Failure Classification
Failure Types
• Transaction failure: Two types
– Logical errors: transaction cannot complete due to some
internal error condition.eg: bad input, data not found,
overflow or resource limit exceeded.
– System errors: the database system has entered
undesirable state(e.g., deadlock) as a result of which a
transaction cannotcontinue.
• System crash: a power failure or other hardware or software
failure causes the system to crash.(Bug in database software or
os software)
– Fail-stop assumption: non-volatile storage contents are
assumed to not be corrupted by system crash
• Disk failure: A disk block loses its content as a result of either head
crash or failure during data transfer operation. use DVDs or tapes
are used to recover from failure.
Storage Structure
• Volatile storage:
– does not survive system crashes
– examples: main memory, cache memory
• Nonvolatile storage:
– survives system crashes
– examples: disk, tape, flash memory,
non-volatile (battery backed up) RAM
– but may still fail, losing data
• Stable storage:
– maintaining multiple copies on distinct
nonvolatilemedia
1. Stable-Storage Implementation:
• To implement stable storage,needto replicate the needed
information in several nonvolatile storage media with independent
failure modesand to update the information in a controlledmanner
to ensure that failure duringdata transfer does not damage the
needed information.
• RAID- Redundant Array of Independent Disks
• Mirrored disk: two copies of each block on separate disks. but it
is notprotecting from disasters.
• copies can be at remote sites to protect against disasters such as
fire orflooding.
• Failure during data transfer can still result in inconsistent copies:
Blocktransfer between memory and disk storage can result in:
– Successful completion: Transferred information arrived
safely atits destination.
– Partial failure: A failure occurred in the midst of transfer and
destination block has incorrect information
– Total failure: A failure occurred early during the transfer a
thatdestination block was never updated
Execute output operation as follows (assuming two copies of each block):
1. Write the information onto the first physical block.
2. When the first write successfully completes, write the same
informationonto the second physical block.
3. The output is completed only after the second write successfully
completes.
If the system fail while blocks are being written, two copies of a block are
inconsistent with each other.
During recovery for each block of the system need to examine two
copies ofthe blocks
If Both blocks are same and no detectable error existsthen no action
If system detects error in one block then replace its contents with contents
of other block
If both blocks contain no detectable error but they differ in content then
system replace the content of first block with content of second.
Each transaction Tihas its private work-area in which local copies of all data
items accessed and updated by Ti are kept.
– Ti's local copy of a data item X is called xi.
Transferring data by two operations
1. read(X) assigns the value of data item X to the local variable xi.
a) if block BX on which X resides is not in main memory, it issues
input(BX).
b) It assigns to xi the value of X from the buffer block.
2. write(X) assigns the value of local variable xito data item X in the buffer
block.
Deferred-Modification Technique
If a transaction does not modify the databaseuntil it has committed.
overhead: Transactions need to make local copies of all updated data items;
Further, if a transaction reads a data item that it has updated, it
mustread the value from its local copy.
Immediate-Modification Technique
If database modifications occur while thetransaction is still active.
The recovery algorithm supports Immediate-modification technique. The
system to perform undo and redo operations as appropriate.
Transaction Commit: A transaction has committed when its commit log
record, which is thelast log record of the transaction, has been output to stable storage.
Using the Log to Redo and Undo Transactions
After a system crash which transactions need to be redone, and which need to
be undone to ensure atomicity.
Transaction Tineeds to be undone if the log contains the record
<Tistart>,but does not contain either the record <Ticommit>or the
record <Tiabort>.
Transaction Tineeds to be redone if the log contains the record
<Tistart>and either the record <Ticommit>or the record <Tiabort>.
To search the entire log to determine which transactions need to undo or redo.
Two difficulties with this approach:
1. search process is time-consuming.
2. unnecessarily redo transactions which have already output their
updates to the database. So that recovery to takes longer
A checkpoint is performed as follows:
1. Output all log records currently residing in main memory onto
stable storage.
2. Output all modified buffer blocks to the disk.
3. Write a log record < checkpoint L> onto stable storage where L is
a list of all transactions active at the time of checkpoint.
All updates are stopped while doing checkpointing.
After a system crash has occurred, the system examines the log to find the
last <checkpoint L>record by scanning log backward.
The redo or undo operations need to be applied only to transactions in Land
all transactions started execution after check point.
For all transactions Tkin T that have no <Tkcommit>record or <Tkabort>
record in the log, execute undo(Tk).
For all transactions Tkin T such that either the record <Tkcommit>or the
record <Tkabort>appears in the log, execute redo(Tk).
Example:
consider the set of transactions {T0, T1, . . . , T100}.
Suppose most recent checkpoint took place during the execution of
transaction T67 and T69, while T68 and all transactions with subscripts lower
than 67 completed before the checkpoint. Thus, only transactions T67, T69, . . .
, T100 need to be considered during the recovery scheme.
A fuzzy checkpoint is a checkpoint where transactions are allowed to perform
updates even while buffer blocks are being written out.
Recovery Algorithm
1. Transaction Rollback
consider transaction rollback during normal operation
1. The log is scanned backward, and for each log record of Tiof the form
<Ti ,Xj , V1, V2>that is found:
a) The value V1 is written to data item Xj, and
INDEXING
Ordered Indices:
An index for a file in a database system is similar to the index in the text
book. Index is much smaller than the text book.
Its help to retrieve information or recodes from files.
ex: retrieve a student record for given ID.
Two basic kinds of indices:
1. Ordered indices. Based on a sorted ordering of the values.
2. Hash indices. Based on a uniform distribution of values across a range of
buckets. The bucket to which a value is assigned is determined by a
function, called a hash function.
Each technique evaluated on the basis of following factors:
• Access types: Access types supported efficiently.
finding records with a specified attribute value.
finding records whose attribute values fall in a specified range.
• Access time: Time it takes to find a particular data item, or set of items.
• Insertion time: Time it takes to insert a new data item.
Time to find the correct place to insert the new data item + time ittakes
to update the index structure.
• Deletion time: Time it takes to delete a data item.
Time to find the item to be deleted + time it takes to update the index
structure.
Space overhead: sacrifices additional space for index to achieve
performance but that space is moderate.
• An attribute or set of attributes used to look up records in a file is called
a search key.
• A file may have more than one index. If several indices on a file, there
are several search keys.
• Example: search for a book by author, by subject, or by title.
Ordered Indices:
Primary index or cluster index: search key defines an order same to the
sequential order of the file.
search key of a primary index is usually but not necessarily the
primary key
Secondary index or uncluster index: search key specifies an order
different from the sequential order of the file.
• All files are ordered sequentially on some search key, with a clustering
index on the search key, are called index-sequential files.
• Dense non clustering index: the index must store a list of pointers to
allrecords with the same search-key value.
• Dense clustering index for the instructor file with the search key being
dept name.
Sparse Index:
An index entry appears for only some of the search-key values.
• It can be used only if the relation is stored in sorted order of the search
key.
search key value < desired search value. Ex find 22222 record.
10101<=22222
Multilevel Indices
• If primary index does not fit in memory, access becomes expensive. Even
main memory also required for number of other tasks.
• 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
inner index – the primary index file
• To locate a record, use binary search on the outer index to find the
record for the largest search-key value less than or equal to the one that
we desire. similarly inner index also.
• If even outer index is too large to fit in main memory, yet another level
of index can be created, and so on.
• Indices with two or more levels are called multilevel indices.
• Indices at all levels must be updated on insertion or deletion from the
file.
Index Update:
• every index must be updated whenever a record is either inserted into
or deleted from the file.
• Update = deletion of the old record, followed by an insertion of the new
value of the record.
updating single-level indices:
Insertion:
1. If the search-key value does not appear in the index, the system inserts
an index entry with the search-key value in the index at the appropriate
position.
2. Otherwise the following actions are taken:
a) If the index entry stores pointers to all records with the same search
keyvalue, the system adds a pointer to the new record in the index
entry.
b) Otherwise, the index entry stores a pointer to only the first record with
the search-key value then inserted record after the other records with
the same search-key values.
• 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.
Deletion:
Sparse Indices:
1. If the index does not contain an index entry with the search-key value of
the deleted record, nothing needs to be done to the index.
2. Otherwise the system takes the following actions:
a. If the deleted record was the only record with its search key, the
system replaces the corresponding index record with an index record
for the next search-key value. If the next search-key value already has
an index entry, the entry is deleted instead of being replaced.
b. Otherwise, if the index entry for the search-key value points to the
record being deleted, the system updates the index entry to point
to the next record with the same search-key value.
Secondary Indices:
• A secondary index on a candidate key similar to dense index but
secondary indices may have a different structure from sequential file.
• If the search key of a secondary index is not a candidate key, it is not
enough to point to first record with each search-key value because
remaining records with the same search-key value could be anywhere in
the file.
• secondary index must contain pointers to all the records.
• Use an extra level of indirection to implement secondary indices on
search keys that are not candidate keys.
• Scan the file sequentially in secondary-key order, the reading of each
record is likely to require the reading of a new block from disk, which is
very slow.
• impose a significant overhead on modification of the database.
Uses an extra level of indirection on the instructor file, on the search key
salary.
B+ Trees: A DYNAMIC INDEX STRUCTURE
It is a dynamic index structure because tree structure grows and shrinks
dynamically.
It is a balanced tree in which the internal nodes contain index entries
(direct the search) and the leaf nodes contain the data entries (sequence
set).
To retrieve all leaf pages efficiently, we have to link them using page
pointers. By organizing them into a doubly linked list, we can easily
traverse the sequence of leaf pages (sometimes called the sequence set) in
either direction.
Properties of a B+ tree:
Operations (insert, delete) on the tree keep it balanced.
A minimum occupancy of 50 percent is guaranteed for each node except
the root.
Searching for a record requires just a traversal from the root to the
appropriate leaf.
every node contains m entries, where d ≤ m ≤ 2d. The value d is a
parameter of the B+ tree, called the order of the tree, and is a measure of
the capacity of a tree node.
The root node is the only exception to this requirement on the number of
entries; for the root it is simply required that 1 ≤ m ≤ 2d
B+ trees maintain 67 percent space occupancy.
Efficient Insertion and deletion operations.
FORMAT OF A NODE:
• n − 1 search-key values K1, K2, . . . , Kn−1, and n pointers P1, P2, . . . , Pn.
• Search-key values within a node are kept in sorted order, if i< j, then Ki <
Kj
K1 < K2 < K3 < . . . <Kn–1
SEARCH Operation: Finds the leaf node in which a given data entry belongs.
Consider below figure B+ tree is of order d=2. Each node contains between 2
and 4 entries.
Data entries are denoted as K*.
Example:
To search for entry 5*, we follow the left-most child pointer, since 5 < 13.
To find 24*, we follow the fourth child pointer, since 24 ≤ 24 <30.
INSERT Operation:
Insertion takes an entry, finds the leaf node where it belongs, and inserts
it there.
A node is full and it must be split. When the node is split, an entry
pointing to the node created by the split must be inserted into its
parent. This entry is pointed to by the pointer variable newchildentry.
If the root is split, a new root node is created and the height of the tree
increases by one.
Example:
leaf-level splits: when leaf node is split copied up the data entry to the parent
node.
Index-level splits: when non-leaf node is split pushed up the index entry to the
parent node.
Redistribution: It redistribute entries of a node N with a sibling before splitting
the node. Redistribution used when there is space in sibling node for insertion
operation.