Case Study: Automating Db2 REORG and RUNSTATS

Modified on Mon, 30 Nov at 4:25 PM

Running REORG and RUNSTATS on a fixed schedule spends CPU on objects that do not need them and reaches the ones that do too late. Choosing the right objects for every run takes more analysis than a fixed job or a script can do. North Carolina Farm Bureau Mutual Insurance Company replaced periodic maintenance with TUC automation that decides for each object whether it needs REORG or RUNSTATS, and selects, ranks, schedules and runs the work.

Results

Hours, Not Days — Routine maintenance that took the DBAs days every month now takes hours. The team lets the automation run and spends its time on projects that add value.

No More Space Failures — For one job, the DBAs used to add four to five volumes every three months and were called in the middle of the night when it failed for lack of space. Now that REORG releases wasted space in time, those failures have stopped, and so have the outages they caused.

REORG at the Right Time — Each object is reorganized when it needs it, not earlier, which wastes CPU, and not later, which costs performance.

The Most Urgent First — TUC calculates how many objects fit in the available window from the duration of past REORGs, and takes the objects in greatest need first.

Current Statistics — Statistics are kept up to date, so Db2 chooses access paths from current information and the workload runs faster and more efficiently.

Paid Back in Year One — The whole TUC implementation, which also automated the refresh of test data, returned its full cost in its first year, counting only the staff time it saved.

The Site

The customer runs 15 Db2 subsystems in four LPARs on an IBM z14 ZR1. Three subsystems serve production and twelve serve testing. The machine has ample capacity for the Db2 workload, but performance suffers whenever the objects are not in good shape. A small database team supports all of the subsystems.

The Problem

Before TUC, the team ran REORG and RUNSTATS periodically, with only a broad idea of which objects needed attention at a given time. Being selective would have meant analyzing every object before every run, and that analysis took more time than the team had.

The result cut both ways. Objects that did not need maintenance were processed anyway, and the CPU spent on them was wasted. Objects that did need it were processed too late: their disorganization slowed the applications, and some ran out of space and held up the work that depended on them. For a team responsible for fast and reliable services, these were real obstacles.

The team had seen other tools that promised to solve these problems. They were either too expensive, not flexible enough for the site, or did not come with the support the team knew it would need during and after the implementation.

Why Selective Maintenance Is Hard

Db2 records everything needed to decide which objects need maintenance in its real-time statistics. Turning those numbers into the right jobs at the right time is where the work lies:

  • Many indicators per object. Tablespaces and indexes degrade in different ways: changed rows, unclustered inserts, overflow rows, mass deletes, extents, index levels, page splits and pseudo-deleted entries. Each has its own threshold, and an installation has thousands of objects.
  • Disorganized is not the same as slow. The statistics show that an object is disorganized. They do not show whether the disorganization actually hurts the applications. A REORG that brings no improvement is pure cost.
  • The window is limited. When there are more candidates than the window can hold, someone has to decide which come first and how many fit.
  • Space. An online REORG builds shadow datasets beside the originals. Several large REORGs running at once can exhaust the storage group and fail, even though space would be released as soon as the first of them finished.
  • Collisions. A REORG that runs while an application job or another utility holds the object fails or delays the application.
  • After the REORG. Packages must be rebound to benefit from the new organization and statistics, and a rebind can occasionally choose a worse access path.
  • Statistics. Collecting statistics on every object is expensive when most of the data has not changed. Not collecting them leaves the optimizer working from stale numbers.

What the Customer Did

The customer introduced TUC in two phases, starting in early 2021. The first phase automated REORG and RUNSTATS and optimized when each one runs. The second phase replaced the refresh of test data by UNLOAD and LOAD with DSN1COPY.

Most objects were under automatic REORG and RUNSTATS within a short time. The exceptions followed, with TUC support adapting the setup to each of them, and the project was completed in May 2022. The team could have finished sooner had it given the project more of its time, but the measured pace still produced a full return on the investment in the first year.

Today each object is reorganized exactly when it needs it. TUC calculates a quota of objects for the available window from the performance of past REORGs, and the objects in greatest need are processed first. Space that disorganized objects used to waste has been released, the space failures and the night calls have stopped, and the statistics are always current. The team has gained confidence in the health of its databases.

The customer plans to use more of TUC next, in particular automated backup and recovery. TUC can take image copies only when the data has changed, and when a table has to be recovered it shows the available recovery points, asks which one to use, and identifies the related tables that must be recovered with it.

How TUC Automates REORG and RUNSTATS

TUC turns the real-time statistics into decisions, the decisions into jobs, and the jobs into a schedule that fits the window and the available space. The DBA sets the rules once; TUC applies them on every run.

Policy Rules Read the Real-Time Statistics

The TUC enforce process evaluates every object covered by the policy against a set of rules, each with a threshold. Objects that qualify become candidates for the utility. TUC comes with a ready set of rules, and every threshold can be changed for the whole installation, for a database or for a single object.

Utility What the rules detect
REORG of tablespaces Rows inserted and deleted since the last REORG, unclustered inserts, overflow rows that add I/O on every read, mass deletes, too many extents or volumes, fragmented LOBs, low cluster ratio, and REORG pending states.
REORG of indexes Added index levels, page splits, pseudo-deleted entries, entries appended at the end of the index, and entries changed since the last REORG. An index can be reorganized on its own when its tablespace does not need it.
RUNSTATS Rows inserted, updated and deleted since the last statistics, mass deletes, objects with no statistics or reorganized after their last statistics, space growth such as after a LOAD RESUME, and statistics that Db2's own monitoring reports as stale.
Exclusions Objects reorganized only a few days ago, objects too large for the normal window, empty objects, volatile tables in a transitional state, freshly rotated partitions, objects held by a long-running write claim that a REORG could not drain, and objects that are stopped or already under a utility.

