File Organization and Indexing
Disk Storage Devices
Secondary storage device for high storage capacity and low cost.
Data stored as magnetized areas on magnetic disk surfaces.
A disk pack contains several magnetic disks connected to a rotating spindle.
Disks are divided into concentric circular tracks on each disksurface. Track capacities vary typically from 4 to 50 Kbytes.
A track is divided into blocks (In some systems, there is an intermediate unit called sectors). The block size B is fixed for each system. Typical block sizes range from B=512 bytes to B=4096 bytes.
Whole blocks are transferred between disk and main memory for processing.
A read-write head moves to the track that contains the block to be transferred. Disk rotation moves the block under the read-write head for reading or writing.
A physical disk block address consists of a surface number, track number (within surface), and block number (within track).
Reading or writing a disk block is time consuming because of the seek time s and rotational delay (latency) rd.
Double buffering can be used to speed up the transfer of contiguous disk blocks.
A file is a sequence of records, where each record is a collection of data values (or data items).
A file descriptor (or file header ) includes information that describes the file, such as the field names and their data types, and the addresses of the file blocks on the disk.
Records are stored on disk blocks. The blocking factor bfr for a file is the (average) number of file records stored in one disk block.
A file can have fixed-length records orvariable-length records.
File of Records
-
● File records can be
● unspanned (no record can span two blocks) or
● spanned (a record can be stored in more than one block).
-
● In a file of fixed-length records, all records have the same format. Usually, unspanned blocking is used with such files.
-
● Files of variable-length records require additional information to be stored in each record. Usually spanned blocking is used with such files.
-
● The physical disk blocks that are allocated to hold the records of a file can be contiguous, linked, or indexed.
OPEN: Make the file ready for access, and associates a pointer that will refer to a current file record at each point in time.
FIND: Searches for the first file record that satisfies a certain condition, and makes it the current file record.
FINDNEXT: Searches for the next file record (from the current record) that satisfies a certain condition, and makes it the current file record.
READ: Reads the current file record into a program variable.
INSERT: Inserts a new record into the file, and makes it the current file record.
DELETE: Removes the current file record from the file, usually by marking the record to indicate that it is no longer valid.
MODIFY: Changes the values of some fields of the current file record.
CLOSE: Terminates access to the file.
REORGANIZE: Reorganizes the file records. For example, the records marked “deleted” are physically removed from the file or a new organization of the file records is created.
READ_ORDERED: Reads the file blocks in order of a specific field of the file.
Unordered Files
-
● Also called a pile file.
-
● New records are inserted at the end of the file.
-
● To search for a record, a linear search through the file records is necessary. This requires reading and searching half the file blocks on the average, and is hence quite expensive.
-
● Record insertion is quite efficient.
-
● Reading the records in order of a particular field requires sorting the file records.
Ordered Files
-
-
● Also called a sequential file.
-
● File records are kept sorted by the values of an ordering
field.
-
● Insertion is expensive: records must be inserted in thecorrect position. It is common to keep a separate unordered overflow file for new records to improve insertion efficiency; this is periodically merged with the main ordered file.
-
● A binary search can be used to search for a record on itsordering field value. This requires reading and searching log2n of the file blocks on the average, an improvement over linear search.
-
● Reading the records in order of the ordering field is quite efficient.
-
Pointersint y=5;
int *ypointer; ypointer=&y;
*ypointer=?
Indexing is pretty much about referencing
Or more precisely, organizing references to help identify records that you are interested in
c=1196
1196
data
y6p0o0in0t0er
60000
y=5
a0
a2
a1
first
CS3402
14
Preparation on Data Structures
class TreeNode {
private:
int info; TreeNode* left; TreeNode* right;
};
class Mytree {
private:
TreeNode* root; }
root
left |
A |
right |
left |
C |
right |
left |
B |
right |
left |
G |
right |
left
E
right
left
F
right
__
__ __ ___ ______ ______
Binary Trees: Every node has two links (pointers)
__ __ __
●
CS3402
15
Hashed Files
-
● Hashing for disk files is called External Hashing
-
● The file blocks are divided into M equal-sized buckets, numbered bucket0,
bucket1, ..., bucketM-1.
● Typically, a bucket corresponds to one (or a fixed number of) disk block.
-
● One of the file fields is designated to be the hash key of the file.
-
● The record with hash key value K is stored in bucket i, where i=h(K), and h
is the hashing function.
-
● Search is very efficient on the hash key.
-
● Collisions occur when a new record hashes to a bucket that is already full.
-
● An overflow file is kept for storing such records.
-
● Overflow records that hash to each bucket can be linked together.
In the following, we assume h(x)=x%10 (take the last digit)
-
CS3402
16
●
Hashed Files
There are numerous methods for collision resolution, including the following:
-
● Open addressing: Proceeding from the occupied position specified by the hash address, the program checks the subsequent positions in order until an unused (empty) position is found.
● Linear Probing
● If collide, try Bucket_id+1, Bucket_id+2, ..., Bucket_id+n● Quadratic Probing
● If collide, try Bucket_id+1, Bucket_id+4,..., Bucket_id+n2 -
● Chaining: For this method, various overflow locations are kept, usually by extending the array with a number of overflow positions. In addition, a pointer field is added to each record location. A collision is resolved by placing the new record in an unused overflow location and setting the pointer of the occupied hash address location to the address of that overflow location.
CS3402
17
Collision Resolution
954,323
954,323
Linear Probing
Quadratic Probing
0 |
1 |
2 |
Full |
Full |
5 |
6 |
7 |
8 |
9 |
0 |
1 |
2 |
Full |
Full |
5 |
6 |
7 |
8 |
9 |
Every slot is a bucket
CS3402
18
Hashed Files - Overflow handling
Each bucket has a capacity of 3 records.
CS3402
19
Hashed Files
-
● The hash function h should distribute the records uniformly among the buckets
● Otherwise, search time will increase because many overflow records will exist.
-
● Main disadvantages of static external hashing:
● Fixed number of buckets M is a problem when the number of records in the file grows or shrinks.
CS3402
20
Dynamic And Extendible Hashing
-
● Dynamic and Extendible Hashing Techniques
-
● Hashing techniques are adapted to allow the dynamic growth
and shrinking of the number of file records.
-
● These techniques include the following: dynamic hashing,
extendible hashing
-
-
● Both dynamic and extendible hashing use the binary representation of the hash value h(K) in order to access adirectory.
● In dynamic hashing the directory is a binary tree.
● In extendible hashing the directory is an array of size 2d where d is called the global depth.
CS3402
21
Dynamic And Extendible Hashing
-
● The directories can be stored on disk, and they expand or shrink dynamically.
● Directory entries point to the disk blocks that contain the stored records.
-
● An insertion in a bucket that is full causes the bucket to split into two buckets and the records are redistributed among the two buckets.
● The directory is updated appropriately.
-
● Dynamic and extendible hashing do not require an overflow area.
CS3402
22
Extendible Hashing
CS3402
23
How to find a record quickly?
-
● Try to find the first page which contains the word “database” in a book
-
● What tool can you use?
CS3402
24
Indexes as Access Paths
-
● A single-level index is an auxiliary file that makes it more efficient to search for a record in the data file
-
● The index is usually specified on one field of the file (although it could be specified on several fields)
-
● One form of an index is a file of entries <field value, ptr to record>, which is ordered by field value
-
● The index is called an access path on the field
-
● The index file usually occupies considerably less disk blocks than the data file because its entries are much smaller
-
● A binary search on the index yields a pointer to the file record
CS3402
25
Indexes as Access Paths
Example: Given the following data file:EMPLOYEE(NAME, SSN, ADDRESS, JOB, SAL, ... )
Suppose that:
record size R=150 bytes block size B=512 bytes r=30000 records
Then, we get:
blocking factor Bfr= B div R= 512 div 150= 3 records/block
number of file blocks b= (r/Bfr)= (30000/3)= 10000 blocks
CS3402
26
Indexes as Access Paths
For an index on the SSN field, assume the field size VSSN=9 bytes, assume the record pointer size PR=7 bytes. Then:
index entry size RI=(VSSN+ PR)=(9+7)=16 bytes
index blocking factor BfrI= B div RI= 512 div 16= 32 entries/block number of index blocks b= (r/BfrI)= (30000/32)= 938 blocks
binary search needs log2b= log2938= 10 block accesses (+1 get file block)
This is compared to an average linear search cost of: (b/2)= 10000/2= 5000 block accesses
CS3402
27
Types of Single-Level Indexes
Primary Index
- - -
Defined on an ordered data file
The data file is ordered on a key field
Includes one index entry for each block in the data file; the index entry has the key field value for the first record in the block, which is called the block anchor
A similar scheme can use the last record in a blockClustering Index
●
- - -
Defined on an ordered data file
The data file is ordered on a non-key field
Includes one index entry for each distinct value of the field; the index entry points to the first data block that contains records with that field value
CS3402
28
Types of Single-Level Indexes
CS3402
29
Types of Single-Level Indexes
CS3402
30
Types of Single-Level Indexes
CS3402
31
If the file is ordered by SSN
-
● How to find all the employees in Department 1?
-
● What structure would you design?
CS3402
32
Types of Single-Level Indexes
● Secondary Index
-
● A secondary index provides a secondary means of accessing a file
for which some primary access already exists
-
● The secondary index may be on a key field or a non-key field
-
● The index is an ordered file with two fields (what you wanna findand reference to where it is).
● The first field is of the same data type as some non-ordering field of the data file that is an indexing field.
● The second field is either a block pointer or a record pointer.● There can be many secondary indexes for the same file.
● Includes one entry for each record in the data file (dense index)CS3402
33
CS3402
34
CS3402
35
Multi-Level Indexes
● If the index file occupies many blocks, searching still involve a lot of block readings
-
● How to reduce block readings?
-
● Hint
● Index file is an ordered file
In order to understand recursion, you first need to understand recursion.
-
● Because a single-level index is an ordered file, we can create a primary index to the index itself ; in this case, the original index file is called the first-level index and the index to the index is called the second-level index
-
● We can repeat the process, creating a third, fourth, ..., top level until all entries of the top level fit in one disk block
CS3402
36
CS3402
37
Multi-Level Indexes
-
● A multi-level index can be created for any type of first-level index (primary, secondary, clustering) as long as the first-level index consists of more than one disk block
-
● Such a multi-level index is a form of search tree ; however, insertion and deletion of new index entries is a severe problem because every level of the index is anordered file
-
● Because of the insertion and deletion problem, most multi-level indexes use B tree or B+ tree data
structures, which leave space in each tree node (disk block) to allow for new index entries
CS3402
38
Multi-Level Indexes
CS3402
39
A Node in a Search Tree with Pointers to Subtrees below It
P1 |
K1 |
... |
Ki-1 |
Pi-1 |
Ki |
... |
Kq-1 |
Pq |
CS3402
40
A Search Tree of Order p = 3 (3 pointers)
CS3402
41
Using B Trees and B+ Trees as Dynamic Multi-level Indexes
-
● These data structures are variations of search trees that allow efficient insertion and deletion of new search values
-
● In B Tree and B+ Tree data structures, each node corresponds to a disk block
-
● Each node is kept between half-full and completely full
CS3402
42
Using B Trees and B+ Trees as Dynamic Multi-level Indexes
-
● An insertion into a node that is not full is quite efficient; if a node is full the insertion causes a split into two nodes
-
● Splitting may propagate to other tree levels
-
● A deletion is quite efficient if a node does not become
less than half full
-
● If a deletion causes a node to become less than half full, it must be merged with neighboring nodes
CS3402
43
Difference between B tree and B+ tree:
-
● In a B tree, pointers to data records exist at all levels of the tree
-
● In a B+ tree, all pointers to data records exists at the leaf-level
nodes
-
● A B+ tree can have less levels (or more search values) than the corresponding B tree, example?
CS3402
44
Example B trees
CS3402
45
Example B+ Tree Node
-
● (a) Internal node of a B+-tree with q –1 search values.
-
● (b) Leaf node of a B+-tree with q – 1 search values and q – 1 data pointers.
CS3402
46
Example Insertion in a B+ Tree
CS3402
47
Example Deletion in a B+ Tree
CS3402
48
Physical Database Design in Relational Databases (indexing)
-
● Physical Database Design Decisions
-
● Design decisions about indexing
● Whether to index an attribute?
● What attribute or attributes to index on?
● Whether to set up a clustered index?
● Whether to use a hash index or a tree index?● Whether to use dynamic hashing for the file?
CS3402
49
Physical Database Design in Relational Databases (denormalization)
● Denormalization as a design decision for speeding up queries
-
● The goal of normalization is to separate the logically related attributes into tables to minimize redundancy and thereby avoid the update anomalies that cause an extra processing overheard to maintain consistency of the database.
-
● The goal of denormalization is to improve the performance of frequently occurring queries and transactions. (Typically the designer adds to a table attributes that are needed for answering queries or producing reports so that a join with another table is avoided.)
-
● Trade off between update and query performance
CS3402
50
An Overview of Database Tuning in Relational Systems
● Tuning:
● The process of continuing to revise/adjust the physical database design by monitoring resource utilization as well as internal DBMS processing to reveal bottlenecks such as contention for the same data or devices.
● Goal:
-
● To make application run faster
-
● To lower the response time of queries/transactions
-
● To improve the overall throughput of transactions
CS3402
51
An Overview of Database Tuning in Relational Systems
● Tuning Indexes
-
● Reasons to tuning indexes
-
● Certain queries may take too long to run for lack of an index;
-
● Certain indexes may not get utilized at all;
-
● Certain indexes may be causing excessive overhead because the index is on an attribute that undergoes frequent changes
-
-
● Options to tuning indexes
-
● Drop or/and build new indexes
-
● Change a non-clustered index to a clustered index (and vice versa)
-
● Rebuild the index
-
52
CS3402
An Overview of Database Tuning in Relational Systems
● Possible changes to the database design
-
● Existing tables may be joined (denormalized) because certain attributes from two or more tables are frequently needed together.
-
● For the given set of tables, there may be alternative design choices, all of which achieve 3NF or BCNF. One may be replaced by the other.
-
● A relation of the form R(K, A, B, C, D, ...) that is in BCNF can be stored into multiple tables that are also in BCNF by replicating the key K in each table.
-
● Attribute(s) from one table may be repeated in another even though this creates redundancy and potential anomalies.
-
● Apply horizontal partitioning as well as vertical partitioning if
necessary.
CS3402
53
An Overview of Database Tuning in Relational Systems
● Tuning Queries
● Motivations for tuning queries
● A query issues too many disk accesses
● The query plan shows that relevant indexes are not being used.
CS3402
54
An Overview of Database Tuning in Relational Systems
● Typical instances for query tuning
-
● In some situations involving using of correlated queries,
temporaries are useful.
-
● If multiple options for join condition are possible, choose one that uses a clustering index and avoid those that contain string comparisons.
-
● The order of tables in the FROM clause may affect the join processing.
-
● Some query optimizers perform worse on nested queries compared to their equivalent un-nested counterparts.
CS3402
55
An Overview of Database Tuning in Relational Systems
● Additional Query Tuning Guidelines
-
● A query with multiple selection conditions that are connected via OR may not be prompting the query optimizer to use any index. Such a query may be split up and expressed as a union of queries, each with a condition on an attribute that causes an index to be used.
-
● Apply the following transformations
-
● NOT condition may be transformed into a positive
expression.
-
● Embedded SELECT blocks may be replaced by joins.
-
● If an equality join is set up between two tables, the range predicate on the joining attribute set up in one table may be repeated for the other table
-
-
● WHERE conditions may be rewritten to utilize the indexes on multiple columns.
CS3402
56
Learning Objective
● The principle of hashed files● Indexes
-
● Primary Indexes
-
● Clustering Indexes
-
● Secondary Indexes
-
● Multilevel Indexes
● Definitions of and operations on B-Trees and B+-Trees
● ● ● ● ●
Textbook topics and pages: Pertinent: Chapter 16, pages 565-598
Optional:
Chapter 17, pages 613-642 Chapter 17, pages 642-652
Chapter 19