Reclaim the space from deleted rows

Here is a source of wasted disk space and quietly degraded performance that hides in plain sight on almost every system: tables full of deleted rows that were never actually reclaimed. One of my favorite topics, because it is a free performance improvement. Every time !!!

When you delete rows from a physical file on IBM i, the system doesn’t always give the space back. The rows are marked deleted, but they still take up room, and the file still has to read past them. (Yes, you can use reuse deleted records, but I am not a huge fan). On a file/table with heavy delete activity, that dead space accumulates until you are storing and scanning a great deal of nothing. You bring those records in and out of memory, with every page of data you read. You can find your worst offenders directly:

SELECT TABLE_SCHEMA, TABLE_NAME,
      NUMBER_ROWS, NUMBER_DELETED_ROWS,
      DATA_SIZE
 FROM QSYS2.SYSPARTITIONSTAT
 WHERE NUMBER_DELETED_ROWS > 0
 ORDER BY NUMBER_DELETED_ROWS DESC
 FETCH FIRST 50 ROWS ONLY;

That gives you the files/tables carrying the most deleted rows, worst first. When you find a file where NUMBER_DELETED_ROWS rivals or exceeds NUMBER_ROWS, you have found a table that is mostly empty data, costing you storage and slowing every full scan. Excessive page faults.

The fix is a reorganize physical file member, RGZPFM, which physically removes the deleted rows and compacts the file. A word of experience, though: reorganizing takes a lock and takes time, so you plan it for a maintenance window rather than firing it at a busy production file mid-morning. Modern releases can reorganize while active in many cases, but you still want to understand what you are doing before you do it.

The real value here is knowing which files are worth the effort. You do not reorganize everything. You find the handful of files/tables where the dead space is genuinely large, and you spend your maintenance window on those. This query is how you aim.

Run it, find your problem files, and put the worst couple of files on your next maintenance window. You get storage and speed back for almost no cost. An added benefit is that your backups and recoveries will also be quicker once you have reorganized the files.

Originally posted on LinkedIn. Join the conversation there.

Get new posts by email

Practical IBM i tips on SQL services, security and backup, sent when I publish. No spam, unsubscribe anytime.

Leave a Reply