Presentation is loading. Please wait.

Presentation is loading. Please wait.

Database Management Systems, R. Ramakrishnan and J. Gehrke1 File Organizations and Indexing Chapter 5, 6 of Elmasri “ How index-learning turns no student.

Similar presentations


Presentation on theme: "Database Management Systems, R. Ramakrishnan and J. Gehrke1 File Organizations and Indexing Chapter 5, 6 of Elmasri “ How index-learning turns no student."— Presentation transcript:

1 Database Management Systems, R. Ramakrishnan and J. Gehrke1 File Organizations and Indexing Chapter 5, 6 of Elmasri “ How index-learning turns no student pale Yet holds the eel of science by the tail. ” -- Alexander Pope (1688-1744)

2 Database Management Systems, R. Ramakrishnan and J. Gehrke2 2 Files of Records A file is a sequence of records, where each record is a collection of data values (or data items, data fields).

3 Database Management Systems, R. Ramakrishnan and J. Gehrke3 File Descriptor 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 disk.

4 Database Management Systems, R. Ramakrishnan and J. Gehrke4 Files of Records Records are stored on disk blocks. –The blocking factor bfr for a file is the (average) number of file records stored in a disk block. Disk block Employee record bfr = 3

5 Database Management Systems, R. Ramakrishnan and J. Gehrke5 Files of Records A file can have – fixed-length records or – variable-length records.

6 Database Management Systems, R. Ramakrishnan and J. Gehrke6 Files of Records File records can be – unspanned o (no record can span two blocks) or – spanned o (a record can be stored in more than one block). Block spanned

7 Database Management Systems, R. Ramakrishnan and J. Gehrke7 Files of Records The physical disk blocks that are allocated to hold the records of a file can be contiguous, linked, or indexed. Indexed- Unix inode (index node) Linked-DOS

8 Database Management Systems, R. Ramakrishnan and J. Gehrke8 Fixed-length Records In a file of fixed-length records, all records have the same format. Usually, unspanned blocking is used with such files.

9 Database Management Systems, R. Ramakrishnan and J. Gehrke9 Variable-length Records Files of variable-length records require additional information to be stored in each record, such as separator characters and field types. Usually spanned blocking is used with such files.

10 Database Management Systems, R. Ramakrishnan and J. Gehrke10 3 Operations on Files Typical file operations include: –OPEN –FIND –FINDNEXT –READ –INSERT –DELETE –MODIFY –CLOSE –REORGANIZE –READ_ORDERED

11 Database Management Systems, R. Ramakrishnan and J. Gehrke11 4 Unordered Files Also called a heap or a pile file. New records are inserted at the end of the file.

12 Database Management Systems, R. Ramakrishnan and J. Gehrke12 Unordered Files 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.

13 Database Management Systems, R. Ramakrishnan and J. Gehrke13 Unordered Files Record insertion is quite efficient. Reading the records in order of a particular field requires sorting the file records.

14 Database Management Systems, R. Ramakrishnan and J. Gehrke14 5 Ordered Files Also called a sequential file. File records are kept sorted by the values of an ordering field.

15 Database Management Systems, R. Ramakrishnan and J. Gehrke15 Ordered Files Insertion is expensive: –records must be inserted in the correct order. –It is common to keep a separate unordered overflow (or transaction ) file for new records to improve insertion efficiency; –this is periodically merged with the main ordered file.

16 Database Management Systems, R. Ramakrishnan and J. Gehrke16 Ordered Files A binary search can be used to search for a record on its ordering field value. –This requires reading and searching log2 of the file blocks on the average, an improvement over linear search. Reading the records in order of the ordering field is quite efficient.

17 Database Management Systems, R. Ramakrishnan and J. Gehrke17 6 Hashed Files Static External Hashing Extendible Hashing Techniques

18 Database Management Systems, R. Ramakrishnan and J. Gehrke18 6.1 Static External Hashing The file blocks are divided into M equal-sized buckets, numbered bucket 0, bucket 1,..., bucket M-1. Typically, a bucket corresponds to one (or a fixed number of) disk block.

19 Database Management Systems, R. Ramakrishnan and J. Gehrke19

20 Database Management Systems, R. Ramakrishnan and J. Gehrke20 Hash Key and Hashing Function 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.

