Using a Logical File

Logical files are paths the system can use to access the records in a physical file by using a different sequence and optionally selecting or omitting some records.


A logical file doesn't actually contain any records. The logical file only contains pointers to the records in the physical file. Therefore, when you say that the logical file alters the sequence of the records, you are really saying that the logical file presents the records in a different sequence. This is why logical files are usually referred to as different "views" of the database.

If you use a logical file for sorting and/or selecting or omitting records, the logical file uses a fixed-sort sequence and fixed criteria for selection and omission. The specifications are coded in the DDS in the form of key fields or constants that you cannot change after the logical file is created. If you need flexibility, you should consider using Open Query File (OPNQRYF) instead of logical files.

Specifying Sequence

To change the arrival sequence of the records, you must create a logical file whose key fields describe the order in which you want to process the physical file's records. For example, if the Inventory Transaction History file has two fields named ITWHSE (the warehouse number) and ITITEM (the item number), you should use these two fields as the keys to the logical file to sort the physical file by warehouse number and item number.

The key can be specified in descending sequence by adding the DDS keyword DESCEND. That keyword seems to satisfy the requirement for the second report described in the opening paragraphs of this chapter. To sort the Inventory Transaction History file in descending order of transaction value, you would specify a key field of ITTVAL (assuming that is the name of the transaction value field) with DESCEND. Of course, if two transactions on that file have identical values, the system will not know what order to put those in.

Including and Omitting Records

Logical files also are capable of specifying which records to include and which records to omit from the view. For more information about coding, see Chapter 20.

CRTLF Options and Performance

The process of creating a logical file is described in Chapter 20. Review the subheading that deals with the Create Logical File (CRTLF) command and pay particular attention to the MAINT and RECOVER parameters.

These parameters, particularly MAINT, have an impact on system performance. As you continue to create more and more logical files over the same physical file, the system starts to slow down if MAINT (*IMMED) has been specified on all of them. The more logicals you have with MAINT(*IMMED), the more work the system has to do when records are added, changed, or deleted from the physical file.


You should use MAINT(*IMMED) with logical files you use in interactive jobs or those that have unique keys (DDS keyword UNIQUE). The other two options, MAINT(*REBLD) and MAINT(*DLY), maintain the access path only when the logical file is used. The difference between the two is that *REBLD forces the system to rebuild the access path for the entire logical file, whereas *DLY makes the system update the access path for the records that have been added, changed, or deleted since the last time the logical file was opened. You should not use the *REBLD and *DLY options with logical files that will be used for interactive jobs or end users will likely experience unnecessary delays while processing.

*DLY therefore is preferable over *REBLD for those files you don't need to open very frequently. However, when the number of records that have been added, changed, or deleted since the last open exceeds 10 percent of the total, the system rebuilds the entire access path (*REBLD-style) even if *DLY is specified. For example, you could have a logical file with an access path for 1,000 records and MAINT(*DLY). You opened it last week but, since then, you have added 250 records, which is 25 percent of the size it had the last time you opened it. When you open the file today, the system rebuilds the entire access path (for all 1,250 records), and treats the file as if it were created with MAINT(*REBLD).

Using the Logical File

Using logical files in your application programs is very easy. Code the name of the logical file instead of the name of the physical file and define the file as having keyed access. Your application program then reads the file by key.

To the application program, the logical file appears to contain all the data records. You don't have to reference the physical file anywhere.

IBM i5/iSeries Primer(c) Concepts and Techniques for Programmers, Administrators, and Sys[... ]ators
IBM i5/iSeries Primer(c) Concepts and Techniques for Programmers, Administrators, and Sys[... ]ators
Year: 2004
Pages: 245 © 2008-2017.
If you may any questions please contact us: