File Operations for Secondary Indexes
Record Addition
- When a secondary index is used, adding a record involves updating the data file, the primary index and the secondary index.
- The key field in secondary index is stored in canonical form and held to a fixed length: This means sometimes they are truncated.
- The secondary index may contain duplicates. Duplicate keys are grouped together and sorted (using the primary key) within the group.
Record Deletion
- Removing a record from the data file would mean removing not only the corresponding entry in the primary index but also all of the entries in the secondary indexes that refer to this primary index entry.
- Another approach is to leave intact the references to the deleted record that exist in the secondary key indexes. Searches starting from a secondary index that lead to a deleted record are caught when we consult the primary key index.
†The disadvantage is the large space taken in RAM by these deleted records (but not removed) in secondary indexes.
Record Updating
Since secondary indexes do not refer to the physical location of of the records in the data file, record updating will not affect secondary index in a direct way. Three possible situations:
- Update changes the secondary key:
may have to rearrange and sort secondary index.
- Update changes the primary key:
may have to rearrange and sort primary index, but very few changes are needed for the secondary index (update some reference fields of secondary index).
- Update confined to other fields:
no changes necessary to primary nor secondary index.