21 Database Management Systems, R. Ramakrishnan and J. Gehrke21 Collisions and Overflows 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. To reduce overflow records, a hash file is typically kept 70-80% full.

22 Database Management Systems, R. Ramakrishnan and J. Gehrke22

23 Database Management Systems, R. Ramakrishnan and J. Gehrke23 A Perfect Hash Function The hash function h should distribute the records uniformly among the buckets; otherwise, search time will be increased because of many overflow records.

24 Database Management Systems, R. Ramakrishnan and J. Gehrke24 Main disadvantages of static external hashing: Fixed number of buckets M is a problem if the number of records in the file grows or shrinks. Ordered access on the hash key is quite inefficient (requires sorting the records).

25 Database Management Systems, R. Ramakrishnan and J. Gehrke25 6.2 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: – extendible hashing, and – linear hashing.(not discussed here!)

26 Database Management Systems, R. Ramakrishnan and J. Gehrke26 Binary Representation The extendible hashing use the binary representation of the hash value h(K) in order to access a directory. –In extendible hashing the directory is an array of size 2 d where d is called the global depth.

27 Database Management Systems, R. Ramakrishnan and J. Gehrke27 Directories 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.

28 Database Management Systems, R. Ramakrishnan and J. Gehrke28

29 Database Management Systems, R. Ramakrishnan and J. Gehrke29 example A file with the following record ’ s sequence for insertion, build both a static hash structure and extendible hash structure, Record key h(K) binary of h(K) Record1 2369 1 001 Record2 3760 0 000 Record3 4692 4 100 Record4 4871 7 111 Record5 5659 3 011 Record6 1821 5 101 Record7 1074 2 010 Record8 7115 3 011 Record9 1620 4 100 Record10 2428 4 100 Record11 3943 7 111 Record12 4750 6 110 Record13 6975 7 111

30 Database Management Systems, R. Ramakrishnan and J. Gehrke30 Redistributing Records An insertion in a disk block that is full causes the block to split into two blocks and the records are redistributed among the two blocks. The directory is updated appropriately. Local depth d ’ is used to check for expanding the directory by 2, if d ’ < d when the bucket is full, no need for expanding; otherwise, doubling the directory for bucket space.

31 Database Management Systems, R. Ramakrishnan and J. Gehrke31 7 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)

32 Database Management Systems, R. Ramakrishnan and J. Gehrke32 SSN index 123 125 0x341 ﹕ Employee file on disk 0x341 123, Chang, … 125, Ordering field

33 Database Management Systems, R. Ramakrishnan and J. Gehrke33 Indexes as Access Paths One form of an index is a file of entries, which is ordered by field value The index is called an access path on the field

34 Database Management Systems, R. Ramakrishnan and J. Gehrke34 Indexes as Access Paths 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

35 Database Management Systems, R. Ramakrishnan and J. Gehrke35 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 o bfr= B div R= 512 div 150= 3 records/block –number of file blocks o b= (r/bfr)= (30000/3)= 10000 blocks

36 Database Management Systems, R. Ramakrishnan and J. Gehrke36 Example: For an index on the SSN field, assume the field size V SSN =9 bytes, assume the record pointer size PR=7 bytes. Then: –index entry size RI=(V SSN + PR)=(9+7)=16 bytes –index blocking factor bfrI o bfrI= B div RI= 512 div 16= 32 entries/block –number of index blocks b o bI= (r/bfrI)= (30000/32)= 938 blocks –binary search needs log 2 bI o log 2 bI= log 2 938= 10 block accesses

37 Database Management Systems, R. Ramakrishnan and J. Gehrke37 Example: This is compared to an average linear search cost of: –(b/2)= 10000/2= 5000 block accesses If the file records are ordered, the binary search cost would be: –log 2 b= log 2 10000= 14 block accesses

38 Database Management Systems, R. Ramakrishnan and J. Gehrke38 8 Types of Single-Level Indexes Primary Index Clustering Index Secondary Index

39 Database Management Systems, R. Ramakrishnan and J. Gehrke39 8.1 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 block

40 Database Management Systems, R. Ramakrishnan and J. Gehrke40

41 Database Management Systems, R. Ramakrishnan and J. Gehrke41 8.2 Clustering 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

42 Database Management Systems, R. Ramakrishnan and J. Gehrke42

