Alternate Key Indices |
![]() ![]() ![]() |
An alternate key index provides a method to access data file other than by the primary key (record id).
Consider a file holding information about orders with, for example, 100000 records in it. For simplicity, assume that these records are made up of 10 orders from each of 10000 customers. To locate all the orders placed by a specific customer would require all 100000 records to be processed to find the 10 that we want.
Using an alternate key index on the customer number field of the order records, QM can look up the customer in the index and then go directly to the 10 order records.
An alternate key index is created using the CREATE.INDEX command. This defines the index content but does not populate it. The BUILD.INDEX command builds the actual index and activates it. From that point forwards, QM will maintain the index automatically and the query processor will use it automatically. No changes are required to application software.
The functions of the CREATE.INDEX and BUILD.INDEX commands are combined in the MAKE.INDEX command.
Once an index has been built, it is maintained totally automatically by QM such that it is impossible to write or delete a record without the corresponding index updates being applied. The query processor will also use the index totally automatically where it appears relevant.
Indices can be built on real data stored in the file (dictionary D-type or A/S-type without a correlative) or on calculated values (dictionary I-type or A/S-type with a correlative). When using calculated values, it is essential that the expression relies only on data from the file to which the index applies and is not time variant. Thus an index using data retrieved from another file using the TRANS() function, a T-conversion or a subroutine that performs a read will fail because the index will not be updated if the remote file is modified. Similarly, an index built using a calculation that uses the date or time (age calculated from a date of birth, for example) will fail because the expression does not always return the same output value for the same input.
These are both examples of the one and only rule that determines whether an index based on a calculated value will work: The virtual attribute expression must always return the same value when evaluated for the same data record.
For an index to be effective, each entry in the index should lead to a very small proportion of the data in the file. Index entries that lead to very large numbers of records are less effective and may also be costly to access or update. The worst case of this is indexing on a simple yes/no value.
A file may have up to 32 indices. The more indices there are, the longer it will take to update a record in the data file though this should be outweighed by the advantage of being able to use the indices in queries. Also, it should be remembered that indexing on a multivalued field may require many index entries to be updated when writing a data record.
Indices can be deleted using the DELETE.INDEX command if they are no longer wanted. A report of any or all indices on a file can be produced with the LIST.INDEX command.
Programmers can gain access to the index itself using the QMBasic SELECTINDEX statement and advanced index scanning operations can be performed using SELECTLEFT, SELECTRIGHT, SETLEFT and SETRIGHT.
|