Explain: Window To The DB2 Optimizer
Explain: Window To The DB2 Optimizer
1
Optimizer
♦ What is optimization ?
For a given SQL statement, select the access path which returns
the correct result with minimum elapsed time.
2
Sample SQL query
3
Sample SQL query
4
Sample SQL query
5
Explain What ?
♦ What’s this explain thing anyway?
– Explain is a DB2 SQL tuning tool designed to give a picture of
how DB2 intends to access the data
– Explain can tell you a lot about what DB2 “plans” to do with your
SQL , IF you know what to look for.
♦ What Does the Optimizer “care about”
♦ Cardinality – What does it really mean
♦ Cluster ratio – so what !
♦ Filter Factor – and ….
♦ Correlation Statistics – what’s that!
6
How Do I Get One ?
♦ Visual Explain
♦ Static Explain (Desktop application)
(Bind/Rebind command)
7
Explain Report Query
SELECT P.PROGNAME, STRIP(DIGITS(P.QUERYNO),LEADING,'0') AS QN,
STRIP(DIGITS(P.QBLOCKNO), LEADING,'0') AS QBN,
MIXOPSEQ AS SEQ, STRIP(DIGITS(PLANNO), LEADING,'0') AS PN,
STRIP (DIGITS(METHOD), LEADING,'0') AS MT,
TNAME, PREFETCH AS PRE, ACCESSTYPE AS AT,
SUBSTR(DIGITS(MATCHCOLS),5,1) CONCAT ' ' CONCAT
INDEXONLY AS MC_IO, ACCESSNAME AS XNAME,
SORTN_UNIQ CONCAT SORTN_JOIN CONCAT SORTN_ORDERBY CONCAT SORTN_GROUPBY AS UJOG,
SORTC_UNIQ CONCAT SORTC_JOIN CONCAT SORTC_ORDERBY CONCAT SORTC_GROUPBY AS UJOC, QBLOCK_TYPE,
ROUND(DECIMAL(PROCMS,9,2),2) AS PRO_MSEC,COST_CATEGORY AS CC, VERSION, P.BIND_TIME FROM
SYDB2FT.PLAN_TABLE P, SYDB2FT.DSN_STATEMNT_TABLE S WHERE P.QUERYNO = S.QUERYNO
AND P.APPLNAME = S.APPLNAME AND P.PROGNAME = S.PROGNAME
AND P.COLLID = S.COLLID AND P.PROGNAME = 'FBC4300'
AND P.COLLID = 'TEST' AND P.BIND_TIME = S.EXPLAIN_TIME
AND P.PROGNAME = 'FBC4300' AND P.BIND_TIME = (SELECT MAX(BIND_TIME) FROM SYDB2FT.PLAN_TABLE X
WHERE X.APPLNAME = P.APPLNAME AND X.PROGNAME = P.PROGNAME AND X.COLLID = P.COLLID AND
X.PROGNAME = 'FBC4300') ORDER BY P.QUERYNO,QBLOCKNO,PLANNO,MIXOPSEQ
--------+---+-+-+-+-+-----------------+-+-+-+-+--------+----+----+---+-----+-+--------------------------
PROGNAME QN Q S P M TNAME P A M I XNAME SORT SORT QRY PRO C VERSION
B E N T R T C O NEW COMP BLO MSEC C
N Q E UJOG UJOG CK
--------+---+-+-+-+-+-----------------+-+-+-+-+--------+----+----+---+-----+-+--------------------------
DSSCWS15 463 1 0 1 T_DSBB_BANK_BATCH I 1 N DSBBNN01 NNNN NNNN SEL 15.00 A 2004-02-02-20.28.37.114881
DSSCWS15 463 1 0 2 1 T_DSBA_BATCH I 1 N DSBACU00 NNNN NNNN SEL 15.00 A 2004-02-02-20.28.37.114881
DSSCWS15 463 1 0 3 4 T_DSDP_DEP L I 1 N DSDPNN01 NNNN NYNN SEL 15.00 A 2004-02-02-20.28.37.114881
DSSCWS15 463 1 0 4 1 T_DSCK_CHECK I 1 N DSCKNN01 NNNN NNNN SEL 15.00 A 2004-02-02-20.28.37.114881
DSSCWS15 858 1 0 1 T_DSBD_BANK_DEP I 1 N DSBDCU00 NNNN NNNN SEL 1.00 A 2004-02-02-20.28.37.114881
DSSCWS15 915 1 0 1 T_DSBD_BANK_DEP I 1 N DSBDCU00 NNNN NNNN UPD 1.00 A 2004-02-02-20.28.37.114881
DSSCWS15 925 1 0 1 T_DSBD_BANK_DEP I 1 N DSBDCU00 NNNN NNNN UPD 1.00 A 2004-02-02-20.28.37.114881
DSSCWS15 958 1 0 1 T_DSBA_BATCH I 1 N DSBACU00 NNNN NNNN UPD 1.00 A 2004-02-02-20.28.37.114881
--------+---+-+-+-+-+-----------------+-+-+-+-+--------+----+----+---+-----+-+--------------------------
♦ QUERY in DB2A.DBA.EXPLAIN(EXPLAIN1)
8
Predicate Types
9
Predicate Application
♦ Stage 1 Predicates
– Sometimes referred to as Sargable
– Can be applied at the 1st stage of predicate processing
– All indexable predicates are also stage 1
– But not all stage 1 predicates are indexable
♦ Stage 2 Predicates
– Sometimes referred to as Nonsargable or Residual
– Cannot be applied until the 2nd stage of predicate
processing
– Stage 2 predicates not indexable
10
Predicate Application
(Relational
Data System)
(Data
Manager)
(Index
Manager)
(Buffer
Manager)
11
Indexable Predicate
12
Single Table Access Method
♦ Tablescpace Scan
♦ Index Access
– Matching Index Access
– Non matching Index Access
– Index Scan
13
Tablespace Scan
14
Sequential Prefetch
15
Sequential Detection
16
Tablespace Scan
17
Matching Index Access
Index (workdept,empno,name)
18
Non-matching Index Scan
Index (workdept,empno,name)
19
Multi Index Access
20
Multi Index Vs IN-list Access
21
Multi Index Vs IN-list Access
22
List Prefetch
23
Sort Indicators – Number of Sorts
24
Join Methods
♦ Nested Loop Join
♦ Merge Scan Join
♦ Hybrid Join
Note : The forth join method Star Joins will not be discussed in this presentation.
25
Table Join Terminology
26
Nested Loop Join
27
Nested Loop Join
SELECT A.EMPNO, A.EMPNAME, A.DEPTNO, B.DEPTNAME
FROM EMPLOYEE A, DEPARTMENT B
WHERE A.DEPTNO = B.DEPTNO
1. Employee Table (outer) is read sequentially. First row contains deptno = 500
2. Department table (inner) is scanned until deptno = 500 is found
3. Rows are merged and data is stored in the composite table
4. Steps 1 thru 3 are repeated for all the rows in the outer table
28
Nested Loop Join
29
Sort Merge Scan Join
30
SORT MERGE SCAN JOIN
SELECT A.EMPNO, A.EMPNAME, A.DEPTNO, B.DEPTNAME
FROM EMPLOYEE A, DEPARTMENT B
WHERE A.DEPTNO = B.DEPTNO
1. Employee Table is read in the order of the join column, which is deptno. In this example
the data was clustered by deptno, however, DB2 may invoke a sort if needed.
2. Department Table is sorted by deptno. This is necessary since the Employee is providing
the deptno as the join column.
3. Each table is read sequentially and the rows are matched.
4. Merge the inner and outer table rows and store in composite table
5. Step 3 and 4 are repeated for all rows
31
Sort Merge Scan Join
32
Hybrid Join
33
Hybrid Join
34
Hybrid Join
35
Hybrid Join
1. Employee Table (outer) is read in the order of the join column, which is
deptno. In this example the data was clustered by deptno, however, DB2 may
invoke a sort if needed.
2. Read the index on Department Table (inner) containing the deptno. Obtain the
RID associated with the matching deptno from the Employee Table.
3. Merge the results of the Employee Table data and the Department Index RIDs
into an intermediate phase 1 table.
4. Sort intermediate phase 1 table and create both a sorted RID list and an
intermediate phase 2 table.
5. Using the sorted RID list, utilize list prefetch to obtain the data from the
Department Table (inner).
6. Merge the results of the intermediate phase 2 table with the information
obtained from step 5 to create the final composite table.
36
Hybrid Join
37
Visual Explain
Visual Explain capability
38
Visual Explain
39
Visual Explain
40
Visual Explain
41
Visual Explain
42
Visual Explain
43
Visual Explain
44
Visual Explain
45
Visual Explain
46
How do I get Visual Explain ?
http://dbserve.fmr.com/
1) Click the DB2 button
2) Click the Install DB2 Visual Explain link
Follow instructions.
DB2 Connect must be installed before you run Visual Explain
Step 1
Enable Visual Explain by.. Click on … Subsystem Æ Enable Visual Explain
Enter
Database name = PLANDB
Table Space name = PLNSPACE
Qualifier = DB2VE click OK! And another OK!
To view Static bound program access path. Click on … Tools Æ List Static SQL statements
Click on.. Add Package.. Enter Package Name(DBRM).. OK.. And.. Retrieve Statements
47
Thank you for attending!!!
References
Fundamentals of DB2 Query Optimization
Gene Fuh, Manager, Query Optimizer and Index Manager, DB2 z/OS, IBM Silicon
Valley Lab
DB2 for z/OS and OS/390 Development for Performance Book with V6 & V7 Features
Copyright© 1998, 2001, by Gabrielle & Associates
48
Appendix 1. Stage 1 Predicates
49
Appendix 1. Stage 2 Predicates
50
Appendix 2. Plan Table Columns
Copyright© 1998, 2001, by Gabrielle & Associates
51
Appendix 2. Plan Table Columns (cont.)
Copyright© 1998, 2001, by Gabrielle & Associates
52
Appendix 2. Plan Table Columns (cont.)
Copyright© 1998, 2001, by Gabrielle & Associates
53
Appendix 2. Plan Table Columns (cont.)
Copyright© 1998, 2001, by Gabrielle & Associates
54
Appendix 2. Plan Table Columns (cont.)
Copyright© 1998, 2001, by Gabrielle & Associates
55
Appendix 2. Plan Table Columns (cont.)
Copyright© 1998, 2001, by Gabrielle & Associates
56
Appendix 2. Plan Table Columns (cont.)
Copyright© 1998, 2001, by Gabrielle & Associates
57
Appendix 2. Plan Table Columns (cont.)
Copyright© 1998, 2001, by Gabrielle & Associates
58
Appendix 2. Plan Table Columns (cont.)
Copyright© 1998, 2001, by Gabrielle & Associates
59
Appendix 2. Plan Table Columns (end.)
Copyright© 1998, 2001, by Gabrielle & Associates
60