Unit 3
Unit 3
Magnetic Disks
Disk Subsystem
● Checksum
● Remapping of bad sectors
● Access time is the time from when a read or write request is issued to when data transfer
begins.
● The time for repositioning the arm is called the seek time, and it increases with the distance
that the arm must move. Typical seek times range from 2 to 30 milliseconds,
● average seek time is the average of the seek times, measured over a sequence of (uniformly
distributed) random requests.
● the time spent waiting for the sector to be accessed to appear under the head is called the
rotational latency time.
● The average latency time of the disk is one-half the time for a full rotation of the disk.
● The data-transfer rate is the rate at which data can be retrieved from or stored to the disk.
● mean time to failure (MTTF) is a measure of the reliability of the disk. It is the amount of time
that, on average, we can expect the system to run continuously without any failure.
● Scheduling.
○ Disk-arm–scheduling algorithms
○ Elevator algorithm
● File organization
● Nonvolatile write buffers
● Log disk
RAID
● Mean time to repair, which is the time it takes (on an average) to replace a
failed disk and to restore the data on it.
RAID Levels
● The factors to be taken into account when choosing a RAID level are
○ Monetary cost of extra disk storage requirements
○ Performance requirements in terms of number of I/O operations
○ Performance when a disk has failed
○ Performance during rebuild (that is, while the data in a failed disk is being rebuilt on a
new disk)
File Organization
● There are different ways of storing data in the database. Storing data in files is one of them. A user can
store the data in files in an organized manner. These files are organized logically as a sequence of records
and reside permanently on disks.
● Each file is divided into fixed-length storage units known as Blocks. These blocks are the units of storage
allocation as well as data transfer. Although the default block size in the database is 4 to 8 kilobytes, many
databases allow specifying the size at the time of creating the database instance.
● Usually, the record size is smaller than the block size. But, for large data items such as images, the size
can vary.
● For accessing the data quickly, it is required that one complete record should reside in one block only. It
should not be partially divided between one or two blocks.
● In RDBMS, the size of tuples varies in different relations. Thus, we need to structure our files in multiple
lengths for implementing the records. In file organization, there are two possible ways of representing the
records:
○ Fixed-length records
○ Variable-length records
Fixed-Length Records
● Fixed-length records means setting a length and storing the records
into the file. If the record size exceeds the fixed size, it gets divided
into more than one block.
● Consider the following example
● At the beginning of the file, a certain number of bytes is allocated as a file header. The file
header stores the address of the first record whose contents are deleted. The first record to
store the address of the second available record, and so on.
● The deleted records thus form a linked list, which is often referred to as a free list.
● On insertion of a new record, the record pointed to by the header is used. The header pointer
is changed to point to the next available record. If no space is available, the new record is
added to the end of the file. This process is similar to pointers.
Variable-Length Records
● Consider the following example:
● Variable-length records are the records that vary in size. It requires the creation of multiple
blocks of multiple sizes to store them. These variable-length records are kept in the following
ways in the database system:
○ Storage of multiple record types in a file
○ Record types that allow variable lengths for one or more fields
○ Record types that allow repeating fields
Byte-String Representation
● A simple method for implementing variable-length records is to attach a special
endof-record (⊥) symbol to the end of each record. We can then store each record as
a string of consecutive bytes.
slotted-page structure
● It is an alternative form of byte-string representation and is commonly used
for organizing records within a single block.
● The actual records are allocated contiguously in the block, starting from the end of the block.
The free space in the block is contiguous, between the final entry in the header array, and the
first record. If a record is inserted, space is allocated for it at the end of free space, and an
entry containing its size and location is added to the header.
● If a record is deleted, the space that it occupies is freed, and its entry is set to deleted (its size
is set to −1, for example). Further, the records in the block before the deleted record are
moved, so that the free space created by the deletion gets occupied, and all free space is
again between the final entry in the header array and the first record. The end-of-free-space
pointer in the header is appropriately updated as well.
● Records can be grown or shrunk by similar techniques, as long as there is space in the block.
The cost of moving the records is not too high, since the size of a block is limited: A typical
value is 4 kilobytes.
● The slotted-page structure requires that there be no pointers that point directly to records.
Instead, pointers must point to the entry in the header that contains the actual location of the
record. This level of indirection allows records to be moved to prevent fragmentation of space
inside a block, while supporting indirect pointers to the record.
Fixed-Length Representation
Reserved space.
● If there is a maximum record length that is never exceeded, we can use fixed-
length records of that length. Unused space (for records shorter than the
maximum space) is filled with a special null, or end-of-record, symbol.
Figure shows how the file would be represented if we allowed a maximum
of three accounts per branch. A record in this file is of the account-list
type, but with the array containing exactly
three elements. Those branches with fewer than three accounts (for
example, Round Hill) have records with null fields. We use the symbol ⊥
to represent this situation in Figure 11.12. In practice, a particular value
that can never represent real data is used (for example, an account
number that is blank, or a name beginning with “*”).
The reserved-space method is useful when most records have a length
close to the maximum. Otherwise, a significant amount of space may be
wasted. In our bank example, some branches may have many more
accounts than others.
List representation
● We can represent variable-length records by lists of fixed length records, chained together by pointers.
● A disadvantage to the structure is that we waste space in all records except the first in a chain. The first
record needs to have the branch-name value, but subsequent records do not. Nevertheless, we need to
include a field for branch-name in all records, lest the records not be of fixed length. This wasted space is
significant, since we expect, in practice, that each branch has a large number of accounts.
● To deal with this problem, we allow two kinds of blocks in our file:
1. Anchor block, which contains the first record of a chain
2. Overflow block, which contains records other than those that are the first record of a chain
● Thus, all records within a block have the same length, even though not all records in the file have the same
length.
● Hash File Organization uses the computation of hash function on some fields
of the records. The hash function's output determines the location of disk
block where the records are to be placed.
● When a record has to be received using the hash key columns, then the address is
generated, and the whole record is retrieved using that address. In the same way,
when a new record has to be inserted, then the address is generated using the hash
key and record is directly inserted. The same process is applied in the case of delete
and update.
● In this method, there is no effort for searching and sorting the entire file. In this
method, each record will be stored randomly in the memory.
● In this method, we can directly insert, update or delete any record. Data is sorted based on the
key with which searching is done. Cluster key is a type of key with which joining of the table is
performed.
Excellence and Service
CHRIST
Deemed to be University
1. Indexed Clusters:
○ In indexed cluster, records are grouped based on the cluster key and stored together.
The above EMPLOYEE and DEPARTMENT relationship is an example of an indexed
cluster. Here, all the records are grouped based on the cluster key- DEP_ID and all the
records are grouped.
2. Hash Clusters:
○ It is similar to the indexed cluster. In hash cluster, instead of storing the records based on
the cluster key, we generate the value of the hash key for the cluster key and store the
records with the same hash key value.
● If any record has to be retrieved based on its index value, then the address of
the data block is fetched and the record is retrieved from the memory.
Excellence and Service
CHRIST
Deemed to be University
● Pros of ISAM:
○ In this method, each record has the address of its data block, searching a record in a
huge database is quick and easy.
○ This method supports range retrieval and partial retrieval of records. Since the index is
based on the primary key values, we can retrieve the data for the given range of value. In
the same way, the partial value can also be easily searched, i.e., the student name
starting with 'JA' can be easily searched.
● Cons of ISAM
○ This method requires extra space in the disk to store the index value.
○ When the new records are inserted, then these files have to be reconstructed to maintain
the sequence.
○ When the record is deleted, then the space used by it needs to be released. Otherwise,
the performance of the database will slow down.
B+ File Organization
● B+ tree file organization is the advanced method of an indexed sequential access method. It uses a tree-
like structure to store records in File.
● It uses the same concept of key-index where the primary key is used to sort the records. For each primary
key, the value of the index is generated and mapped with the record.
● The B+ tree is similar to a binary search tree (BST), but it can have more than two children. In this method,
all the records are stored only at the leaf node. Intermediate nodes act as a pointer to the leaf nodes. They
do not contain any records.
• There is one root node of the tree, i.e., 25.
• There is an intermediary layer with nodes.
They do not store the actual record. They
have only pointers to the leaf node.
• The nodes to the left of the root node
contain the prior value of the root and
nodes to the right contain next value of the
root, i.e., 15 and 30 respectively.
• There is only one leaf node which has only
values, i.e., 10, 12, 17, 20, 24, 27 and 29.
• Searching for any record is easier as all the
leaf nodes are balanced.
• In this method, searching any record can be
traversed through the single path and
accessed easily.
● The first column of the database is the search key that contains a copy of the primary
key or candidate key of the table. The values of the primary key are stored in sorted
order so that the corresponding data can be accessed easily.
● The second column of the database is the data reference. It contains a set of pointers
holding the address of the disk block where the value of the particular key can be
found.
Excellence and Service
CHRIST
Deemed to be University
Indexing Methods
Ordered Indices
● The indices are usually sorted to make searching faster. The indices which
are sorted are known as ordered indices.
● Example: Suppose we have an employee table with thousands of record and
each of which is 10 bytes long. If their IDs start with 1, 2, 3....and so on and
we have to search student with ID-543.
● In the case of a database with no index, we have to search the disk block
from starting till it reaches 543. The DBMS will read the record after reading
543*10=5430 bytes.
● In the case of an index, we will search using indexes and the DBMS will read
the record after reading 542*2= 1084 bytes which are very less compared to
the previous case.
Primary Index
● If the index is created on the basis of the primary key of the table, then it is
known as primary indexing. These primary keys are unique to each record
and contain 1:1 relation between the records.
● As primary keys are stored in sorted order, the performance of the searching
operation is quite efficient.
● The primary index can be classified into two types:
○ Dense index and
○ Sparse index.
Dense index
● The dense index contains an index record for every search key value in the
data file. It makes searching faster.
● In this, the number of records in the index table is same as the number of
records in the main table.
● It needs more space to store index record itself. The index records have the
search key and a pointer to the actual record on the disk.
Sparse index
● Here, the index record appears only for a few items in the data file. Each item
points to a block.
● sparse Index stores index records for only some search-key values. It needs
less space, less maintenance overhead for insertion, and deletions but It is
slower compared to the dense Index for locating records.
Clustering Index
Secondary Index
● In the sparse indexing, as the size of the table grows, the size of mapping
also grows. These mappings are usually kept in the primary memory so that
address fetch should be faster. Then the secondary memory searches the
actual data based on the address got from mapping. If the mapping size
grows then fetching the address itself becomes slower. In this case, the
sparse index will not be efficient. To overcome this problem, secondary
indexing is introduced.
● In secondary indexing, to reduce the size of mapping, another level of
indexing is introduced. In this method, the huge range for the columns is
selected initially so that the mapping size of the first level becomes small.
Then each range is further divided into smaller ranges. The mapping of the
first level is stored in the primary memory, so that address fetch is faster. The
mapping of the second level and actual data are stored in the secondary
memory (hard disk).
Excellence and Service
CHRIST
Deemed to be University
Multi-level Indexing
● Btree
● B+ tree
Btree
● B Tree is a specialized m-way tree that can be widely used for disk access. A
B-Tree of order m can have at most m-1 keys and m children. One of the
main reason of using B tree is its capability to store large number of keys in a
single node and large key values by keeping the height of the tree relatively
small.
● A B tree of order m contains all the properties of an M way tree. In addition, it
contains the following properties.
1. Every node in a B-Tree contains at most m children.
2. Every node in a B-Tree except the root node and the leaf node contain at least m/2
children.
3. The root nodes must have at least 2 nodes.
4. All leaf nodes must be at the same level.
● It is not necessary that, all the nodes contain the same number of children
but, each node must have m/2 number of nodes.
● A B tree of order 4 is shown in the following image.
● Deletion
● Deletion is also performed at the leaf nodes. The node which is to be deleted can either be a leaf node or
an internal node. Following algorithm needs to be followed in order to delete a node from a B tree.
● Locate the leaf node.
● If there are more than m/2 keys in the leaf node then delete the desired key from the node.
● If the leaf node doesn't contain m/2 keys then complete the keys by taking the element from eight or left
sibling.
○ If the left sibling contains more than m/2 elements then push its largest element up to its parent and move the intervening element
down to the node where the key is deleted.
○ If the right sibling contains more than m/2 elements then push its smallest element up to the parent and move intervening element
down to the node where the key is deleted.
● If neither of the sibling contain more than m/2 elements then create a new leaf node by joining two leaf
nodes and the intervening element of the parent node.
● If parent is left with less than m/2 nodes then, apply the above process on the parent too.
● If the the node which is to be deleted is an internal node, then replace the node with its in-order successor
or predecessor. Since, successor or predecessor will always be on the leaf node hence, the process will be
similar as the node is being deleted from the leaf node.
● Example 1
● Delete the node 53 from the B Tree of order 5 shown in the following figure.
● Now, 57 is the only element which is left in the node, the minimum number of elements that must be
present in a B tree of order 5, is 2. it is less than that, the elements in its left and right sub-tree are also not
sufficient therefore, merge it with the left sibling and intervening element of parent i.e. 49.
Application of B tree
● B tree is used to index the data and provides fast access to the actual data
stored on the disks since, the access to value stored in a large database that
is stored on a disk is a very time consuming process.
● Searching an un-indexed and unsorted database containing n key values
needs O(n) running time in worst case. However, if we use B Tree to index
this database, it will be searched in O(log n) time in worst case.
Excellence and Service
CHRIST
Deemed to be University
B+ tree
● The B+ tree is a balanced binary search tree. It follows a multi-level index format.
● In the B+ tree, leaf nodes denote actual data pointers. B+ tree ensures that all leaf
nodes remain at the same height.
● In the B+ tree, the leaf nodes are linked using a link list. Therefore, a B+ tree can
support random access as well as sequential access.
Structure of B+ Tree
○ In the B+ tree, every leaf node is at equal distance from the root node. The B+
tree is of the order n where n is fixed for every B+ tree.
○ It contains an internal node and leaf node.
● Internal node
○ An internal node of the B+ tree can contain at least n/2 record pointers except
the root node.
○ At most, an internal node of the tree contains n pointers.
● Leaf node
○ The leaf node of the B+ tree can contain at least n/2 record pointers and n/2 key
values.
○ At most, a leaf node contains n record pointer and n key values.
○ Every leaf node of the B+ tree contains one block pointer P to point to next leaf
node.
B+ Tree Insertion
● The 3rd leaf node has the values (50, 55, 60, 65, 70) and its current root node
is 50. We will split the leaf node of the tree in the middle so that its balance is
not altered. So we can group (50, 55) and (60, 65, 70) into 2 leaf nodes.
● If these two has to be leaf nodes, the intermediate node cannot branch from
50. It should have 60 added to it, and then we can have pointers to a new leaf
node.
B+ Tree Deletion
● Suppose we want to delete 60 from the above example. In this case, we have
to remove 60 from the intermediate node as well as from the 4th leaf node
too. If we remove it from the intermediate node, then the tree will not satisfy
the rule of the B+ tree. So we need to modify it to have a balanced tree.
● After deleting node 60 from above B+ tree and re-arranging the nodes, it will
show as follows:
Hashing
● The above diagram shows data block addresses same as primary key value.
This hash function can also be a simple mathematical function like
exponential, mod, cos, sin, etc. Suppose we have mod (5) hash function to
determine the address of the data block. In this case, it applies mod (5) hash
function on the primary keys and generates 3, 3, 1, 4 and 2 respectively, and
records are stored in those data block addresses.
Types of Hashing
● Static hashing
● Dynamic hashing
Open Hashing
Close Hashing
● When buckets are full, then a new data bucket is allocated for the same hash
result and is linked after the previous one. This mechanism is known
as Overflow chaining.
● For example: Suppose R3 is a new address which needs to be inserted into
the table, the hash function generates address as 110 for it. But this bucket is
full to store the new data. In this case, a new bucket is inserted at the end of
110 buckets and is linked to it.
●
Dynamic Hashing
● The dynamic hashing method is used to overcome the problems of static
hashing like bucket overflow.
● In this method, data buckets grow or shrink as the records increases or
decreases. This method is also known as Extendable hashing method.
● This method makes hashing dynamic, i.e., it allows insertion or deletion
without resulting in poor performance.
● Firstly, you have to follow the same procedure for retrieval, ending up in some
bucket.
● If there is still space in that bucket, then place the record in it.
● If the bucket is full, then we will split the bucket and redistribute the records.
● For example: Consider the following grouping of keys into buckets, depending
on the prefix of their hash address:
The last two bits of 2
and 4 are 00. So it will
go into bucket B0. The
last two bits of 5 and 6
are 01, so it will go into
bucket B1. The last
two bits of 1 and 3 are
10, so it will go into
bucket B2. The last
two bits of 7 are 11, so
it will go into B3.
[
1. The first step is to transform the query into a standard form.
2. A query is translated into SQL and into a relational algebraic expression. During this process, Parser
checks the syntax and verifies the relations and the attributes which are used in the query.
Example:
SELECT Ename FROM Employee
WHERE Salary > 5000;
Translated into Relational Algebra Expression
σ Salary > 5000 (π Ename (Employee))
OR
π Ename (σ Salary > 5000 (Employee))
3. The second step is Query Optimizer. In this, it transforms the query into equivalent expressions that are
more efficient to execute.
4. The third step is Query evaluation. It executes the above query execution plan and returns the result.
Thank you