Advanced Query Tuning With IBM Data Studio
Advanced Query Tuning With IBM Data Studio
David Simpson
Themis Training
dsimpson@themisinc.com
www.themisinc.com
Platform: Both Db2 LUW and z/OS
David Simpson
David Simpson is currently the Vice President of Themis Inc. He teaches courses on SQL,
Application Programming, Database Administration as well as optimization, performance
and tuning. He also installs and maintains the database systems used for training at Themis
and works with our network of instructors to deliver high quality training solutions to our
customers worldwide.
Since 1993 David has worked as a developer and DBA in support of very large
transactional and business intelligence systems. David is a certified DB2 DBA on both z/OS
and LUW. David was voted Best User Speaker and Best Overall Speaker at IDUG North
America 2006. He was also voted Best User Speaker at IDUG Europe 2006 and is a
member of the IDUG Speakers Hall of Fame. David is also an IBM Gold Consultant.
• System Tuning
Catalog Statistics
Object Definitions
Access Path
Rid Pools, Sort Pools
CPU Speed, # CPUs
Access Path Hint
Explain
OR
Open Visual
Explain
Select Subsystem
Connection
z/OS Data Studio Access Path Graphs
LUW Data Studio Access Path Graphs
Data Studio Access Path Graphs
Sources of Data
Data Studio Access Path Graphs
Data Studio Access Path Graphs
Data Retrieval
Operations
z/OS Stage 1, Stage 2 Predicates
z/OS Stage 2 Predicates Rewrites
Data Studio
Estimated number
of rows
Plan Table
Why Would the Optimizer Choose a Table Space Scan?
1. Are any predicate(s) poorly coded in a non-indexable way that takes away any possible
index choices from the optimizer?
2. Do the predicates in the query not match any available indexes on the table? Know your
indexes on a table!
3. The table could be small and Db2 decides a table scan may be faster than index processing.
4. The catalog statistics could say the table is small. This is more common in test
environments where the Runstats utility is not executed very often.
5. Are the predicates such that Db2 thinks the query is going to retrieve a large enough
amount of data that would require a table scan? Some explain tools will show the number
of rows Db2 thinks will be returned in the execution of a query (the IBM Data Studio tool is
very good at this).
Why Would the Optimizer Choose a Table Space Scan?
6. Are the predicates such that Db2 picks a non-clustered index, and the rows needed are
scattered throughout the table file such that the number of data pages to retrieve is high
enough based on total number of pages in the table to require a table scan? Know how
data is physically clustered in the tablespace!
7. Are the tablespace files or index files physically out of shape and
need a REORG?
8, Are there no predicates? So the query wants all the rows.
9. Sometimes there are just too many conditions in the logic to return the
results needed any other way. This is quite typical with many predicates
that are OR’d together.
z/OS Index Scan - Matching
SELECT * FROM EMP
WHERE LASTNAME = ?
PLAN_TABLE AND FIRSTNME = ‘Michelle’;
Notice FF of
.00105 for
LASTNAME
Predicate.
1/947 = .00105
z/OS Index Screening
INDEX XEMP03 on
(LASTNAME, FIRSTNME, MIDINIT)
SELECT * FROM EMP
WHERE LASTNAME = ‘Coldsmith’
AND MIDINIT = ‘R’;
Index Screening
Predicate
PLAN_TABLE
PLAN METHOD TNAME ACCESS MATCH ACCESS INDEX PREFETCH
NO TYPE COLS NAME ONLY
1 0 EMP I 1 XEMP03 N
z/OS Index Screening
LUW Index Scan - Matching (Start/Stop Keys)
SELECT * FROM EMP
WHERE EMPNO BETWEEN ‘000000’ and ‘099999’
LUW Index Screening
SELECT * FROM EMP
WHERE LASTNAME = ‘Smith’
AND MIDINIT = ‘R’
z/OS Index Scan - Nonmatching
PLAN_TABLE
PLAN METHOD TNAME ACCESS MATCH ACCESS INDEX PREFETCH
NO TYPE COLS NAME ONLY
1 0 EMP I 0 XEMP03 N
z/OS Index Scan - Nonmatching
The Premise
If A must equal B
And A is RED,
Then B must also be RED.
Predicate Generation Through Transitive Closure
SELECT . . . . SELECT . . . .
FROM DEPT FROM DEPT
WHERE DEPTNO = ADMRDEPT WHERE DEPTNO = ADMRDEPT
AND ADMRDEPT = ‘A00’ ; AND ADMRDEPT = ‘A00’
AND DEPTNO = ‘A00’ ;
SELECT . . . . SELECT . . . .
FROM DEPT D, EMP E FROM DEPT D, EMP E
WHERE D.DEPTNO = E.DEPTNO WHERE D.DEPTNO = E.DEPTNO
AND D.DEPTNO IN (‘A00’ AND D.DEPT IN (‘A00’,
‘B01’, ‘B01’,
‘C11’) ; ‘C11’)
AND D.DEPTNO IN (‘A00’,
‘B01’,
‘C11’) ;
Predicate Transitive Closure
SELECT . . . .
FROM DEPT
WHERE DEPTNO = ADMRDEPT
AND ADMRDEPT = ‘A00’ ;
Note: Index on
DEPTNO chosen
LUW Predicate Transitive Closure
Tuning a Query
Tune a Query
Tuning a Query
Tuning a Query Output
Opens Advisor
details tab to view
information
Note: Note:
Non Correlated Correlated
Case #1: 2 Possible Indexes
Better Index
Chosen
Case #2: Join Order
SELECT *
FROM EMP E, PROJ P
WHERE E.EMPNO = P.EMPNO
AND E.COMM = 0
AND P.PRSTAFF > 3
Comparing Estimates with Reality
SELECT *
FROM EMP E, PROJ P
WHERE E.EMPNO = P.EMPNO
AND E.COMM = 0
AND P.PRSTAFF > 3
Thank You for Attending!
Tony Andrews
Themis Inc.
tandrews@themisinc.com