The candidates can be reviewed in a report before anything runs. When the list reflects what the installation needs, the same rules place the orders automatically.

REORG Only When It Pays

A disorganized object does not always perform worse. TUC measures what each object actually costs to access. It reads the Db2 statistics trace, which has a very low overhead, and keeps for each object the ratio of getpages to synchronous I/O requests measured right after its last REORG, when the object is at its best. When that ratio has fallen by more than 20 percent, performance has degraded.

A REORG goes ahead when the object both meets a disorganization rule and shows degraded performance. When the object is disorganized but still performs well, the REORG is postponed. Conditions that threaten availability, such as a REORG pending state, too many extents or mass deletes, always lead to a REORG, whatever the performance. The same measurements show whether each REORG that did run really improved access.

TUC records the cost of each object's last REORG and how many REORGs it has avoided since. The CPU and hours saved by the REORGs that were safely skipped, and the synchronous I/O removed by the ones that ran, are computed from the measurements on the installation's own system and shown, database by database, in the Savings view of the TUC Dashboard.

RUNSTATS Only When Needed

RUNSTATS runs for the objects whose data has changed enough to matter, not for every object. TUC's REORG jobs collect statistics inline, during the REORG itself, so reorganized objects need no separate RUNSTATS. Statistics options can be kept per table, and TUC can work with Db2's autonomic statistics monitoring so that the statistics Db2 asks for are the ones collected.

The Most Urgent First

Qualifying is not enough when the window is short. TUC ranks the candidates by how severe their condition is. Rules can carry priorities with their own thresholds, either fixed values or percentiles calculated over the history of all objects. For example, a priority set at the 40th percentile goes to every object whose value is higher than that of 40 percent of the objects. Within a priority, objects that have waited longest since their last REORG come first, and objects that were never reorganized are not left behind.

Fitting the Window

The amount of work placed in one run can be limited by the number of orders, by their total size, or by a time window. When the limit is a time window, TUC calculates how much data fits in it from the processing rate of the orders completed in the last month. This is how a site catches up with its backlog a little every day, most urgent first, without ever overrunning the window.

Parallel Jobs That Finish Together

A profile can be split into parts that run as parallel jobs. TUC distributes the objects between the parts by size, so the jobs end at about the same time instead of one job running for hours while the others finish in seconds. Each order is rebalanced again for the objects it actually contains. All the partitions of a tablespace with a non-partitioned index stay in the same job, so that parallel jobs do not compete to rebuild the same index.

When several REORG orders run at once, TUC does not simply start the largest first. It mixes small, medium and large orders, so that space is released by the small ones while the large ones are running, and several large REORGs never hold shadow datasets in the same storage group at the same time.

No Space Failures

Before a REORG, TUC can resize the object from the expected number of rows, corrected with the real-time statistics, so that it is reallocated at the right size. Rules for extents and volumes consolidate objects that have spread out.

Before each REORG order starts, TUC can check that the storage group of the data has room for the shadow datasets and that the work storage group has room for the largest object in the order. If space is short, the order waits five minutes and is checked again, while other orders that fit continue. Space is reserved for every order as it starts, so two orders never count on the same free space.

No Collisions With Applications

TUC estimates how long each REORG will take from its size and the average REORG rate. It then searches the TWS current plan for application or utility jobs that are due to use the same objects in that period, and makes the REORG wait for them. If such a job is late, the dependency is removed after 30 minutes so the REORG is not held forever. Objects that are held by a long-running write claim, stopped, or already under a utility are left out, so the REORG is not attempted where it would fail.

Everything Around the REORG

Each REORG job is prepared complete. TUC creates the mapping tables that online REORG requires, one per parallel job. It takes inline image copies and inline statistics, so no separate copy or RUNSTATS is needed, can create a point of consistency for the objects that were processed, applies discard conditions when rows are to be removed, and rebinds the packages that depend on the reorganized objects.

Rebinding Without Risk

A rebind after REORG or RUNSTATS is usually as good as or better than before, but not always. TUC asks Db2 to compare each new access path with the previous one and evaluates every change, including its cost. A package whose access path got worse can be switched back to the previous one automatically, and the tables responsible can be selected for a corrective REORG or RUNSTATS by the policy.

Without and With TUC

Task Without TUC With TUC
Choosing objects Fixed lists on a fixed schedule. Rules on the real-time statistics of every object, every run.
Needless REORGs Run anyway. Skipped when measured access cost has not risen; the savings are reported.
Statistics Collected for everything, or left stale. Collected where the data changed, and inline during REORG.
Order of work Whatever is next in the job. By severity, then by time since the last REORG.
The window Guessed, and sometimes overrun. Quota calculated from last month's processing rate.
Space Volumes added after a failure. Objects resized, space checked and reserved before each order.
Collisions Found when a job fails. Planned jobs found in TWS and waited for; held objects left out.
Rebind Run and hoped for the best. Every access path change evaluated; a worse one switched back.

In short — TUC replaced the customer's periodic REORG and RUNSTATS with maintenance that each object receives when it needs it, the most urgent first, within the window. Days of routine work each month became hours, the space failures stopped, and the implementation paid for itself in its first year.

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons

Feedback sent

We appreciate your effort and will try to fix the article