Home > AS/400 Tips > iSeries programmer tips > Making the most of RPG data handling on IBM i
iSeries 400 Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

ISERIES PROGRAMMER TIPS

Making the most of RPG data handling on IBM i


Andrew Borts, Contributor
07.10.2009
Rating: -4.00- (out of 5)


iSeries news and advice
Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


Andrew Borts

Today we're obsessed with getting new techniques into our programs as fast as we can. They're cool and neat, we have to do it. On the IBM i, programmers are migrating to using only SQL in their programming – the downside is that if you want the system to handle like a Ferrari, you need to talk to the gas pedal directly.

What raw RPG programming has going for it is the speed in which it can read a file. Millions of records can be accessed in a short period of time using simple methods we all grew to know in the old days. I'd like to help you understand how operations work within an RPG program so you can decide which method you need to use.

Primary file programs are fast to program -- you can do wonderful things with a five line program. For example, to do the following task "Read all records in TESTFILE, and copy all records with an "A" in FIELD5 into "NEWFILE" (assuming all fields in NEWFILE are exactly the same) would look like:


Click on image for larger version

The brevity of this code is due to the built in functionality of the "Primary" file from yesteryear that allows RPG to handle a great deal of data all at once. Also note that this file is being read via the order of appearance of the records, not where they are located per the index of the file.

Speeding through data with indexing
The advantage of the built in functionality is that RPG can handle data faster than other options. We can position randomly in the file using the powerful built-in indexing. This OS level functionality gives the ability to randomly traverse the indexes and deliver data quickly. It's this sort of index skipping that allows you to position to areas in the file you'd rather be in – kind of like lifting the needle of a record player (for you old folks). You must also strategically create your keys (or indexes) for the file so you can utilize them as many times as you can. Who wants to create an index for a single purpose? It's why I discourage "select/omit" indexes for single purpose reports or functions.

Looking at the following program, you'll see where the speed can be lost – after the explanation of how it's working. We'd like to read through 1,000 records relating to the key "ATSALOT".


Click on image for larger version

In this case, the system is reading from an index of ascending values for Field 2. Using the "CHAIN" operation – the system literally says "I only want one!" so the buffer is only grabbing one record. The iSeries has "SMART" caching where the system predicts the part of the file you are working on, and using "CHAIN" causes the system to only get one record at a time. CHAIN is a combination of "SETLL" or set lower limits, and "READEQ" or get one record that is equal to that specific key for the record that is found at that specific index position. This isn't going to win races when trying to read a mass amount of records (say 10,000 to 100,000 and beyond), but is fine for loading records for a subfile or 12 to 20 records. So, what would we need to do to increase this efficiency? Well, we could use the following combination to do exactly the same thing:


Click on image for larger version

Why? In this case, we positioned in the file to the first record and then used READ instead of READEQ or CHAIN. Chain and READEQ do essentially the same thing in this example. READEQ would utilize where it's positioned in the file, but here we have 1,000 consecutive numbers. There's slightly more code processing in the program, but remember I/O 100 times slower than code when processing. So even though the program is literally doing the work of the READEQ to process the records, the program is using READ. The system is now in a "Hey this person wants to read a lot of records… So, I'm going to buffer a lot of records" mode. The next time it asks for a record to process, READ will look at memory where the records may have been read.

Could this have been processed using SQL? Certainly. But SQL has it's own nuances. When I process anything using SQL, I make sure to utilize the indexes properly, or else the system generates those indexes on the fly after every use. Also make sure that you've created your least complex index first. This way, if the system can utilize the index from the initial less complex index, the system will use less storage for the index and find records quicker, etc.

So, what does this teach us? Not all programs are as they seem. When you look at older programs, and you see the above technique, think twice before re-writing to use SQL for the I/O, unless you need to have late binding (i.e. to avoid level problems).

ABOUT THE AUTHOR: Andrew Borts is webmaster at United Auto Insurance Group in North Miami, Fla. He is a frequent speaker at COMMON and is past president of The Southern National Users Group, an iSeries-AS/400 user group based in Deerfield Beach, Fla.

Rate this Tip
To rate tips, you must be a member of Search400.com.
Register now to start rating these tips. Log in if you are already a member.




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google



RELATED CONTENT
RPG iSeries programming
Enhancing RPG with external SQL stored procedures
Introduction to SQLRPGLE on IBM i: Making a report
IBM i shop boosts online sales with RPG-based Web platform
Migrating from RPG to EGL on IBM i
Allow access to data from a stored procedure result set using COBOL or RPG
EGL Rich UI on IBM i: Do you Dojo?
Programming for the Web on the IBM i, what is possible
A taste of COMMON: ILE, IBM releases, Web applications and new products
Documenting nested program structures on the AS/400
How to: Sort arrays using RPGIV

iSeries SQL commands and statements
Enhancing RPG with external SQL stored procedures
Tracking data changes on IBM i with triggers
Introduction to SQLRPGLE on IBM i: Making a report
When is the YES option for 'reuse deleted files' function the best choice?
Monitoring members 'stuck' within a physical file on an EDI system
Creating a host variable of the 'where in' statement in SQL
Choose which column names are returned via ODBC when working with DB2 files
SQL server error message -321
Convert a numeric physical file to a character in SQL without leading zeros
Inserting data from a CTE into a file in SQL
iSeries SQL commands and statements Research

iSeries programmer tips
Enhancing RPG with external SQL stored procedures
Tracking data changes on IBM i with triggers
Introduction to SQLRPGLE on IBM i: Making a report
Implementing a browser interface in COBOL: Displaying database fields
Taking advantage of CL advancements, starting with V5R3
TAATOOL: Useful tools for programmers on IBM i
Implementing a browser interface in COBOL: Creating your graphic Web page
Implementing a browser interface in COBOL: Getting started
Groovy programming on IBM i
EGL Rich UI on IBM i: Do you Dojo?

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
Report Program Generator  (Search400.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



iSeries Security - Security Tools, Physical Security and System Security
HomeNewsTopicsITKnowledge ExchangeTipsBlogsAsk the ExpertsMultimediaWhite PapersProducts
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 1999 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts