Storage and File Structure
Storage and File Structure
BITS Pilani
Hyderabad Campus
Pilani Campus
Storage and File Structure
BITS Pilani,Pilani
Hyderabad Campus
Campus
Storage and File Structure
BITS Pilani,Pilani
Hyderabad Campus
Campus
Classification of Physical Storage Media
BITS Pilani,Pilani
Hyderabad Campus
Campus
Storage Hierarchy
BITS Pilani,Pilani
Hyderabad Campus
Campus
Storage Hierarchy (Cont.)
BITS Pilani,Pilani
Hyderabad Campus
Campus
Magnetic Hard Disk Mechanism
NOTE: Diagram is schematic, and simplifies the structure of actual disk drives
BITS Pilani,Pilani
Hyderabad Campus
Campus
Magnetic Disks
Read-write head
– Positioned very close to the platter surface (almost touching it)
– Reads or writes magnetically encoded information.
Surface of platter divided into circular tracks
– Over 50K-100K tracks per platter on typical hard disks
Each track is divided into sectors.
To read/write a sector
– disk arm swings to position head on right track
– platter spins continually; data is read/written as sector passes under head
Cylinder i consists of ith track of all the platters
BITS Pilani,Pilani
Hyderabad Campus
Campus
Disk Subsystem
BITS Pilani,Pilani
Hyderabad Campus
Campus
Performance Measures of Disks
Access time – the time it takes from when a read or write request is issued to when data
transfer begins. Consists of:
– Seek time – time it takes to reposition the arm over the correct track.
• 4 to 10 milliseconds on typical disks
– Rotational latency – time it takes for the sector to be accessed to appear under the head.
• 4 to 11 milliseconds on typical disks (5400 to 15000 r.p.m.)
Data-transfer rate – the rate at which data can be retrieved from or stored to the disk.
– 25 to 100 MB per second max rate, lower for inner tracks
– Multiple disks may share a controller, so rate that controller can handle is also important
• E.g. SATA: 150 MB/sec, SATA-II 3Gb (300 MB/sec)
• Ultra 320 SCSI: 320 MB/s, SAS (3 to 6 Gb/sec)
• Fiber Channel (FC2Gb or 4Gb): 256 to 512 MB/s
BITS Pilani,Pilani
Hyderabad Campus
Campus
Performance Measures (Cont.)
Mean time to failure (MTTF) – the average time the disk is expected to run continuously without
any failure.
– Typically 3 to 5 years
– Probability of failure of new disks is quite low, corresponding to a “theoretical MTTF” of
500,000 to 1,200,000 hours for a new disk
– E.g., an MTTF of 1,200,000 hours for a new disk means that given 1000 relatively new
disks, on an average one will fail every 1200 hours
– MTTF decreases as disk ages
BITS Pilani,Pilani
Hyderabad Campus
Campus
RAID
BITS Pilani,Pilani
Hyderabad Campus
Campus
Improvement of Reliability via Redundancy
E.g. a system with 100 disks, each with MTTF of 100,000 hours (approx. 11 years), will have a system
MTTF of 1000 hours (approx. 41 days)
– Techniques for using redundancy to avoid data loss are critical with large numbers of disks
Redundancy – store extra information that can be used to rebuild information lost in a disk failure
E.g., Mirroring (or shadowing)
– Duplicate every disk. Logical disk consists of two physical disks.
– Every write is carried out on both disks
• Reads can take place from either disk
– If one disk in a pair fails, data still available in the other
• Data loss would occur only if a disk fails, and its mirror disk also fails before the system is repaired
Mean time to data loss depends on mean time to failure,
and mean time to repair
– E.g. MTTF of 100,000 hours, mean time to repair of 10 hours gives mean time to data loss of 500*10 6
hours (or 57,000 years) for a mirrored pair of disks (ignoring dependent failure modes)
BITS Pilani,Pilani
Hyderabad Campus
Campus
Improvement in Performance via Parallelism
BITS Pilani,Pilani
Hyderabad Campus
Campus
RAID Levels
Schemes to provide redundancy at lower cost by using disk striping combined with parity bits
RAID Level 0: Block striping; non-redundant.
Used in high-performance applications where data loss is not
critical.
RAID Level 1: Mirrored disks with block striping
Offers best write performance.
Popular for applications such as storing log files in a database.
BITS Pilani,Pilani
Hyderabad Campus
Campus
RAID Levels
BITS Pilani,Pilani
Hyderabad Campus
Campus
RAID Levels (Cont.)
RAID Level 5: Block-Interleaved Distributed Parity; partitions data and parity among all N + 1
disks
E.g., with 5 disks, parity block for nth set of blocks is stored on disk (n mod 5) + 1, with the data
blocks stored on the other 4 disks.
BITS Pilani,Pilani
Hyderabad Campus
Campus
Choice of RAID Level
BITS Pilani,Pilani
Hyderabad Campus
Campus
Hardware Issues
Software RAID: RAID implementations done entirely in software, with no special hardware
support
Hardware RAID: RAID implementations with special hardware
– Use non-volatile RAM to record writes that are being executed
– Beware: power failure during write can result in corrupted disk
• Such corrupted data must be detected when power is restored
• Recovery from corruption is similar to recovery from failed disk
• NV-RAM helps to efficiently detected potentially corrupted blocks
• Otherwise all blocks of disk must be read and compared with mirror/parity block
BITS Pilani,Pilani
Hyderabad Campus
Campus
File Organization, Record Organization and
Storage Access
BITS Pilani,Pilani
Hyderabad Campus
Campus
File Organization
The database is stored as a collection of files. Each file is a sequence of records mapped onto
disk blocks. A record is a sequence of fields.
One approach:
–assume record size is fixed
–each file has records of one particular type only
–different files are used for different relations
This case is easiest to implement; will consider variable length records later.
BITS Pilani,Pilani
Hyderabad Campus
Campus
Fixed-Length Records
Simple approach:
– Store record i starting from byte n * (i – 1), where n is the size of each record.
– Record access is simple but records may cross blocks
• Modification: do not allow records to cross block boundaries
Deletion of record i:
alternatives:
– move records i + 1, . . ., n
to i, . . . , n – 1
– move record n to i
– do not move records, but
link all free records on a
free list
BITS Pilani,Pilani
Hyderabad Campus
Campus
Deleting record 3 and compacting
BITS Pilani,Pilani
Hyderabad Campus
Campus
Deleting record 3 and moving last
record
BITS Pilani,Pilani
Hyderabad Campus
Campus
Free Lists
Store the address of the first deleted record in the file header.
Use this first record to store the address of the second deleted record, and so on
Can think of these stored addresses as pointers since they “point” to the location of a record.
Reuse space for normal attributes of free records to store pointers. (No pointers stored in in-use
records.)
BITS Pilani,Pilani
Hyderabad Campus
Campus
Variable-Length Records
BITS Pilani,Pilani
Hyderabad Campus
Campus
Variable-Length Records: Slotted Page Structure
Heap – a record can be placed anywhere in the file where there is space
Sequential – store records in sequential order, based on the value of the search key of each
record
Hashing – a hash function computed on some attribute of each record; the result specifies in
which block of the file the record should be placed
BITS Pilani,Pilani
Hyderabad Campus
Campus
Sequential File Organization
Suitable for applications that require sequential processing of the entire file
The records in the file are ordered by a search-key
BITS Pilani,Pilani
Hyderabad Campus
Campus
Sequential File Organization
BITS Pilani,Pilani
Hyderabad Campus
Campus
Multitable Clustering File Organization
instructor
multitable clustering
of department and
instructor
BITS Pilani,Pilani
Hyderabad Campus
Campus
Multitable Clustering File Organization (cont.)
good for queries involving department instructor, and for queries involving one single
department and its instructors
bad for queries involving only department
results in variable size records
Can add pointer chains to link records of a particular relation
BITS Pilani,Pilani
Hyderabad Campus
Campus
Data Dictionary Storage
The data dictionary (also called system catalog) stores metadata; that is, data about data,
such as
BITS Pilani,Pilani
Hyderabad Campus
Campus
Storage Access
A database file is partitioned into fixed-length storage units called blocks. Blocks are units of
both storage allocation and data transfer.
Database system seeks to minimize the number of block transfers between the disk and
memory. We can reduce the number of disk accesses by keeping as many blocks as possible
in main memory.
Buffer – portion of main memory available to store copies of disk blocks.
Buffer manager – subsystem responsible for allocating buffer space in main memory .
BITS Pilani,Pilani
Hyderabad Campus
Campus
Buffer Manager
Programs call on the buffer manager when they need a block from disk.
1. If the block is already in the buffer, buffer manager returns the address of the block in
main memory
2. If the block is not in the buffer, the buffer manager
1. Allocates space in the buffer for the block
1. Replacing (throwing out) some other block, if required, to make space for the new block.
2. Replaced block written back to disk only if it was modified since the most recent time that
it was written to/fetched from the disk.
2. Reads the block from the disk to the buffer, and returns the address of the block in main
memory to requester.
BITS Pilani,Pilani
Hyderabad Campus
Campus
Buffer-Replacement Policies
Most operating systems replace the block least recently used (LRU strategy)
– Idea behind LRU – use past pattern of block references as a predictor of future references
Queries have well-defined access patterns (such as sequential scans), and a database system
can use the information in a user’s query to predict future references
– LRU can be a bad strategy for certain access patterns involving repeated scans of data
• For example: when computing the join of 2 relations r and s by a nested loops
for each tuple tr of r do
for each tuple ts of s do
if the tuples tr and ts match …
– Mixed strategy with hints on replacement strategy provided
by the query optimizer is preferable
BITS Pilani,Pilani
Hyderabad Campus
Campus
Buffer-Replacement Policies (Cont.)
Pinned block – memory block that is not allowed to be written back to disk.
Toss-immediate strategy – frees the space occupied by a block as soon as the final tuple of
that block has been processed
Most recently used (MRU) strategy – system must pin the block currently being processed.
After the final tuple of that block has been processed, the block is unpinned, and it becomes
the most recently used block.
Buffer manager can use statistical information regarding the probability that a request will
reference a particular relation
– E.g., the data dictionary is frequently accessed. Heuristic: keep data-dictionary blocks in
main memory buffer
Buffer managers also support forced output of blocks for the purpose of recovery.
BITS Pilani,Pilani
Hyderabad Campus
Campus
Indexing and Hashing
BITS Pilani,Pilani
Hyderabad Campus
Campus
Indexing and Hashing
Basic Concepts
Ordered Indices
B+-Tree Index Files
Static Hashing
Dynamic Hashing
Multiple-Key Access
Bit-Map Indices
BITS Pilani,Pilani
Hyderabad Campus
Campus
Basic Concepts
search-key pointer
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 across “buckets” using a “hash
function”.
BITS Pilani,Pilani
Hyderabad Campus
Campus
Index Evaluation Metrics
BITS Pilani,Pilani
Hyderabad Campus
Campus
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
– The search key of a primary index is usually the primary key.
Secondary index: an index whose search key specifies an order different from the sequential
order of the file. Also called non-clustering index.
Index-sequential file: ordered sequential file with a primary index.
BITS Pilani,Pilani
Hyderabad Campus
Campus
Dense Index Files
Dense index — Index record appears for every search-key value in the file.
E.g. index on ID attribute of instructor relation
BITS Pilani,Pilani
Hyderabad Campus
Campus
Dense Index Files (Cont.)
Dense index on dept_name, with instructor file sorted on dept_name
BITS Pilani,Pilani
Hyderabad Campus
Campus
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
BITS Pilani,Pilani
Hyderabad Campus
Campus
Sparse Index Files (Cont.)
BITS Pilani,Pilani
Hyderabad Campus
Campus
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
– inner index – the primary index file
If even outer index is too large to fit in main memory, yet another level of index can be created,
and so on.
Indices at all levels must be updated on insertion or deletion from the file.
BITS Pilani,Pilani
Hyderabad Campus
Campus
Multilevel Index (Cont.)
BITS Pilani,Pilani
Hyderabad Campus
Campus
Index Update
BITS Pilani,Pilani
Hyderabad Campus
Campus
(Contd.)
BITS Pilani,Pilani
Hyderabad Campus
Campus
Case: Deletion of a record from a file
BITS Pilani,Pilani
Hyderabad Campus
Campus
(Contd.)
BITS Pilani,Pilani
Hyderabad Campus
Campus
Secondary Indices Example
BITS Pilani,Pilani
Hyderabad Campus
Campus
B+-Tree Index Files
BITS Pilani,Pilani
Hyderabad Campus
Campus
Example of B+-Tree
BITS Pilani,Pilani
Hyderabad Campus
Campus
B+-Tree Index Files (Cont.)
BITS Pilani,Pilani
Hyderabad Campus
Campus
B+-Tree Node Structure
Typical node
BITS Pilani,Pilani
Hyderabad Campus
Campus
Leaf Nodes in B+-Trees
BITS Pilani,Pilani
Hyderabad Campus
Campus
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 P1 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 Pn points have values greater than or equal to
Kn–1
BITS Pilani,Pilani
Hyderabad Campus
Campus
Example of B+-tree
BITS Pilani,Pilani
Hyderabad Campus
Campus
Observations about B+-trees
Since the inter-node connections are done by pointers, “logically” close blocks need not be
“physically” close.
The non-leaf levels of the B+-tree form a hierarchy of sparse indices.
The B+-tree contains a relatively small number of levels
• Level below root has at least 2* én/2ù values
• Next level has at least 2* én/2ù * én/2ù values
• .. etc.
– If there are K search-key values in the file, the tree height is no more than é logén/2ù(K)ù
– thus searches can be conducted efficiently.
Insertions and deletions to the main file can be handled efficiently, as the index can be
restructured in logarithmic time.
BITS Pilani,Pilani
Hyderabad Campus
Campus
Queries on B+-Trees
BITS Pilani,Pilani
Hyderabad Campus
Campus
Queries on B+-Trees (Cont.)
If there are K search-key values in the file, the height of the tree is no more than élogén/2ù(K)ù.
A node is generally the same size as a disk block, typically 4 kilobytes
– and n is typically around 100 (40 bytes per index entry).
Contrast this with a balanced binary tree with 1 million search key values — around 20 nodes are accessed
in a lookup
– above difference is significant since every node access may need a disk I/O, costing around 20 milliseconds
BITS Pilani,Pilani
Hyderabad Campus
Campus
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
2. If necessary add a pointer to the bucket.
3. If the search-key value is not present, then
1. add the record to the main file (and create a bucket if necessary)
2. If there is room in the leaf node, insert (key-value, pointer) pair in the leaf node
3. Otherwise, split the node (along with the new (key-value, pointer) entry) as discussed in
the next slide.
BITS Pilani,Pilani
Hyderabad Campus
Campus
Updates on B+-Trees: Insertion (Cont.)
Splitting of nodes proceeds upwards till a node that is not full is found.
– In the worst case the root node may be split increasing the height of the tree by 1.
BITS Pilani,Pilani
Hyderabad Campus
Campus
B+-Tree Insertion
BITS Pilani,Pilani
Hyderabad Campus
Campus
Insertion in B+-Trees (Cont.)
Splitting a non-leaf node: when inserting (k,p) into an already full internal node N
– Copy N to an in-memory area M with space for n+1 pointers and n keys
– Insert (k,p) into M
– Copy P1,K1, …, K én/2ù-1,P én/2ù from M back into node N
– Copy Pén/2ù+1,K én/2ù+1,…,Kn,Pn+1 from M into newly allocated node N’
– Insert (K én/2ù,N’) into parent N
Read pseudocode in book!
BITS Pilani,Pilani
Hyderabad Campus
Campus
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 (Ki–1, Pi), where Pi is the pointer to the deleted node, from its parent,
recursively using the above procedure.
BITS Pilani,Pilani
Hyderabad Campus
Campus
Updates on B+-Trees: Deletion
Otherwise, if the node has too few entries due to the removal, but the entries in the node and a
sibling do not fit into a single node, then redistribute pointers:
– Redistribute the pointers between the node and a sibling such that both have more than the
minimum number of entries.
– Update the corresponding search-key value in the parent of the node.
The node deletions may cascade upwards till a node which has én/2ù or more pointers is found.
If the root node has only one pointer after deletion, it is deleted and the sole child becomes the
root.
BITS Pilani,Pilani
Hyderabad Campus
Campus
Examples of B+-Tree Deletion
BITS Pilani,Pilani
Hyderabad Campus
Campus
Examples of B+-Tree Deletion (Cont.)
Node with Redwood became underfull, and was merged with its sibling
Root node then has only one child, and is deleted
BITS Pilani,Pilani
Hyderabad Campus
Campus
Example of B+-tree Deletion (Cont.)
BITS Pilani,Pilani
Hyderabad Campus
Campus
B+-Tree File Organization
BITS Pilani,Pilani
Hyderabad Campus
Campus
B+-Tree File Organization (Cont.)
Good space utilization important since records use more space than pointers.
To improve space utilization, involve more sibling nodes in redistribution during splits and merges
– Involving 2 siblings in redistribution (to avoid split / merge where possible) results in each node having at least entries
BITS Pilani,Pilani
Hyderabad Campus
Campus
Multiple-Key Access
BITS Pilani,Pilani
Hyderabad Campus
Campus
Indices on Multiple Attributes
Composite search keys are search keys containing more than one attribute
– E.g. (branch_name, balance)
Lexicographic ordering: (a1, a2) < (b1, b2) if either
– a1 < b1, or
– a1=b1 and a2 < b2
BITS Pilani,Pilani
Hyderabad Campus
Campus
Indices on Multiple Attributes
Suppose we have an index on combined search-key
(branch_name, balance).
With the where clause
where branch_name = “Perryridge” and balance = 1000
the index on (branch_name, balance) can be used to fetch only records that satisfy both
conditions.
– Using separate indices in less efficient — we may fetch many records (or pointers) that
satisfy only one of the conditions.
Can also efficiently handle
where branch_name = “Perryridge” and balance < 1000
But cannot efficiently handle
where branch_name < “Perryridge” and balance = 10000
– May fetch many records that satisfy the first but not the second condition
BITS Pilani,Pilani
Hyderabad Campus
Campus
Other Kinds of Indices
Hash indices
– Used widely for in-memory indices, but not much for on-disk indices
Bitmap indices
– Used for attributes with very few distinct values
– Primary use: answering some aggregate queries very efficiently
BITS Pilani,Pilani
Hyderabad Campus
Campus
Hash-Based Indexes
BITS Pilani,Pilani
Hyderabad Campus
Campus
Static Hashing
BITS Pilani,Pilani
Hyderabad Campus
Campus
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.
BITS Pilani,Pilani
Hyderabad Campus
Campus
Example of Hash File Organization
BITS Pilani,Pilani
Hyderabad Campus
Campus
Example of Hash File Organization
BITS Pilani,Pilani
Hyderabad Campus
Campus
Handling of Bucket Overflows (Cont.)
Overflow chaining – the overflow buckets of a given bucket are chained together in a
linked list.
Above scheme is called closed hashing.
– An alternative, called open hashing, which does not use overflow buckets, is not
suitable for database applications.
BITS Pilani,Pilani
Hyderabad Campus
Campus
Hash Indices
Hashing can be used not only for file organization, but also for index-structure
creation.
A hash index organizes the search keys, with their associated record pointers,
into a hash file structure.
Strictly speaking, hash indices are always secondary indices
– if the file itself is organized using hashing, a separate primary hash index on it using the same search-key is unnecessary.
– However, we use the term hash index to refer to both secondary index structures and hash organized files.
BITS Pilani,Pilani
Hyderabad Campus
Campus
Example of Hash Index
BITS Pilani,Pilani
Hyderabad Campus
Campus
Deficiencies of Static Hashing
BITS Pilani,Pilani
Hyderabad Campus
Campus
Dynamic Hashing
BITS Pilani,Pilani
Hyderabad Campus
Campus
General Extendable Hash Structure
BITS Pilani,Pilani
Hyderabad Campus
Campus
Use of Extendable Hash Structure
BITS Pilani,Pilani
Hyderabad Campus
Campus
Insertion in Extendable Hash Structure (Cont.)
BITS Pilani,Pilani
Hyderabad Campus
Campus
Deletion in Extendable Hash Structure
– Coalescing of buckets can be done (can coalesce only with a “buddy” bucket having same value of i and same i –1 prefix, if it is present)
j
j
– Decreasing bucket address table size is also possible
• Note: decreasing bucket address table size is an expensive operation and should be done only if number of buckets becomes
much smaller than the size of the table
BITS Pilani,Pilani
Hyderabad Campus
Campus
Use of Extendable Hash Structure: Example
BITS Pilani,Pilani
Hyderabad Campus
Campus
Example (Cont.)
Initial Hash structure; bucket size = 2
BITS Pilani,Pilani
Hyderabad Campus
Campus
Example (Cont.)
BITS Pilani,Pilani
Hyderabad Campus
Campus
Example (Cont.)
BITS Pilani,Pilani
Hyderabad Campus
Campus
Example (Cont.)
BITS Pilani,Pilani
Hyderabad Campus
Campus
Example (Cont.)
BITS Pilani,Pilani
Hyderabad Campus
Campus
Extendable Hashing vs. Other
Schemes
Benefits of extendable hashing:
– Hash performance does not degrade with growth of file
– Buckets can be allocated dynamically
Disadvantages of extendable hashing
– Extra level of indirection to find desired record
– Changing size of bucket address table is an expensive operation
BITS Pilani,Pilani
Hyderabad Campus
Campus
Bitmap Indices
Bitmap indices are a special type of index designed for efficient querying on multiple
keys
Records in a relation are assumed to be numbered sequentially from, say, 0
– Given a number n it must be easy to retrieve record n
• Particularly easy if records are of fixed size
Applicable on attributes that take on a relatively small number of distinct values
– E.g. gender, country, state, …
– E.g. income-level (income broken up into a small number of levels such as 0-9999,
10000-19999, 20000-50000, 50000- infinity)
A bitmap is simply an array of bits
BITS Pilani,Pilani
Hyderabad Campus
Campus
Bitmap Indices (Cont.)
In its simplest form a bitmap index on an attribute has a bitmap for each value of the attribute
– Bitmap has as many bits as records
– In a bitmap for value v, the bit for a record is 1 if the record has the value v for the attribute,
and is 0 otherwise
BITS Pilani,Pilani
Hyderabad Campus
Campus
Bitmap Indices (Cont.)
BITS Pilani,Pilani
Hyderabad Campus
Campus