Data Base Management Systems
LAKIREDDY BALI REDDY COLLEGE OF ENGINEERING (AUTONOMOUS) Accredited by NAAC & NBA (Under Tier - I) ISO 9001:2015 Certified Institution Approved by AICTE, New Delhi. and Affiliated to JNTUK, Kakinada L.B. REDDY NAGAR, MYLAVARAM, KRISHNA DIST., A.P.-521 230. DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND DATA SCIENCE |
UNIT V: Physical Database Design
Program & Semester: B.Tech & III SEM
Academic Year: 2023 - 24
Overview of Physical Storage
Cache
Main memory.
Flash memory.
Flash memory is also widely used for storing data in “USB keys,” which can be plugged into the Universal Serial Bus (USB) slots of computing devices.
Magnetic-disk storage
Optical storage
Tape storage
Tape storage is used primarily for backup and archival data. Although magnetic tape is cheaper than disks, access to data is much slower, because the tape must be accessed sequentially from the beginning. For this reason, tape storage is referred to as sequential-access storage.
Magnetic Disk
Magnetic disks provide the bulk of secondary storage for modern computer
systems. A very large database may require hundreds of disks. In recent years, flash-memory storage sizes have grown rapidly, and flash storage is increasingly becoming a competitor to magnetic disk storage for several applications.
Physical Characteristics of Disks
Each disk platter has a flat, circular shape. Its two surfaces are covered with a magnetic material, and information is recorded on the surfaces.
There is a read–write head positioned just above the surface of the platter. The disk surface is logically divided into tracks, which are subdivided into sectors. A sector is the smallest unit of information that can be read from or written to the disk.
The read–write head stores information on a sector magnetically.
A disk typically contains many platters, and the read–write heads of all the tracks are mounted on a single assembly called a disk arm, and move together.
RAID (Redundant Arrays of Independent Disks)
RAID-0 (Striping)
2
Evaluation
Reliability: 0
Capacity: N*B
RAID-1 (Mirroring)
Evaluation:
Assume a RAID system with mirroring level 2.
Reliability: 1 to N/2
Capacity: N*B/2
RAID 2(Error- Correcting Code)
Evaluation:
RAID 3 (Byte-level striping )
Evaluation:
RAID 4 (Block-Level Striping with Dedicated Parity)
Instead of duplicating data, this adopts a parity-based approach.
Parity is calculated using a simple XOR function. If the data bits are 0,0,0,1 the parity bit is XOR(0,0,0,1) = 1. If the data bits are 0,1,1,0 the parity bit is XOR(0,1,1,0) = 0. A simple approach is that even number of ones results in parity 0, and an odd number of ones results in parity 1.
Evaluation:
RAID-5 (Block-Level Striping with Distributed Parity)
This is a slight modification of the RAID-4 system where the only difference is that the parity rotates among the drives.
This was introduced to make the random write performance better.
Evaluation:
Reliability: 1
Capacity: (N-1)*B
RAID-6 (Block-Level Striping with Dual Distributed Parity)
File Organization
A file is organized logically as a sequence of records. These records are mapped onto disk blocks.
Fixed-Length Records
Variable-Length Records
RECORD FORMATS
Fixed-Length Records
Variable-Length Records
File Organization
A database consist of a huge amount of data. The data is grouped within a table in RDBMS, and each table have related records. A user can see that the data is stored in form of tables, but in actual this huge amount of data is stored in physical memory in form of files.
File : A file is named collection of related information that is recorded on secondary storage such as magnetic disks, magnetic tables and optical disks.
File Organization refers to the logical relationships among various records that constitute the file, particularly with respect to the means of identification and access to any specific record.
Types of File Organizations
Sequential File Organization
The easiest method for file Organization is Sequential method. In this method the file are stored one after another in a sequential manner. There are two ways to implement this method:
Pile Method
This method is quite simple, in which we store the records in a sequence i.e one after other in the order in which they are inserted into the tables
Advantages
Disadvantages
Heap File Organization
If a new block is inserted
Advantages
Disadvantages
Hash File Organization
Data bucket
Data buckets are the memory locations where the records are stored. These buckets are also considered as Unit Of Storage.
Hash Function
Hashing is further divided into two sub categories :
Static Hashing
In static hashing, when a search-key value is provided, the hash function always computes the same address.
For example, if we want to generate address for STUDENT_ID = 76 using mod (5) hash function, it always result in the same bucket address 1. There will not be any changes to the bucket address here.
Hence number of data buckets in the memory for this static hashing remains constant throughout.
Insertion
When a new record is inserted into the table, The hash function h generate a bucket address for the new record based on its hash key K.
Bucket address = h(K)
Searching
When a record needs to be searched, The same hash function is used to retrieve the bucket address for the record.
If we want to retrieve whole record for ID 76, and if the hash function is mod (5) on that ID, the bucket address generated would be 1. Then we will directly got to address 1 and retrieve the whole record for ID 76. Here ID acts as a hash key.
Deletion
If we want to delete a record, Using the hash function we will first fetch the record which is supposed to be deleted. Then we will remove the records for that address in memory.
Updation
The data record that needs to be updated is first searched using hash function, and then the data record is updated.
When, we want to insert some new records into the file, but the data bucket address generated by the hash function is not empty or the data already exists in that address. This becomes a critical situation to handle. This situation in the static hashing is called bucket overflow.
Open Hashing
Open hashing method, next available data block is used to enter the new record, instead of overwriting older one. This method is also called linear probing.
Closed hashing
In Closed hashing method, a new data bucket is allocated with same address and is linked it after the full data bucket. This method is also known as overflow chaining.
Quadratic probing
Quadratic probing is an open-addressing scheme where we look for i2‘th slot in i’th iteration if the given hash value x collides in the hash table.
How Quadratic Probing is done?
Let hash(x) be the slot index computed using the hash function.
If the slot hash(x) % S is full, then we try (hash(x) + 1*1) % S.
If (hash(x) + 1*1) % S is also full, then we try (hash(x) + 2*2) % S.
If (hash(x) + 2*2) % S is also full, then we try (hash(x) + 3*3) % S.
This process is repeated for all the values of i until an empty slot is found
Double hashing
Double hashing is a collision resolving technique in Open Addressed Hash tables. Double hashing uses the idea of applying a second hash function to key when a collision occurs.
Double hashing can be done using :
(hash1(key) + i * hash2(key)) % TABLE_SIZE
Here hash1() and hash2() are hash functions and TABLE_SIZE is size of hash table.
Dynamic Hashing
In Dynamic hashing, data buckets grows or shrinks (added or removed dynamically) as the records increases or decreases. Dynamic hashing is also known as extended hashing.
Extendible Hashing
Extendible Hashing is a dynamic hashing method where in directories, and buckets are used to hash data. It is an aggressively flexible method in which the hash function also experiences dynamic changes.
Directories
These containers store pointers to buckets. Each directory is given a unique id which may change each time when expansion takes place. The hash function returns this directory id which is used to navigate to the appropriate bucket. Number of Directories = 2^Global Depth.
Buckets
They store the hashed keys. Directories point to buckets. A bucket may contain more than one pointers to it if its local depth is less than the global depth.
Global Depth
It is associated with the Directories. They denote the number of bits which are used by the hash function to categorize the keys. Global Depth = Number of bits in directory id.
Local Depth
Bucket Splitting:
When the number of elements in a bucket exceeds a particular size, then the bucket is split into two parts.
Directory Expansion
Directory Expansion Takes place when a bucket overflows. Directory Expansion is performed when the local depth of the overflowing bucket is equal to the global depth.
Example of hashing the following elements: 16,4,6,22,24,10,31,7,9,20,26.
Bucket Size: 3 (Assume)
Hash Function: Suppose the global depth is X. Then the Hash Function returns X LSBs.
16- 10000 , 4- 00100 , 6- 00110 , 22- 10110 , 24- 11000 , 10- 01010 ,
31- 11111, 7- 00111, 9- 01001 , 20- 10100 , 26- 11010
The drawback of static hashing is that that it does not expand or shrink dynamically as the size of the database grows or shrinks
Dynamic Hashing
In Dynamic hashing, data buckets grows or shrinks as the records increases or decreases. Dynamic hashing is also known as extended hashing.
B+ Tree File Organization
B+ Tree, as the name suggests, It uses a tree like structure to store records in File. It uses the concept of Key indexing where the primary key is used to sort the records.
B+ Tree is very much like binary search tree, with the only difference that instead of just two children, it can have more than two.
All the information is stored in leaf node and the intermediate nodes acts as pointer to the leaf nodes.
The information in leaf nodes always remain a sorted sequential linked list.
Advantages
Tree traversal is easier and faster.
Searching becomes easy as all records are stored only in leaf nodes and are sorted sequential linked list.
There is no restriction on B+ tree size. It may grows/shrink as the size of data increases/decreases.
Disadvantages
Inefficient for static tables.
Cluster File Organization
In cluster file organization, two or more related tables/records are stored withing same file known as clusters.
These files will have two or more tables in the same data block and the key attributes which are used to map these table together are stored only once.
Thus it lowers the cost of searching and retrieving various records in different files as they are now combined and kept in a single cluster.
For example we have two tables or relation Employee and Department. These table are related to each other
These table are allowed to combine using a join operation and can be seen in a cluster file.
If we must insert, update or delete any record we can directly do so. Data is sorted based on the primary key or the key with which searching is done. Cluster key is the key with which joining of the table is performed.
There are two ways to implement this method:
Indexed Clusters:
In Indexed clustering the records are group based on the cluster key and stored together. The above example of the Employee and Department relationship is an example of Indexed Cluster where the records are based on the Department ID.
Hash Clusters:
This is very much similar to indexed cluster with only difference that instead of storing the records based on cluster key, we generate hash key value and store the records with same hash key value.
Advantages of Cluster file organization
Disadvantages of Cluster file organization
| Sequential | Heap/ Direct | Hash | ISAM | B+ tree | Cluster |
Method of storing | Stored as they come or sorted as they come | Stored at the end of the file. But the address in the memory is random. | Stored at the hash address generated | Address index is appended to the record | Stored in a tree like structure | Frequently joined tables are clubbed into one file based on cluster key |
Types | Pile file and sorted file Method | | Static and dynamic hashing | Dense, Sparse, multilevel indexing | | Indexed and Hash |
Design | Simple Design | Simplest | Medium | Complex | Complex | Simple |
Storage Cost | Cheap (magnetic tapes) | Cheap | Medium | Costlier | Costlier | Medium |
Comparison Of File Organizations:
Indexing
Indexes are created using a few database columns.
The first column is the Search key that contains a copy of the primary key or candidate key of the table.
The second column is the Data Reference or Pointer which contains a set of pointers holding the address of the disk block where that particular key value can be found.
Classification Of Index
Dense Index
�
Sparse Index
The index record appears only for a few items in the data file. Each item points to a block as shown.
Types of Indexing
1. Single-level Indexing
2. Multilevel Indexing
Primary indexing
It is defined mainly on the primary key of the data-file, in which the data-file is already ordered based on the primary key.
Clustered index:
Clustered index is created only when both the following conditions satisfy
Secondary Index or Non-clustered Index:
Multilevel Indexing:
With the growth of the size of the database, indices also grow. As the index is stored in the main memory, a single-level index might become too large a size to store with multiple disk accesses. The multilevel indexing segregates the main block into various smaller blocks so that the same can stored in a single block. The outer blocks are divided into inner blocks which in turn are pointed to the data blocks. This can be easily stored in the main memory with fewer overheads.
B Tree
Properties of B-Tree
A B tree of order 4 is shown below
While performing some operations on B Tree, any property of B Tree may violate such as number of minimum children a node can have. To maintain the properties of B Tree, the tree may split or join.
Operations
Searching
Insertion
Deletion
Searching
Searching in B Trees is like that in Binary search tree.
For example, if we search for an item 49 in the following B Tree. The process will something like following :
Compare item 49 with root node 78. since 49 < 78 hence, move to its left sub-tree.
Since, 40<49<56, traverse right sub-tree of 40.
49>45, move to right. Compare 49.
match found, return.
Searching in a B tree depends upon the height of the tree. The search algorithm takes O(log n) time to search any element in a B tree.
Insertion
Insertions are done at the leaf node level.
The following algorithm needs to be followed in order to insert an item into B Tree.
DELETION
After Deleting 6 the tree loos like
After deleting 13 the tree looks like
After deleting 7. Two leaf nodes are added to form the new node
For a B-Tree of order M
Run time is:
The drawback of B-tree is that it stores the data pointer (a pointer to the disk file block containing the key value), corresponding to a particular key value, along with that key value in the node of a B-tree.
B+ tree
Internal Node structure
External Node (Leaf Node)
Using the Pnext pointer it is viable to traverse all the leaf nodes, just like a linked list, thereby achieving ordered access to the records stored in the disk.
A B+ tree with ‘l’ levels can store more entries in its internal nodes compared to a B-tree having the same ‘l’ levels. This accentuates the significant improvement made to the search time for any given key.
Operations on B+ Tree
Searching
Insertion
insert 28 into the below 5 ordered tree.
INSERTION
After inserting 28 the tree looks as below.
insert 70 into the below 5 ordered tree.
Deletion
Example
Delete 60 from the below tree
The total time complexity of the B+ tree search operation is O(t log tn). Where O(t) is time complexity for each linear search.
The time complexity of the insertion algorithm is O(t log tn).
Advantages of B+ Trees:
Disadvantages of B+ Trees: