Q: Is it faster to use the Open Query File (OPNQRYF) command to read a file and select a number of records, or to read the file in an RPG program and select the records there? I get the impression that if you use OPNQRYF, you are essentially processing some records twice-once when they are read by OPNQRYF and again when the RPG program using the records processes them. Is this true?
A: It really depends on your application and how you are processing the records. OPNQRYF will beat RPG reads hands-down when you are processing records where the selection is variable. This means that you can build an OPNQRYF request dynamically and execute it-and your RPG program doesn't have to worry about the selection. If you are processing a specific selection all the time, a logical file might be a better approach.
OPNQRYF is faster because it selects the records at the microcode level. No data is "brought" up to the application program level. This saves time. When you read a record in RPG, it must be brought all the way into your program before any decision can be made on whether you need it.
I use OPNQRYF, SQL and dedicated logicals. Which is the best really depends on what I am trying to do. For example, if I have a file with 100,000 records and I need to execute a report based on input criteria from the end user, I probably would use OPNQRYF if record selection is based on fields that are not key fields. If the selection is based on a key field, then it might be faster to use a logical instead.
Here's my general rule of thumb: If the number of records is small-that is, if I know I am only going to read a few records in the RPG program-I will not use OPNQRYF. However, if the number of records in the file is large and my selection is small, I will use OPNQRYF. Each tool provided is valuable. Learn each one and then use it when the application design makes it the most advantageous.
LATEST COMMENTS
MC Press Online