1 / 57

File Organizations and Indexing

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 Files of Records.

ebony
Download Presentation

File Organizations and Indexing

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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. 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. 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. 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 bfr = 3 Employee record

  5. Files of Records • A file can have • fixed-length records or • variable-length records.

  6. Files 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). spanned Block

  7. 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. 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. 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. Typical file operations include: OPEN FIND FINDNEXT READ INSERT DELETE MODIFY CLOSE REORGANIZE READ_ORDERED 3 Operations on Files

  11. 4 Unordered Files • Also called a heap or a pile file. • New records are inserted at the end of the file.

  12. 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. Unordered Files • Record insertion is quite efficient. • Reading the records in order of a particular field requires sorting the file records.

  14. 5 Ordered Files • Also called a sequential file. • File records are kept sorted by the values of an ordering field.

  15. 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. 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. 6 Hashed Files • Static External Hashing • Extendible Hashing Techniques

  18. 6.1 Static 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.

  19. 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 bucketi, where i=h(K), and h is the hashing function. • Search is very efficient on the hash key.

  20. 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.

  21. 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.

  22. 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).

  23. 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!)

  24. 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 2d where d is called the global depth.

  25. 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.

  26. 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

  27. 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.

  28. 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)

  29. Employee file on disk index SSN 0x341 123 0x341 123, Chang, … 125 ﹕ 125, Ordering field

  30. Indexes as Access Paths • One form of an index is a file of entries <field value, pointer to record>, which is ordered by field value • The index is called an access path on the field

  31. 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

  32. 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

  33. Example: • 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 • bfrI= B div RI= 512 div 16= 32 entries/block • number of index blocks b • bI= (r/bfrI)= (30000/32)= 938 blocks • binary search needs log2bI • log2bI= log2938= 10 block accesses

  34. 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: • log2b= log210000= 14 block accesses

  35. 8 Types of Single-Level Indexes • Primary Index • Clustering Index • Secondary Index

  36. 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

  37. 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

  38. 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

  39. 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 「 log2b = (log23000) = 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 ri is equal to the number of blocks in the data file, which is 3000. The number of index blocks is hence bi = (rj/bfri) 」= (3000/68) 」 = 45 blocks. To perform a binary search on the index file would need 「(log2bi) = 「 (log245) = 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.

  40. 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 bfrj =(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 log2bi = log2 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.

  41. 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

  42. 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

  43. 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

More Related