Group processing is one of the most common reporting requirements-an application summarizes multiple records and provides a group summary record. SQL and the Open Query File (OPNQRYF) command support a special function for grouping. Another traditional method is to use a sort, and summarize the data in a high level language (HLL) program. A less traditional method is to provide for an "in memory" summary (e.g., the use of arrays to accumulate by group). Loading an array for this type of work was described in "The Truth About RPG Performance Coding Techniques," MC, September 1993. The fastest solution shown in that article (except for a technique where the search argument itself could be used as the array index) used the RPG Alternate Lookup (RPGALTLKP) tool from QUSRTOOL. The tool provides standard code that uses a simple hashing algorithm to access an array element that is accumulating values. The tool avoids using the RPG LOKUP op code which takes a long time to accumulate multiple group summaries. RPGALTLKP is in the April 1994 update to QUSRTOOL. To receive this update, ask your SE to send a note to the QUSRTOOL node at RCHVMX2 in Rochester. (For more information, see "Tips for Managing the QUSRTOOL Library," MC, October 1993. ) In the set of tests shown in 1, the traditional solutions of OPNQRYF group processing (case 1) and an OPNQRYF sort with an HLL summarization (case 2) are compared against the RPGALTLKP tool (case 3). The three cases all produce the same result (a printed listing in group order with a count of the records in each group). Each test used no more than 1,000 groups (unique values). The record size was 200 bytes.
Group processing is one of the most common reporting requirements-an application summarizes multiple records and provides a group summary record. SQL and the Open Query File (OPNQRYF) command support a special function for grouping. Another traditional method is to use a sort, and summarize the data in a high level language (HLL) program. A less traditional method is to provide for an "in memory" summary (e.g., the use of arrays to accumulate by group). Loading an array for this type of work was described in "The Truth About RPG Performance Coding Techniques," MC, September 1993. The fastest solution shown in that article (except for a technique where the search argument itself could be used as the array index) used the RPG Alternate Lookup (RPGALTLKP) tool from QUSRTOOL. The tool provides standard code that uses a simple hashing algorithm to access an array element that is accumulating values. The tool avoids using the RPG LOKUP op code which takes a long time to accumulate multiple group summaries. RPGALTLKP is in the April 1994 update to QUSRTOOL. To receive this update, ask your SE to send a note to the QUSRTOOL node at RCHVMX2 in Rochester. (For more information, see "Tips for Managing the QUSRTOOL Library," MC, October 1993. ) In the set of tests shown in Figure 1, the traditional solutions of OPNQRYF group processing (case 1) and an OPNQRYF sort with an HLL summarization (case 2) are compared against the RPGALTLKP tool (case 3). The three cases all produce the same result (a printed listing in group order with a count of the records in each group). Each test used no more than 1,000 groups (unique values). The record size was 200 bytes.
Test Results The SQL and OPNQRYF group functions cause an access path to be built if none exists over the group field. Keyed sequential processing is used to access all records in the file. Although only the summary records are passed to the HLL program, the solution is slow because of the amount of disk access required for keyed processing. In test case 1, no access path existed for the group field. If an access path had existed on the group, the results of case 1 in each test would have been better-but not by a margin large enough to make a difference in the conclusion. The use of OPNQRYF to sort the data provides a better solution than the OPNQRYF group function. Sorting avoids all the random arm movement to access every record. The best solution is the RPGALTLKP tool. This avoids the sort and still lets the file be processed in arrival sequence. The RPGALTLKP tool is a performance-oriented hashing technique. It generates a random number based on a control group's fields. The random number is used as an index to an array that stores the group totals. Because the RPGALTLKP algorithm uses no divide or multiply operations, it is very efficient. The more unique the values of the control group, the better the technique will perform. The tool has a limit of 2,000 groups. In this test, the hashing algorithm used by RPGALTLKP does not produce any duplicates (only unique values exist). In a real situation, this would not be realistic and the performance would be somewhat slower. However, the solution is so much better than sorting that, even with a reasonable number of duplicate hashing values, RPGALTLKP would still produce the best results. This test only summarizes a count of the records in each group. The RPLALTLKP tool sorted the array before printing to provide the correct sequence for the report. You can accumulate other fields and take percentages (including a percentage of each group's part of the total). You can sequence the array on the group field, on any field you are accumulating, or on one of the percentage fields you have created. The tool is reasonably flexible.
When to Use RPGALTLKP The key decision with the RPGALTLKP tool is to determine what produces the best hashing algorithm for your data. You want to choose the positions of your control group field(s) that will produce the most unique values. For example, if your control field is a 5-digit, sequential number, you would want to use positions 3, 4, and 5 rather than 1, 2, and 3, since the low-order positions are more likely to be different than the high-order positions. The Analyze RPG Lookup (ANZRPGLKP) command, shown in 2 assists you in determining what will produce the best results. The ANZRPGLKP command is automatically created when you create the RPGALTLKP tool. It asks you to identify the fields and byte positions within the fields that will provide the best hashing solution and produces spooled output that resembles 3. By default, the command compares a traditional look-up method of loading an array versus the RPGALTLKP approach. You are concerned with the figure that describes the average compares per record for the alternate look-up technique. Any value that approaches 1.0000 shows that the fields and byte positions you specified will produce a fast solution. To use RPGALTLKP, copy the code from the TAARPGFR3 member in file QATTRPG in library QUSRTOOL. It contains instructions (in the form of special comments) to modify the code to fit your needs. Once you have a working program, you can get rid of all the special comments with another TAA tool, RMVSRCCMT. If RPGALTLKP fits your application need, you certainly want to use it from a performance viewpoint.
When to Use RPGALTLKP The key decision with the RPGALTLKP tool is to determine what produces the best hashing algorithm for your data. You want to choose the positions of your control group field(s) that will produce the most unique values. For example, if your control field is a 5-digit, sequential number, you would want to use positions 3, 4, and 5 rather than 1, 2, and 3, since the low-order positions are more likely to be different than the high-order positions. The Analyze RPG Lookup (ANZRPGLKP) command, shown in Figure 2 assists you in determining what will produce the best results. The ANZRPGLKP command is automatically created when you create the RPGALTLKP tool. It asks you to identify the fields and byte positions within the fields that will provide the best hashing solution and produces spooled output that resembles Figure 3. By default, the command compares a traditional look-up method of loading an array versus the RPGALTLKP approach. You are concerned with the figure that describes the average compares per record for the alternate look-up technique. Any value that approaches 1.0000 shows that the fields and byte positions you specified will produce a fast solution. To use RPGALTLKP, copy the code from the TAARPGFR3 member in file QATTRPG in library QUSRTOOL. It contains instructions (in the form of special comments) to modify the code to fit your needs. Once you have a working program, you can get rid of all the special comments with another TAA tool, RMVSRCCMT. If RPGALTLKP fits your application need, you certainly want to use it from a performance viewpoint.
Jim Sloan is president of Jim Sloan, Inc., a consulting company. Now a retired IBMer, Sloan was a software planner on the S/38 when it began as a piece of paper. He also worked on the planning and early releases of the AS/400. In addition, Jim wrote the TAA tools that exist in QUSRTOOL. He has been a speaker at COMMON and the AS/400 Technical Conferences for many years.
Lab Notes: Improve Performance of Group Processing
Figure 1 Performance Test Results
CPU Total Job Results on 1,000 records Seconds Seconds Case 1 OPNQRYF group processing 14.8 30 Case 2 OPNQRYF sort and HLL summarization 9.4 23 Case 3 RPLALTLKP Tool 6.5 15 CPU Total Job Results on 10,000 records Seconds Seconds Case 1 OPNQRYF group processing 75.4 153 Case 2 OPNQRYF sort and HLL summarization 44.0 62 Case 3 RPLALTLKP Tool 22.3 32 CPU Total Job Results on 50,000 records Seconds Seconds Case 1 OPNQRYF group processing 411.2 1166 Case 2 OPNQRYF sort and HLL summarization 211.4 260 Case 3 RPLALTLKP Tool 85.7 102
Lab Notes: Improve Performance of Group Processing
Figure 2 The Analyze RPG Lookup Command
UNABLE TO REPRODUCE GRAPHICS
Lab Notes: Improve Performance of Group Processing
Figure 3 The Analyze RPG Lookup Output
11/09/94 19:34:12 MCPGMR ANZRPGLKP - Analyze RPG Alternate LOKUP Method File name - DSPOBJDP Library - $SHARIC Member - DSPOBJDP Field 1 - ODOBSZ First pos - 7 Field 2 - ODOBSZ Second pos - 8 Field 3 - ODOBSZ Third pos - 9 Number of records requested - 10,000 Normal lookup request - *YES Number of records in the file - 125 Data values found in the records Unique values - 66 Duplicate values - 59 Records read - 125 Normal load of array and LOKUP operation Total compares - 4,054 Average compares per record - 32.4320 Alternate LOKUP method Total compares - 125 Average compares per record - 1.0000 The discussion of how to use the tool is in member RPGALTLKP for file QATTINFO in QUSRTOOL. The sample source to be copied is in member TAARPGFR3 for file QATTRPG in QUSRTOOL.
LATEST COMMENTS
MC Press Online