Skip to content

AS/400 performance tuning: batch jobs, SQL and indexes on IBM i

B2Bnetwork team · · 6 min

IBM i, RPG and mainframe

In brief

A slow IBM i rarely means an underpowered server. Most often the cause is a handful of SQL queries without a good index, batch programs that read files record by record, or jobs competing for the same memory pool. Start by measuring: Collection Services, the list of active jobs and the SQL plan cache show where the system loses time. Then fix the most expensive queries and indexes using the index advisor, and verify every change with a test on a copy of production data.

Measure first, change second

On IBM i it is easy to tune by feel: raise a job's priority, add memory, add an index because "it will probably help". Without measurement you do not know whether a change helped, and sometimes it hurts other jobs. So the first step is to collect data from the period when the problem occurs.

SymptomWhere to lookTypical cause
The night batch finishes after the business opensjob history, step times in the schedulerone step grows with the data, jobs wait for each other
5250 screens respond slowly at peak hoursWRKACTJOB, Collection Servicesa memory pool shared with batch jobs, record locks
A report from Excel or BI "freezes" the systemQZDASOINIT jobs, the SQL plan cachea full scan of a large table, a missing index
One job eats the CPUACTIVE_JOB_INFO, Visual Explaina query with a function on a column in WHERE, a poor join order
Slow writesjournals, commitment controla commit after every record, too many indexes on a table

Collection Services gathers performance data in the background, and the tools in IBM i Access Client Solutions let you browse it by job, memory pool and disk. For a quick look, an SQL query is enough:

SELECT job_name, subsystem, cpu_time, total_disk_io_count, sql_statement_text
  FROM TABLE(QSYS2.ACTIVE_JOB_INFO(DETAILED_INFO => 'ALL')) AS j
 ORDER BY cpu_time DESC
 FETCH FIRST 20 ROWS ONLY;

Batch jobs: the night run

The night run is usually a chain of CL and RPG programs started from the scheduler. It grows with the data, so overrunning the window is rarely sudden: for months it finished at 5:30, now it finishes at 7:10. The most common causes and fixes:

  1. Record-at-a-time processing. Replacing a READ and CHAIN loop over millions of records with one SQL statement (an UPDATE with a subquery, an INSERT with a SELECT) often cuts the step by an order of magnitude, because the database chooses the access path itself.
  2. Jobs waiting for each other for no reason. Independent steps (for example recalculating stock for different plants) can run in parallel in separate job queues.
  3. A shared memory pool. Batch and interactive jobs in one pool push each other's data out of memory. A separate subsystem and pool for batch stabilize run times.
  4. Commitment control. Committing after every record slows writes. Committing every few thousand records, with a restart point, is usually enough.
  5. Work files. Deleting and recreating temporary files in every step costs time. Clearing their contents or using SQL temporary tables is often faster.

Job and queue planning is covered in CL on IBM i, and the division of system resources in the IBM work management documentation.

SQL and the optimizer

Db2 for i optimizes SQL queries on its own, but it needs good indexes and queries it is able to optimize. The plan cache stores query plans along with runtime data: how many times they ran and how long they took. It is the easiest place to pick the queries to fix: start with the largest total time, not the longest single run.

Visual Explain in IBM i Access Client Solutions shows the query plan. What to look for:

  • a full table scan on a large table in a query that returns a few rows;
  • a temporary index built on every run, which means a permanent index is missing;
  • functions on a column in a condition, such as SUBSTR(nip, 1, 3) or converting a date stored as a number, which prevent index use;
  • different data types on the two sides of a join, such as CHAR against DECIMAL, which force the database to convert every row.

General rules for fast queries, independent of the platform, are covered in SQL query performance.

Indexes: the advisor and restraint

Db2 for i records index advice in the SYSIXADV system table: the table, the key columns, the index type (radix or EVI), how many times the advice was given and when it was last given. It is a good starting point:

SELECT table_schema, table_name, key_columns_advised, index_type,
       times_advised, last_advised
  FROM QSYS2.SYSIXADV
 WHERE last_advised > CURRENT_TIMESTAMP - 30 DAYS
 ORDER BY times_advised DESC
 FETCH FIRST 20 ROWS ONLY;

Do not apply advice automatically. Every index speeds up reads but slows down writes, because the database maintains it on every change. For a table with heavy writes in the night run, it is better to add one index that covers several pieces of advice than five similar ones. Before adding an index, also check whether an existing logical file with the same key is enough. The difference between DDS files and SQL objects is explained in Db2 for i: SQL instead of DDS.

Testing on a copy of the data and ongoing measurement

Check changes to indexes, programs or memory allocation before production. The test environment needs the full data volume, because on a small test library every query is fast. Steps:

  1. Restore a copy of the production libraries on a test partition, masking personal data when the test calls for it.
  2. Measure the night run and selected queries before the change.
  3. Make one change at a time and measure again.
  4. Check the effect on writes and on other jobs, not only on the query you are fixing.

After go-live, keep measuring: store the step times of the night run in a table and raise an alert when a step grows faster than the data. Load testing and measurement before changes to transactional systems are work for the Performance testing team. ERP performance in general is covered in ERP system performance.

Questions and answers

Will a new server fix IBM i performance problems?

It may speed everything up a little, but it will not remove the cause. If a query scans the whole table, a new server will do it faster, but as the data grows the problem will come back. Measure and fix the most expensive queries and jobs first, then decide on hardware based on the results.

How many indexes are too many?

There is no single number. An index pays off when it speeds up frequent reads more than it slows down writes. For tables with many changes, such as document lines in the night run, measure every additional index. Indexes the optimizer has not used for months are candidates for removal after checking.

Can BI reports slow down the production system?

Yes. Every query from Excel or a BI tool runs as a job on IBM i and uses the same disks and memory. Move large reports outside working hours, limit them to views with the right indexes, or use them to feed a data warehouse instead of querying production on every click.

How long does an IBM i performance analysis take?

Collecting data from several days of operation, including the night run, and analyzing the most expensive queries usually takes a few weeks. Some fixes, such as missing indexes, work immediately. Changes to batch programs need testing on full data and take longer.

Developing a system on IBM i or mainframe? Talk to an RPG engineer.

Let's talk

Read more: IBM i, RPG and mainframe

All in IBM i, RPG and mainframe →