In practice, this makes EXPLAIN useful for answering questions like:
The access path is the route Db2 takes to retrieve the data requested by SQL. It describes which tables and indexes are accessed, how they are accessed, and the order in which Db2 processes them. A good access path usually means less I/O, fewer sorts, and faster response time. A poor access path can lead to table scans, unnecessary sorting, and slower joins.
The central table for EXPLAIN is PLAN_TABLE, which contains one or more rows per query block describing the chosen access path. PLAN_TABLE columns show, for example, access type, index usage, join method, join sequence, estimated row counts, and whether sorts or parallelism are used.
Additional EXPLAIN tables enrich this information:
Together, these tables let performance specialists both see the shape of the access path and quantify its expected cost, which is critical for deciding whether further tuning is justified.
DSNTESC in Db2 libraries, and can be executed once per schema that will run EXPLAIN. You can create it by using the below SQL.CREATE TABLE [userid].PLAN_TABLE LIKE DEFAULT.PLAN_TABLE;
This causes Db2 to insert rows describing the access path for the SELECT portion of the statement into PLAN_TABLE (and related tables if present), tagged with QUERYNO = 1. Alternatively, specifying EXPLAIN(YES) on BIND or REBIND populates EXPLAIN tables for every explainable statement in a package or plan, which is useful for broader access path reviews.
EXPLAIN PLAN SET QUERYNO = 1 FOR [your sql statement here]
Ordering rows in this way helps reconstruct the sequence of operations that Db2 will perform, matching how many visual tools display access plans. For many tasks, only a subset of columns is required, so users often build custom views or queries that project core attributes such as table name, ACCESSTYPE, MATCHCOLS, METHOD, and estimated row counts.
Execute this SQL command to view the Explain information:SELECT * FROM PLAN_TABLE WHERE QUERYNO = 1 ORDER BY TIMESTAMP, QUERYNO, QBLOCKNO, PLANNO, MIXOPSEQ WITH UR;
Look at these fields for important information:
Query to find Total Cost: SELECT SUBSTR(PL.COLLID,1,10) AS COLLID, SUBSTR(PL.PROGNAME,1,10) AS PROGNAME, DATE(PL.EXPLAIN_TIME) AS DATE, TIME(PL.EXPLAIN_TIME) AS TIME, COUNT(PL.QUERYNO) AS "STMT COUNT", DEC(SUM(ST.TOTAL_COST),8,2) AS "TOTAL COST" FROM SJD.PLAN_TABLE PL, SJD.DSN_STATEMNT_TABLE ST WHERE PL.PROGNAME = ST.PROGNAME AND PL.COLLID = ST.COLLID AND PL.EXPLAIN_TIME = ST.EXPLAIN_TIME AND PL.QUERYNO = ST.QUERYNO GROUP BY PL.COLLID, PL.PROGNAME, PL.EXPLAIN_TIME ORDER BY PL.PROGNAME; Result: ————+---------+---------+---------+---------+------------- COLLID PROGNAME DATE TIME STMT COUNT TOTAL COST +---------+---------+---------+---------+----------------- MYCOLL MYPACK 05/08/2014 11.19.38 5 10.10 MYCOLL MYPACK 05/08/2014 17.36.17 8 19.11 Query to Compare Old & New Cost: SELECT ST1.QUERYNO AS QUERYNO, COALESCE(DEC(ST1.TOTAL_COST,8,2),0) AS "OLD COST", COALESCE(DEC(ST2.TOTAL_COST,8,2),0) AS "NEW COST" FROM SJD.DSN_STATEMNT_TABLE ST1 FULL JOIN SJD.DSN_STATEMNT_TABLE ST2 ON ST1.QUERYNO = ST2.QUERYNO WHERE ST1.COLLID = 'MYCOLL' AND ST2.COLLID = 'MYCOLL' AND ST1.PROGNAME = 'MYPACK' AND ST2.PROGNAME = 'MYPACK' AND DATE(ST1.EXPLAIN_TIME) = '2014-05-08' AND TIME(ST1.EXPLAIN_TIME) = '17.36.17' AND DATE(ST2.EXPLAIN_TIME) = '2014-05-15' AND TIME(ST2.EXPLAIN_TIME) = '13.05.14' AND ST1.TOTAL_COST <> ST2.TOTAL_COST ORDER BY QUERYNO; Result: ---------+---------+---------+---------+----- QUERYNO OLD COST NEW COST ---------+---------+---------+---------+----- 353 .15 .11 360 8.07 16.24 Query to find Total Cost and Explain Time: SELECT QUERYNO, TOTAL_COST, EXPLAIN_TIME FROM SJD.DSN_STATEMNT_TABLE WHERE PROGNAME = 'ID14SQP' AND COLLID = 'ID14SQL' ORDER BY TOTAL_COST DESC, EXPLAIN_TIME DESC; Result: -----+---------+---------+---------+---------+---------+---- QUERYNO TOTAL_COST EXPLAIN_TIME -----+---------+---------+---------+---------+---------+---- 88 +0.5616997729372477E+05 2014-11-02-18.03.04.993751 131 +0.1453335050780900E+01 2014-11-02-18.03.04.993751 131 +0.1453335050780900E+01 2014-10-27-16.55.51.641367 SELECT QUERYNO, COALESCE(DEC(TOTAL_COST,8,0),0) AS "COST", EXPLAIN_TIME FROM SJD.DSN_STATEMNT_TABLE WHERE COLLID = ‘ID14SQL' AND PROGNAME = ‘ID14SQP' ORDER BY EXPLAIN_TIME DESC; Result: ---------+---------+---------+---------+---------+ QUERYNO COST EXPLAIN_TIME ---------+---------+---------+---------+---------+ 88 434758 2014-11-02-18.03.04.993751 88 23184 2014-10-27-16.55.51.641367
Several recurring issues in Db2 performance work can be uncovered or confirmed by studying EXPLAIN output.
By scrutinizing these patterns in EXPLAIN output, DBAs can prioritize which schema changes, query rewrites, or statistics maintenance jobs are likely to deliver the biggest performance gains.
An effective query tuning workflow with EXPLAIN generally follows a cycle of measurement, analysis, and adjustment.
For dynamic SQL cached in the dynamic statement cache, facilities such as EXPLAIN STMTCACHE can extract access path information for statements currently or previously executed, which is useful for tuning workloads that are not statically bound.
Good query tuning starts with accurate statistics and predicates that support efficient access. If statistics are stale or incomplete, Db2 may choose a plan that looks reasonable but performs poorly in production.
A practical tuning approach is:
DB2 EXPLAIN is a foundational tool for query performance tuning, giving visibility into the optimizer’s access path choices through PLAN_TABLE and related EXPLAIN tables. By systematically capturing, interpreting, and acting on EXPLAIN output—often aided by Visual Explain diagrams—DBAs and developers can ensure that critical queries use efficient access paths and that schema and statistics evolve in alignment with workload demands.
OUTER JOIN Queries are a valuable tool in SQL, allowing you to retrieve data from…
OUTREC control statement is used to reformat (adds, deletes, or reformats fields) each record after…
DFSORT is your go‑to utility for sorting, merging, and transforming data. ICETOOL Utilities takes DFSORT…
The Product Owner role has shifted from just being a requirements proxy to a strategic,…
Business Value: In the world of Agile development, the user story has long been the…
The SAFe Scrum Master certification has become one of the most sought-after credentials for Agile…
The Professional Scrum with Kanban (PSK) course enhances your organization's ability to deliver value efficiently…
Effective User interviews play a crucial role in Scrum methodology, helping Product Owners and Scrum…
Product Owners should be well-versed in various user research tools and techniques to effectively understand…