Unit 2 Data Structures, File Organisation and Physical Database Design
Unit 2 Data Structures, File Organisation and Physical Database Design
UNIT 2
Structure
2.0 2.1 2.2 Objectives Introduction Definitions and Basic Concepts
2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 Why Data Structures Memory Hierarchy RAID Technology Indexes Binary Search
2.3
Data Structures
2.3.1 2.3.2 2.3.3 Linked Lists Inverted Lists B-Trees
2.4
Physical Database Design Summary Answers to Self Check Exercises Keywords References and Further Reading
2.0 OBJECTIVES
After reading this Unit, you will be able to: understand the basic concepts related to data structures and file organisation; comprehend physical storage structures of data and file organisation techniques; and gain an insight into the role the data structures and file organisation play in the overall performance and access efficiency in a database.
2.1 INTRODUCTION
Data structures and file organisation refer to the methods of organising the data in a database. They primarily deal with physical storage of data, which assumes significance in retrieving, storing and re-organising data in a database. Data structures include linked
22
lists, inverted lists, B-trees and hash tables, among others. Data structures can be used to build data files (a data file or a file is a collection of many similar records) and file organisation determines access methods for the file. File organisation (or file structure) is a combination of representations for data in files and of operations for accessing the data. A file structure allows applications to read, write, and modify data. It might also support finding the data that matches some search criteria or reading through the data in some particular order. Data structures and file organisation define the physical design of a database and are critical to its performance.
23
Static RAM (SRAM) which is cache memory is used by CPU to speed up execution of programmes while Dynamic RAM (DRAM) provides the main work area for CPU. Flash memory which is non-volatile and called EEPROM (Electrically Erasable Programmable Read-Only-Memory) has access speed and performance between DRAM and magnetic disks. CD-ROM (Compact Disk Read-Only-Memory) disks store data optically and are read by a laser. WORM (Write-Once-Read-Memory) disks are used for archiving data and allow data to be written once and read any number of times. DVD (Digital Video Disk) a type of optical disk allows storage of four to fifteen gigabytes of data per disk. Magnetic tapes are used for archiving and back-up storage and are becoming popular as tertiary storage to hold terabytes of data. Juke boxes (optical and tape) are employed to use arrays of CD-ROMs and tapes.
24
multiple I/Os to be serviced in parallel, thus providing high overall transfer rates. Data striping also accomplishes load balancing among disks. It should be noted that data can be read or written only one block at a time, so a typical transfer contains 512 bytes (block size = 512 bytes). Data striping can be applied at a finer granularity by breaking up a byte of data into bits and spreading the bits to multiple disks. Using bit-level data striping with 8-bit bytes, eight physical disks may be considered as one logical disk with an eight fold increase in data transfer rate. Each disk participates in each I/O request and the total data read per request is eight times. Data striping may also be done at block level which distributes blocks of a file across disks. In addition to improving performance, RAID is also used to improve reliability by storing redundant information on disks. One technique for introducing redundancy is called mirroring. Data is written redundantly to two identical physical disks that are treated as one logical disk. If a disk fails, the other is used until the first is repaired. Thus, RAID technology has contributed significantly in improving the performance and reliability of data storage on disks.
2.2.4 Indexes
An index is a file in which each entry (record) consists of a data value together with one or more pointers (physical storage addresses). The data value is a value for some field of the indexed file (the indexed field) and pointers identify records in the indexed file having that value for that field. The concept of indexing is closely linked with the operation of searching. An index (sometimes also referred as a list) can be used in two ways. First, it can be used for sequential access to the indexed file, i.e., access according to the values of the indexed field by imposing an ordering of the indexed file. Second, it can also be used for direct access to individual records in the indexed file on the basis of a given value for that same field. In general, indexing speeds up retrieval but may slow down update. Address/Pointer
LastName Pointer A11 A22 A32 A42 A47 A58 A63 A67 ID LastName FirstName DateJoined
A 67 A 58 A 63 A 22 A 42 A 47 A 32 A 11
1 2 3 4 5 6 7 8
Indexed File
in the top or bottom half of the list. The value sought is then compared with the middle entry of the appropriate half. This indicates which fourth the value is in. Then the value is compared to the middle of the fourth, and so on until the desired value is found. Thus the binary search keeps splitting the data set in half until it finds the desired value. An example of binary search is shown in Fig. 2.3. To find the entry for Janardan find the middle of the list (Gautam). Janardan is post Gautam so split the second half in half (Kamla). Keep splitting the remainder in half until Janardan is found.
Alexander Bhatnagar Chand Dhani Ejaz Feroze Gautam Hegde Ipshita 4 Janardan
2 Kamla
Note: i) ii)
There are three major types of data structures : linked lists (indexes), inverted lists (indexes) and B-trees. These data structures have been explained in the following paragraphs:
A 22 B 87
Chauhan
B29
A 58
B 29
Dhote
B71
A 63
B 71
B 38
27
The above lists are dense since there is one-to-one relationship between both company name and primary key and company symbol and primary key. Fig. 2.6 gives an example of non-dense inverted list for area (relationship between area and primary key is one-to-many). Area Aerospace Auto Computer Primary Key 1152 1175, 1323 1231, 1245
The lists are said to be inverted because company names (or area names) have been alphabetized and the corresponding primary keys have been inverted or rearranged accordingly.
2.3.3
B-Trees
B-trees are a form of data structure based on hierarchies. Some authors claim that the letter B stands for Bayer, the originator while others say it stands for balanced. Btrees are balanced in the sense that all the terminal (bottom) nodes have the same path length to the root (top). Algorithms have been developed for efficiently searching and maintaining B-tree indexes, which have become quite popular for representing both primary and secondary indexes. B-trees provide both sequential and indexed access and are quite flexible. The height of a B-tree is the number of levels in the hierarchy. Each node on the tree contains an index element which has a key value, a pointer to the rest of the data and two link pointers (see Fig.2.7) One link (to the left) points to the elements (nodes) that have lower values while the other link (to the right) points to elements that have a value greater than or equal to the value in the node. The root is the highest node on the tree. The bottom nodes are called leaves because they are at the end of the tree branches.
28
<
Key
Data
A B-tree is called unbalanced if the terminal nodes (leaves) are not all at the same level i.e., if different terminal nodes are at different depths below the top node. B-trees provide excellent access performance but do not allow a file to be accessed sequentially with efficiency. This problem is overcome by adding a linked list structure at the bottom level of the B-tree. The combination of a B-tree and a sequential linked list is called a B+ tree.
Note: i) ii)
different records in the file have different sizes, the file is said to be made up variablelength records. A file may have variable-length records for several reasons: i) ii) The file records are of the same record type, but one or more fields are of varying sizes (variable-length fields). The file records are of the same record type but one or more fields may have multiple values for individual records. Such a field is called repeating field and a group of values for the field is often called a repeating group. The file records are of the same record type, but one or more fields are optional. The file has records of different record types and hence of varying size (mixed file). This would occur if related records of different types were clustered (placed together) on disk blocks.
iii) iv)
The records of a file must be allocated to disk blocks because a block is a unit of data transfer between disk and memory. The division of a track (on storage medium) into equal sized disk blocks is set by the operating system during disk formatting. The hardware address of a block comprises a surface number, track number and block number. Buffer a contiguous reserved area in main storage that holds one block has also an address. For a read command, the block from disk is copied into the buffer, whereas for a write command the contents of the buffer are copied into the disk block. Sometimes several contiguous blocks, called a cluster, may be transferred as a unit. In such cases buffer size is adjusted to cluster size. When the block size is larger than the record size each block will contain numerous records, while there can be files with large records that cannot fit in one block. In the latter case the records can span more than one block. Here it is worthwhile to note the difference between the terms File Organisation and Access Method. A file organisation refers to the organisation of the data of a file into records, blocks and access structures; this includes the way the records and blocks are placed on the storage medium and interlinked. An access method on the other hand, provides a group of operations such as find, read, modify, delete, etc, that can be applied to a file. In general, it is possible to apply several access methods to a file organisation. Some access methods, though, can be applied only to files organised in certain ways. For example, we cannot apply an indexed access method to a file without an index.
30
Key Value
Address/Pointer
Index file
31
The objective of the hashing algorithm is to generate relative addresses that disperse the records throughout the reserved storage space in a random but uniform manner. The records can be retrieved very rapidly because the address is computed rather than found through table look-up via indexes stored on a disk file. A collision is said to occur if more than one record maps to the same block. Because one block usually holds several records, collisions are only a problem when the number of records mapping to a block exceeds the blocks capacity. To account for this event, most direct access methods support overflow area for collisions, which is searched sequentially. The hashed key approach is extremely fast since the keys value is immediately converted into a storage location, and data can be retrieved in one pass to the disk. An illustration of direct access method using hashed key is given in Fig. 2.9.
Hashing Algo
The files that will be accessed by the query. The attributes on which any selection conditions for the query are specified. The attributes on which any join conditions or conditions to link multiple tables for the query are specified. The attributes whose values will be retrieved by the query.
d)
The attributes at b) and c) are candidates for definition of access structures. For each update transaction we should specify: a) b) c) d) The files that will be updated. The type of operation on each file (insert, update or delete). The attributes on which the selection conditions for a delete or update are specified. The attributes whose value will be changed by the update operation.
Here the attributes at c) are candidates for access structures and attributes at d) are candidates for avoiding an access structure since modifying them will require updating the access structures. 2) Analysing the expected frequency of queries and transactions: This yields the expected frequency of using each attribute in each file as a selection attribute or a join attribute, over all the queries and transactions. Analysing the time constraint of queries and transactions: Some queries and transactions may have stringent performance constraints with respect to response time. The selection attributes used by queries and transactions with such time constraints become higher priority candidates for primary access structures. Analysing the expected frequencies of update operations : A minimum number of access paths should be specified for a file that is updated frequently, because updating the access paths themselves slows the update operations.
3)
4)
Based on the preceding information one can address the physical database design decisions about indexing. The attributes whose values are required in equality or range conditions (selection operation) and those that are keys or that participate in join conditions (join operation) require access paths. The performance of queries largely depends upon what indexes or hashing schemes exist to expedite the processing of selections and joins. On the other hand, during insert, delete, or update operations, existence of indexes adds to the overhead. The following points may be kept in view while taking decisions for indexing: i) The attribute, which is to be indexed, must be a key or there must be some query that uses that attribute either in selection condition (equality or range of values) or in a join. An index can be made on one or multiple attributes. If multiple attributes from one relation are involved together in several queries, a multiattribute index is warranted. Clustering index (index created on a non-key field i.e., if numerous records in a file can have the same value for the field) can be greatly useful in range queries. If several attributes require range queries relative benefits must be evaluated before deciding which attributes to cluster on. At most one index per table can be a primary or clustering index. RDBMSs generally use B+ trees for indexing. ISAM and hash indexes are also provided in some systems. B+ trees support both equality and range queries on the attribute used as the search key. Hash indexes work well with equality conditions, particularly during joins.
ii) iii)
iv)
2.6 SUMMARY
This Unit covers some of the key issues related to data structures, file organisation and physical database design and provides essential background to facilitate their understanding. The role of data structures and file organisation on the performance of access methods has been explained. RAID technology, binary search and indexes have been discussed to elucidate how access speed can be improved. Typical data structures (linked lists, inverted lists and B-trees) and file organisation techniques (SAM, ISAM and DAM) have been
33
dealt with. The factors, which influence the physical database design and decisions on access structures, have been explained. This Unit lays the foundation for understanding the complex key concepts related to the topic.
2)
2.8 KEYWORDS
Cache Memory Binary Search B-tree B + -tree Hashing : A high speed temporary storage in the CPU for storing parts of a program or data during processing. : A search technique for sorted data. : An indexed data storage method that is efficient for a wide range of data access tasks. : A variation on the B-tree structure that provides sequential access to the data as well as fast-indexed access. : An access mechanism that transforms the search key into a storage address, thereby providing very fast access to stored data. : A sorted list of key values from the original table along with a pointer to the rest of the data in each row. : A logical or physical address of a piece of data . : Redundant Array of Independent Disks. A disk drive system that consists of multiple drives with independent controllers. The goal is to split the data to provide faster access and automatic duplication for error recovery.
Sequential Access : Access that takes records in order, looking at the first, then the next, and so on.
OBrien, James A. (1997). Introduction to Information Systems. Irwin: The McGrawHill Company.