Guru: Why And How Not to Use The Aretha Franklin I/O Method
March 4, 2019 Ted Holt
The Aretha Franklin I/O Method is still used heavily in RPG shops even though a better method has existed for decades. In the following paragraphs, I explain the Aretha Franklin I/O Method, tell you why you should not use it, and show you the superior method.
First, let me give credit where credit is due. Although I had been using the Aretha Franklin I/O Method since my System/34 days, I never knew it by that name. Then Dan Cruikshank (now retired) of IBM informed me of this terminology. Here’s how it works:
Assume an RPG program that needs data from several indexed database files. How does you accomplish that? Simple. You sequentially READ one file and CHAIN, CHAIN, CHAIN to get the data from the other files.
Let’s take a simple example. Suppose you work in a factory and an accounting person whom you serve needs a program that will show them what the customers have on order with us. You need data from four files:
- The order detail file, AGSALESD, has one record for each item the customers have ordered.
- The order header file, AGSALESH, has one record for each customer order. We need this file in order to retrieve the date an order is due to ship.
- The customer master file, AGCUST, has one record for each customer. We need this file in order to retrieve a customer’s name.
- The item master file, AGITEMS, has one record for each item we sell. We need this file in order to retrieve the cost and description of an item.
Here is the code to retrieve this information using the Aretha Franklin I/O Method.
Fagsalesd if e k disk Fagsalesh if e k disk Fagcust if e k disk Fagitems if e k disk read detail; chain (OrderID) header; chain (ItemID) item; chain (CustID) cust;
Great. It does its job. However, it has its shortcomings. Here are two that come to mind:
- What if the user asks you to change the sort order? If the new sort field is (fields are) in the order detail file, no problem. You can read an appropriately-keyed logical file or use Open Query File (OPNQRYF). But what if the new sort field is (fields are) in another file? What if the sort fields are in two or more files?
- What if your shop changes the definition of a field that you’re not even using in this program or adds a new field to a physical file? To avoid a level check, you have to recompile all the programs that use the file in which that field is found.
I much prefer to use SQL. It allows me to join the files, presenting all the necessary fields as if they were stored in one record format.
select d.OrderID, h.Date, d.ItemID, c.Name, i.Cost, i.Description from agsalesd as d left join agsalesh as h on d.OrderID = h.OrderID left join agcust as c on h.CustID = c.CustID left join agitems as i on d.ItemID = i.ItemID order by d.OrderID, d.LineNumber
If someone asks me to change the sort order, I reply “no problem,” and modify the ORDER BY clause.
select . . . order by c.Name, d.ItemID
If a file definition changes, and none of the changes are to fields that I use in this program, I don’t do anything to this code. I don’t even have to recompile.
Whatever you do, DO NOT implement the Aretha Franklin I/O Method in SQL, as seen below:
I won’t say that you should never use the Aretha Franklin Method, but that’s only because I avoid the words always and never. Maybe there are situations where CHAIN, CHAIN, CHAIN beats a join with SQL. If so, I don’t know what they would be.
All I know is that I’ve given up the Aretha Franklin I/O Method, and I advise you to do the same. It’s OK. Aretha wouldn’t mind.
The suggested method does indeed avoid recompiles, but does not explain away the rather significant overheads of repeated chains, or the repeated queries which is even worse.
Level checks are sometimes a useful indicator that something has been forgotten though, and generally repurposing or respecifying columns indicates that very fundamental changes are afoot (or a bodge is underway!)
Ted, what is the alternative to using the Aretha Franklin Method in SQL? I didn’t see in the article a recommendation and I think that would be helpful for people. If you are wanting to select records from a PF and read through the records how would you accomplish that?
Matt, the alternative is to use a SELECT with JOIN. Access all the files with one SELECT.
Hi Ted: Let me give you a little R-E-S-P-E-C-T for your article. Never heard about the Aretha Franklin method, I like the reference.
Question regarding the subsequent SQL statements, when doing a complex select w/multiple joins over large files, would doing some single SELECT “chains” when processing the fetched row be better? Or just put all the SELECTs/JOINs/fields in one large SQL statement? I’ve done both. Wondering if there’s a rule of thumb?
Hope all is well.
Three separate SELECT’s is even worse than what Ted points out above shouldn’t be done, which is three SELECT’s in one statement. SQL is a batch language. The irony here is that this isn’t a batch of same processing on multiple records, but we have to act like it is. So the indexing has to be sufficient to join the three files so the designated fields are selected from the three records in the join. If definitions change that affect CHAIN keys, well they also affect SQL index keys. It’s just that the SQL engine will create even more massive indexes because of those changes. This is supposed to not matter anymore because IBM likes selling DASD and CPU’s and companies like getting rid of programmers. Win win.
There are many design and performance hurdles facing the programmer migrating from native IO to SQL. I have seen some pretty crazy SQL DB designs that IBM i shops tried and failed to migrate to. On the other hand, a programmer can simply write RPG that uses native IO and returns result sets to web apps and deploy the RPG as a stored proc. Instead of change for the sake of change, he can give the users better screens. It seems a far more productive use of his time and talent.
I don’t suppose anyone knows why it’s called the Aretha Franklin IO Method?
Chain Chain Chain are lyrics in Aretha’s song Chain of Fools.
There’s nothing magical going on, Ted. Everything has tradeoffs. The gee it just works is accomplished with massive indexes created by the SQL engine and the processing involved. In large operations SQL people bog things down badly. That’s not a conjecture, that’s an observation. At least for awhile longer RPG native IO will continue to be a higher performance alternative to everyone else. After that, only SQL is left. Then someone will have to reinvent high performace computing.
SQL will use the same indexes used by native RPG IO (provided they are applicable). If you see performance issues, then turn to index adviser to create the necessary indexes. You’ll be surprised how fast (and efficient) SQL can be.
The part I dislike is the tedium of selecting the individual fields “into” fields (or a DS) defined in the program.