An indexed file is a file in which each record includes a primary key. To distinguish one record from another, the value of the primary key must be unique for each record. Records can then be accessed randomly by specifying the value of the record's primary key. Indexed file records can also be accessed sequentially.
As well as a primary key, indexed files can contain one or more additional keys known as alternate keys. The value of a record's alternate key(s) does not have to be unique.
To define a file as indexed, specify ORGANIZATION IS INDEXED in the SELECT clause for the file in your COBOL program. You must also specify a primary key using the RECORD KEY clause:
select idxfile assign to "idx.dat" organization is indexed record key is idxfile-record-key.
Most types of indexed file actually comprise two separate files: the data file (containing the record data) and the index file (containing the index structure). Where this is the case, the name that you specify in your COBOL program is given to the data file and the name of the associated index file is produced by adding an .idx extension to the data file name. You should avoid using the .idx extension in other contexts.
The index is built up as an inverted tree structure that grows as records are added.
With indexed files, the number of disk accesses required to locate a randomly selected record depends primarily on the number of records in the file and the length of the record key. File I/O is faster when reading the file sequentially.
We strongly recommend that you take regular backups of files of all types. However, with indexed files there are events such as media corruption that can result in only one of the two files becoming unusable. If you do lose an index file, use the Rebuild utility to recover the index from the data file and so reduce the time lost due to a failure. For more information, see the topic Rebuild.