Sub topic 9
Secondary Indexes on Key and Non-Key Fields
Assume you know records, disk blocks, blocking factor, sorted and heap files, and the difference between a primary and a clustering index. This chapter focuses on an index built on a field that is not the field used to order the data file.
What Makes an Index Secondary?
A secondary index is an access path built on a search field that does not determine the physical order of the data file. The data file may be a heap file or may be sorted by some other field.
The indexed field can be:
- A key field, whose values are unique.
- A non-key field, whose value can occur in many records.
A secondary index is usually dense. A dense index has an index entry for every record, or for every distinct search value with pointers to all matching records. A sparse index is generally unsafe because the data file is not ordered by the secondary field.
| Indexed field | Duplicate values? | Typical index entry |
|---|---|---|
| Secondary key field | No | `(key, record pointer)` |
| Secondary non-key field | Yes | `(value, list of record pointers)` |
A record pointer identifies the block and position of a record. The index entries are sorted by the indexed field, even though the data records are not.
Secondary Index on a Key Field
Suppose a data file is sorted by EmployeeID, but queries often search by the unique field NationalID. A secondary index on NationalID contains one entry for each record:
NationalID Record pointer
------------ --------------
N1042 (block 7, slot 3)
N1189 (block 2, slot 5)
N2031 (block 9, slot 1)A lookup first searches the secondary index. It then follows the pointer to the data record. If the secondary index has multiple levels, the cost includes the index traversal and the final data-block access.
For a unique secondary key, the search cost is approximately:
index-block accesses + 1 data-block access
The final access is usually necessary because the index stores a pointer rather than the complete record.
Secondary Index on a Non-Key Field
A non-key field has duplicates. For example, many employees may belong to the same department. The index must represent every matching record:
Department Record pointers
----------- -------------------------------
Sales (block 2, slot 1), (block 8, slot 4)
Support (block 1, slot 2), (block 5, slot 3), (block 9, slot 2)The pointer list can be stored directly in the index entry or in separate blocks called an overflow or posting list. The second design avoids making index entries extremely large.
A lookup for one department may therefore require many data-block accesses. If matching records are scattered across the file, those accesses can be expensive even when the index search itself is fast.
Costs and Maintenance
A secondary index improves read performance but adds storage and update work.
- Inserting a record requires adding an index entry.
- Deleting a record requires removing its pointer.
- Updating the indexed field can require removing an old entry and adding a new one.
- A non-key index may need to create or remove pointer-list blocks.
- Every matching pointer may lead to a separate data-block access.
A secondary index on a unique field is often efficient for exact-match queries. An index on a non-key field is useful when the query returns a relatively small fraction of the file. If most records match, a sequential scan may be cheaper.
Exercise
A file contains 10,000 records and is sorted by StudentID. A dense secondary index is built on the unique field Email. How many data records can one exact Email lookup return, and why is the index dense?
Show answer
It can return at most one record because Email is unique. The index is dense because the data file is not ordered by Email, so each indexed record needs a direct pointer.