43 Database Management Systems, R. Ramakrishnan and J. Gehrke43 8.3 Secondary Index Defined on an unordered data file Can be defined on a key field or a non-key field Includes one entry for each record in the data file; hence, it is called a dense index

44 Database Management Systems, R. Ramakrishnan and J. Gehrke44

45 Database Management Systems, R. Ramakrishnan and J. Gehrke45 example1 EXAMPLE 1: Suppose that we have an ordered file with r = 30,000 records stored on a disk with block size B = 1024 bytes. File records are of fixed size and are unspanned, with record length R = 100 bytes. The blocking factor for the file would be bfr = (B/R) 」 = (1024/100) 」 = 10 records per block. The number of blocks needed for the file is b = 「 (r/bfr) = 「 (30,000/10) = 3000 blocks. A binary search on the data file would need approximately 「 log 2 b = (log 2 3000) = 12 block accesses. Now suppose that the ordering key field of the file is V = 9 bytes long, a block pointer is P = 6 bytes long, and we have constructed a primary index for the file. The size of each index entry is Ri= (9 + 6) = 15 bytes, so the blocking factor for the index is bfr i = (B/Rj) 」 = (l024/15)= 68 entries per block. The total number of index entries r i is equal to the number of blocks in the data file, which is 3000. The number of index blocks is hence b i = (r j /bfr i ) 」 = (3000/68) 」 = 45 blocks. To perform a binary search on the index file would need 「 (log 2 b i ) = 「 (log 2 45) = 6 block accesses. To search for a record using the index, we need one additional block access to the data file for a total of 6 + 1 = 7 block accesses-an improvement over binary search on the data file, which required 12 block accesses.

46 Database Management Systems, R. Ramakrishnan and J. Gehrke46 example2 EXAMPLE 2: Consider the file of Example 1 with r = 30,000 fixed-length records of size R = 100 bytes stored on a disk with block size B = 1024 bytes. The file has b = 3000 j blocks, as calculated in Example 1. To do a linear search on the file, we would require b/2 = 3000/2 = 1500 block accesses on the average. Suppose that we construct a secondary index on a nonordering key field of the file that is V = 9 bytes long. As in Example 1, a block pointer is P = 6 bytes long, so each index entry is Rj = (9 + 6) = 15 bytes, and the blocking factor for the index is bfr j =(B/Rj) =(1024/15) = 68 entries per block. In a dense secondary index such as this, the total number of index entries rj is equal to the number of records in the data file, which is 30,000. The number of blocks needed for the index is hence bi = (rj/bfrj) = (30,000/68) = 442 blocks. A binary search on this secondary index needs log 2 bi = log 2 442 = 9 block accesses. To search for a record using the index, we need an additional block access to the data file for a total of 9 + 1 = 10 block accesses. A vast improvement over the 1500 block accesses needed on the average for a linear search, but slightly worse than the 7 block accesses required for the primary index.

47 Database Management Systems, R. Ramakrishnan and J. Gehrke47 9 Multi-Level Indexes 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

48 Database Management Systems, R. Ramakrishnan and J. Gehrke48

49 Database Management Systems, R. Ramakrishnan and J. Gehrke49 Multi-Level Indexes We can repeat the process, creating a third, fourth,..., top level until all entries of the top level fit in one disk block

50 Database Management Systems, R. Ramakrishnan and J. Gehrke50 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

51 Database Management Systems, R. Ramakrishnan and J. Gehrke51 Multi-Level Indexes 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 an ordered file

52 Database Management Systems, R. Ramakrishnan and J. Gehrke52 Multi-Level Indexes 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

53 Database Management Systems, R. Ramakrishnan and J. Gehrke53 10 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

54 Database Management Systems, R. Ramakrishnan and J. Gehrke54 B-Tree and B + -Tree data structures 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

55 Database Management Systems, R. Ramakrishnan and J. Gehrke55 Insertions 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

56 Database Management Systems, R. Ramakrishnan and J. Gehrke56 Deletions 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

57 Database Management Systems, R. Ramakrishnan and J. Gehrke57 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 higher capacity of search values) than the corresponding B-tree


Download ppt "Database Management Systems, R. Ramakrishnan and J. Gehrke1 File Organizations and Indexing Chapter 5, 6 of Elmasri “ How index-learning turns no student."

Similar presentations


Ads by Google