Mainframe Path Start learning free
Applied11 min readLesson 1 of 3

Access paths, EXPLAIN, indexes and statistics

DB2's optimizer chooses how to run each SQL statement, using catalog statistics to estimate costs. EXPLAIN shows you that choice. Good indexes and current statistics are the main levers for getting good access paths.

How an access path is chosen

For each statement the optimizer considers many ways to get the data: which index to use, if any, which table to read first in a join, which join method, whether to sort. It estimates the cost of each using statistics in the DB2 catalog and picks the cheapest. The result is the access path.

For static SQL the choice is made at bind time and stored in the package; it does not change until the package is rebound. For dynamic SQL it is made at prepare time, and the result can be reused from the dynamic statement cache.

From statistics to an access path
RUNSTATScollects statistics
Catalogrow counts, cardinality, cluster ratio
Optimizerat BIND or PREPARE
Access pathstored in package or cache
EXPLAINshows the choice

EXPLAIN

EXPLAIN writes the optimizer's choices into tables you own, chiefly PLAN_TABLE, with others such as DSN_STATEMNT_TABLE for cost estimates. You can run EXPLAIN PLAN FOR a statement, or bind with EXPLAIN(YES) so every statement in the package is recorded. Visual tools from IBM and vendors such as BMC and Broadcom present the same data graphically.

PLAN_TABLE columnWhat to look for
ACCESSTYPEI = index, R = table space scan, I1 = one-fetch index, N = IN-list index, M/MX/MI/MU = multiple index access
ACCESSNAMEWhich index was chosen
MATCHCOLSHow many leading index columns are matched by predicates; higher is usually better
INDEXONLYY if DB2 can answer from the index without reading table rows
METHODJoin method: 1 nested loop, 2 merge scan, 4 hybrid; 3 means an extra sort
PREFETCHS sequential, L list, D dynamic, blank none
SORTN_* / SORTC_*Whether sorts are needed for ORDER BY, GROUP BY, DISTINCT or joins
PLAN_TABLE rows for one query (illustrative)
QUERYNO QBLOCKNO PLANNO METHOD TNAME     ACCESSTYPE MATCHCOLS ACCESSNAME INDEXONLY
     10        1      1      0 ACCOUNT   I                  2 ACCTIX1    N
     10        1      2      1 TXNHIST   I                  1 TXNIX2     N

Read it as: first access ACCOUNT through ACCTIX1 matching two columns, then join TXNHIST with a nested loop join through TXNIX2. A row showing R on a large table, or MATCHCOLS 0 on an index you expected to match, is usually where tuning starts.

Indexing strategy

Statistics and RUNSTATS

The RUNSTATS utility reads tables and indexes and records statistics such as row counts (CARDF), number of distinct key values (FIRSTKEYCARDF, FULLKEYCARDF), index levels and leaf pages, and cluster ratio. It can also collect frequency and histogram statistics for skewed columns, where a few values account for most rows.

Many sites run RUNSTATS after REORG or large LOADs and drive it from real-time statistics (counters DB2 keeps in catalog tables such as SYSTABLESPACESTATS), sometimes using the DSNACCOX stored procedure to recommend which objects need attention. Stale statistics, for example from when a table was nearly empty, are one of the most common causes of bad access paths.

TRY IT YOURSELF

In PLAN_TABLE, what ACCESSTYPE value indicates a table space scan?

Show a hint

It is a single letter.

Show the solution

R. A table space scan reads every page; it is fine for small tables or large batch reads but costly for online lookups.

Common mistakes

Running RUNSTATS on an empty or test-sized table

Statistics showing a few rows lead the optimizer to choose scans that are disastrous at production volumes. Collect statistics when the data is representative, or set them deliberately.

Rebinding everything without a fallback

A mass REBIND can change many access paths at once. Use plan management, compare EXPLAIN output and rebind in controlled batches.

Adding an index for every slow query

Each index slows inserts and updates and adds utility work. Check whether an existing index can be extended or a predicate rewritten first.

What you will see at work

Key terms

Check your understanding.
Take this lesson's quiz and save your progress. Free.

Take the lesson quiz
Utilities, backup and recovery →