Upload
vibhaw-prakash-rajan
View
34
Download
11
Embed Size (px)
DESCRIPTION
DB2
Citation preview
1
z/OS
Application Development
Tuning SQL for Performance
Sheryl M. Larsen Sheryl M. Larsen Sheryl M. Larsen, Inc.Sheryl M. Larsen, Inc.
Platform:
2013
IDUG, World Wide
2© Sheryl M. Larsen, Inc. 2000-2011Contact: [email protected] (630) 399-3330 WWW.SMLSQL.COMContact: [email protected] (630) 399-3330 WWW.SMLSQL.COM
Sheryl M. Larsen, Inc.DB2 SQL Consulting & Education
Sheryl Larsen is an internationally recognized researcher, consultant and lecturer, specializing in DB2 and is known for her extensive expertise in SQL. She co-authored a book, DB2 Answers, Osborne-McGraw-Hill, 1999.
She was voted an inaugural IBM Information Champion 2009- 2011, IDUG Speaker Hall of Fame and, a member of IBM’s DB2 Gold Consultants program since 1994.
Currently, she is President of the Midwest Database Users Group (mwdug.org), and owns Sheryl M. Larsen, Inc., a firm specializing in Advanced DB2 Consulting and Education.
Sheryl has 25 years experience in DB2, has published articles, white papers, webtv, animated
access paths:
Google Sheryl Larsen @Bmc.com Ca.com Ibm.com
3© Sheryl M. Larsen, Inc. 2000-2011
SQL PerformanceDB2 Engine ComponentsPredicate Processing IntelligenceOptimizer DetailsSQL Performance Rules
Tuning QueriesWhen? Why? How? Introduction to Proven SQL Tuning Methods
When are Access Paths Good or Bad? Variations of index accessVariations of table accessVariations of join methods
Reading the Optimizer’s MindPLAN_TABLE OutputDSN_STATEMENT Output
Table of Contents - TuningCase Studies Using a Proven Method
Tuning Example 1 – OPTIMIZE FOR n ROWS/FETCH FIRST n ROWS ONLYTuning Example 2, 3, 4 – No OperationsTuning Example 5, 6 – Fake FilteringTuning Example 7 – Index Design
Extreme TuningTuning Example 8 – Distinct Table Expressions Tuning Example 9 – Anti-JoinsTuning Example 10 – (Predicate OR 0 = 1)Tuning Example 11 – Extreme Cross Query Block Optimization
Designing Performance Structures Index Design for a Single QueryIndex Design for a WorkloadMQT Design for a Workload
4
SQL Performance
DB2 Engine ComponentsDB2 Engine Components
Predicate Processing IntelligencePredicate Processing Intelligence
Optimizer DetailsOptimizer Details
SQL Performance RulesSQL Performance Rules
5
DirectoryDirectory
WorkWorkFilesFiles
Buffer Pool
SQLSQL
CatalogCatalog
DataData
Buffer ManagerBuffer Manager
ResultResult
DB2 Engine Components
Stage 2 – z/OSStage 2 – z/OSResidual – LUW Residual – LUW
IndexIndex
Stage 1 – z/OSStage 1 – z/OSSargable – LUW Sargable – LUW
SQL Execution
DirectoryDirectory
CatalogCatalog
Access Plans
Meta Data
Optimizer
RIDPool
RIDs
Dynamic Statement Cache
DB2 V8 Enhancements
6
DirectoryDirectory
WorkWorkFilesFiles
Buffer Pool
SQL
CatalogCatalog
DataData
Buffer Manager
Result
Stage 2
IndexIndex
Stage 1 DirectoryDirectory
CatalogCatalog
8K 16K 32K Pages
IS NOT NULLIS NOT NULLIS NOT DISTINCT FROMIS NOT DISTINCT FROM
Index only on VARCHARIndexes
Top & Bottom NUDS
Materialized Query Tables
2M Statement Length Multi-row FETCHMulti-row INSERT
SELECT INTO w/ ORDER BYCommon Table Expressions
Recursive SQLIS NOT DISCTINCT FROM
Scalar FullselectGROUP BY Expression
Dynamic Scrollable CursorsINSERT with SELECT
Multiple DISTINCT ClausesSequences
Parallel SortParallel Sort
Forward and Backward IndexesDPSI Indexes
Stage 1 Unlike Data TypesStage 1 Unlike Data Types
64 Bit Addressing
More Hybrid Joins
© SMLI 2009
Access Plans
Meta Data
Optimizer
RIDPool
RIDs
Dynamic Statement
Cache
DB2 9 – WOW!
7© Sheryl M. Larsen, Inc. 2000-2009
DirectoryDirectory
WorkWorkFilesFiles
Buffer Pool
SQL
CatalogCatalog
DataData
Buffer Manager
Result
Stage 2
IndexIndex
Stage 1 DirectoryDirectory
CatalogCatalog
In MemoryGLOBAL TEMP TABLES
Views Table Expressions
In Memory
Faster Sorts for GROUP BYDISTINCT, ORDER BY
Index on Expression
Histogram Statistics
Page Range Index ScreeningFor Partitioned Indexes
Global Optimization
TRUNCATEMERGE
SELECT FROM UPDATE/DELETEEXCEPT, INTERSECTOLAP Expressions
ORDER BY / FETCH FIRST in Subselect
More Parallelism
Sparse Indexesfor more Work Files
Access Plans
Meta Data
Optimizer
RIDPool
RIDs
Dynamic Statement
Cache
8© Sheryl M. Larsen, Inc. 2000-2008
4K
WorkWorkFilesFiles
4K Page Processing – z/OS
Buffer Manager
DataData IndexIndex
Buffer Pool
Stage 2
Stage 1
9© Sheryl M. Larsen, Inc. 2000-2008
Indexable Stage 1 Predicates
Predicate Type Indexable Stage 1
COL = value Y Y COL = noncol expr Y Y COL IS NULL Y Y COL op value Y Y COL op noncol expr Y Y COL BETWEEN value1 AND value2
Y Y
COL BETWEEN noncol expr1 AND noncol expr2
Y Y
COL LIKE 'pattern' Y Y COL IN (list) Y Y COL LIKE host variable Y Y T1.COL = T2.COL Y Y T1.COL op T2.COL Y Y COL=(non subq) Y Y COL op (non subq) Y Y COL op ANY (non subq) Y Y COL op ALL (non subq) Y Y COL IN (non subq) Y Y COL = expression Y Y (COL1,...COLn) IN (non subq) Y Y (COL1, …COLn) = (value1, …valuen)
Y Y
T1.COL = T2.colexpr Y Y COL IS NOT NULL Y Y COL IS NOT DISTINCT FROM value
Y Y
COL IS NOT DISTINCT FROM noncol expression
Y Y
COL IS NOT DISTINCT FROM col expression
Y Y
COL IS NOT DISTINCT FROM non subq
Y Y
T1.COL IS NOT DISTINCT FROM T2.COL
Y Y
T1.COL IS NOT DISTINCT FROM T2.col expression
Y Y
Stage 1 Predicates
Predicate Type
Indexable Stage 1
COL <> value N Y COL <> noncol expr N Y COL NOT BETWEEN value1 AND value2
N Y
COL NOT BETWEEN noncol expr1 AND noncol expr2
N Y
COL NOT IN (list) N Y COL NOT LIKE ' char' N Y COL LIKE '%char' N Y COL LIKE '_char' N Y T1.COL <> T2.COL N Y T1.COL1 = T1.COL2 N Y COL <> (non subq) N Y COL IS DISTINCT FROM N Y
1. Indexable = The predicate is a candidate for Matching Index access. When the optimizer chooses to use a predicate in the probe of the index, the condition is named Matching (matching the index). This is the first point that filtering is possible in DB2.
2. Index Screening = The Stage 1 predicate is a candidate for filtering on the index leaf pages. This is the second point of filtering in DB2.
3. Data Screening = The Stage 1 predicate is a candidate for filtering on the data pages. This is the third point of filtering in DB2.
4. Stage 2 = The predicate is not listed as Stage 1 and will be applied on the remaining qualifying pages from Stage 1. This is the fourth and final point of filtering in DB2.
10© Sheryl M. Larsen, Inc. 2000-2008
Predicate Type Indexable Stage 1
Stage 1
value BETWEEN COL1 AND COL2 N N COL BETWEEN COL1 AND COL2 N N COL BETWEEN expression1 AND expression2 N N value NOT BETWEEN COL1 AND COL2 N N T1.COL1 op T1.COL2 N N T1.COL1 <> T1.COL2 N N COL = ANY (non subq) N N COL = ALL (non subq) N N COL <> ANY (non subq) N N COL <> ALL (non subq) N N COL NOT IN (non subq) N N COL = (cor subq) N N
Stage 2 Predicates - z/OS(Chapter 29, Table 123, page 734 of the Database Administration Guide)
V7 may promote
join Between
11© Sheryl M. Larsen, Inc. 2000-2008
Predicate Type Indexable Stage 1
Stage 1
COL = (cor subq) N N COL = ANY (cor subq) N N COL = ALL (cor subq) N N COL op (cor subq) N N COL op ANY (cor subq) N N COL op ALL (cor subq) N N COL <> (cor subq) N N COL <> ANY (cor subq) N N COL <> ALL (cor subq) N N COL IN (cor subq) N N COL NOT IN (cor subq) N N EXISTS (subq) N N NOT EXISTS (subq) N N expression = value N N expression <> value N N expression op value N N expression op (subquery) N N
Stage 2 Predicates - z/OS(Chapter 29, Table 123, page 734 of the Database Administration Guide)
Not made Indexable,
.
.
V7 just rewrites
the query to a join
Four Points of Filtering – DB2Four Points of Filtering – DB21. Indexable Stage 1 Probe2. Stage 1 Index Filtering 3. Stage 1 Data Filtering4. Stage 2
Type 2 Index
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O O
Root PageO O OC1.C2.C3
44
11
22
33
TOKEN_NR. ROLE_CD
WHERE C.LAST_NM LIKE ? AND C.TOKEN_NR =
B.TOKEN_NRC.ROLE_CD > ?
AND CASE C.SEX WHEN ‘X’ THEN ? END) = ‘ABCDE’
WHERE C.LAST_NM LIKE ? AND C.TOKEN_NR =
B.TOKEN_NRC.ROLE_CD > ?
AND CASE C.SEX WHEN ‘X’ THEN ? END) = ‘ABCDE’
13© Sheryl M. Larsen, Inc. 2000-2008
SQL Performance Rules ….. Promote Stage 2’s/Residuals if possiblePromote Stage 2’s/Residuals if possible
» Value BETWEEN COL1 AND COL2 is Stage 2Value BETWEEN COL1 AND COL2 is Stage 2» Rewrite to: (Value >= COL1 AND value <= COL2)Rewrite to: (Value >= COL1 AND value <= COL2)
» COL NOT IN (K, S, T) = COL IN (known values)COL NOT IN (K, S, T) = COL IN (known values) SELECT only the columns neededSELECT only the columns needed
» Disallow SELECT *Disallow SELECT * SELECT only the rows neededSELECT only the rows needed
» Disallow program filteringDisallow program filtering Use constants and literals if the values will not Use constants and literals if the values will not
change in the next 3 yearschange in the next 3 years» Increase optimizer accuracy for static valuesIncrease optimizer accuracy for static values
WHERE ‘0001-01-01’ BETWEEN B.SCND_ALOC_EFF_DATE AND B.SCND_ALOC_END_DATE
Should be:
WHERE ‘0001-01-01’ >= B.SCND_ALOC_EFF_DATE AND ‘0001-01-01’ <= B.SCND_ALOC_END_DATE
Bad BETWEENs
15© Sheryl M. Larsen, Inc. 2000-2008
…… SQL Performance Rules Make WHERE clause data types matchMake WHERE clause data types match
» Costs more to navigate indexes when not matchingCosts more to navigate indexes when not matching Do not SELECT columns with known static valuesDo not SELECT columns with known static values
» Disallow COLn from SELECT list if COLn = valueDisallow COLn from SELECT list if COLn = value Do not place any local filtering in the ON clause for OUTER Do not place any local filtering in the ON clause for OUTER
JOINs JOINs » Disallow “during join” filtering, encourage “before join”Disallow “during join” filtering, encourage “before join”
Sequence filtering from most restrictive to least restrictive by Sequence filtering from most restrictive to least restrictive by table, by predicate type table, by predicate type
WHEREWHERE A.COL2 = ‘abracadabra’A.COL2 = ‘abracadabra’ ANDAND A.COL1 IN (:hv1, :hv2, :hv3)A.COL1 IN (:hv1, :hv2, :hv3)ANDAND A.COL4 > :hvcol4 A.COL4 > :hvcol4 AND AND A.COL3 > :hvcol3A.COL3 > :hvcol3ANDAND A.COL5 LIKE ‘%SON’A.COL5 LIKE ‘%SON’
Ties
Matter!
16© Sheryl M. Larsen, Inc. 2000-2008
Data Type ConversionsWHERE DATE(col_TS) BETWEEN :date1WHERE DATE(col_TS) BETWEEN :date1
AND :date2AND :date2
WHERE col_TS BETWEEN WHERE col_TS BETWEEN
TIMESTAMP(:date1, ’00:00:00’)TIMESTAMP(:date1, ’00:00:00’)
AND TIMESTAMP(:date2, ’59:59:99’)AND TIMESTAMP(:date2, ’59:59:99’)
Need to leave columns alone and translate the Need to leave columns alone and translate the second argumentsecond argument
17© Sheryl M. Larsen, Inc. 2000-2008
Host Variable Filter Factors (DB2 Database Administration Guide- Properties of Predicates)
COLCARDF Factor for <, <=, >, >= Factor for LIKE or BETWEEN >=100,000,000 1/10,000 3/100,000 >=10,000,000 1/3,000 1/10,000 >=1,000,000 1/1,000 3/10,000 >=100,000 1/300 1/1,000 >=10,000 1/100 3/1,000 >=1,000 1/30 1/100 >=100 1/10 3/100 >=2 1/3 1/10 =1 1/1 1/1 <=0 1/3 1/10
COLCARD Factor for <, <=, >, >= BETWEEN, LIKE
>=100,000,000 1/100,000,000
>=10,000,000 1/10,000,000
>=1,000,000 1/1,000,000
>=100,000 1/100,000
>=10,000 1/10,000
>=1,000 1/1,000
>=100 1/100
>=0 1/3
18© Sheryl M. Larsen, Inc. 2000-2011
SQL Review Checklist1.1. Examine Program logicExamine Program logic
2.2. Examine FROM clauseExamine FROM clause
3.3. Verify Join conditionsVerify Join conditions
4.4. Promote Stage 2’s/Residuals Promote Stage 2’s/Residuals
5.5. Prune SELECT listsPrune SELECT lists
6.6. Verify local filtering sequenceVerify local filtering sequence
7.7. Analyze Access PathsAnalyze Access Paths
8.8. Tune if necessaryTune if necessary
SQL Portfolio- DB2 9 z/OS vs. DB2 9.5 LUW
Inner and Outer Joins, Table Expressions, Subqueries, GROUP BY, Complex Inner and Outer Joins, Table Expressions, Subqueries, GROUP BY, Complex Correlation, Global Temporary Tables, CASE, 100+ Built-in Functions including Correlation, Global Temporary Tables, CASE, 100+ Built-in Functions including
SQL/XML, Limited Fetch, Insensitive Scrollable Cursors, UNION Everywhere, SQL/XML, Limited Fetch, Insensitive Scrollable Cursors, UNION Everywhere, MIN/MAX Single Index Support, Self Referencing Updates with Subqueries, Sort MIN/MAX Single Index Support, Self Referencing Updates with Subqueries, Sort Avoidance for ORDER BY, and Row Expressions 2M Statement Length, GROUP Avoidance for ORDER BY, and Row Expressions 2M Statement Length, GROUP
BY Expression, Sequences, Scalar Fullselect, Materialized Query Tables, BY Expression, Sequences, Scalar Fullselect, Materialized Query Tables, Common Table Expressions, Recursive SQL, CURRENT PACKAGE PATH, Common Table Expressions, Recursive SQL, CURRENT PACKAGE PATH,
VOLATILE Table Support, Star Join Sparse Index, Qualified Column names, VOLATILE Table Support, Star Join Sparse Index, Qualified Column names, Multiple DISTINCT clauses, ON COMMIT DROP, Transparent ROWID Column, Multiple DISTINCT clauses, ON COMMIT DROP, Transparent ROWID Column,
call from trigger, statement isolation, FOR READ ONLY KEEP UPDATE LOCKS, call from trigger, statement isolation, FOR READ ONLY KEEP UPDATE LOCKS, SET CURRENT SCHEMA, client special registers, long SQL Object names, SET CURRENT SCHEMA, client special registers, long SQL Object names,
SELECT FROM SELECT FROM INSERTINSERT, UPDATE, DELETE, MERGE, INSTEAD OF TRIGGER, , UPDATE, DELETE, MERGE, INSTEAD OF TRIGGER, Native SQL Procedure Language, BIGINT, file reference variables, XML, FETCH Native SQL Procedure Language, BIGINT, file reference variables, XML, FETCH
FIRST & ORDER BY IN subselect and fullselect, caseless comparisons, FIRST & ORDER BY IN subselect and fullselect, caseless comparisons, INTERSECT, EXCEPT, not logged tables, DECIMAL FLOAT, XQuery,TRUNCATE, INTERSECT, EXCEPT, not logged tables, DECIMAL FLOAT, XQuery,TRUNCATE,
OLAP Functions, Session variables, OmniFind, Spatial, ROLEOLAP Functions, Session variables, OmniFind, Spatial, ROLE
19© Sheryl M. Larsen, Inc. 2000-2008
GROUPING SETS, ROLLUP, CUBE, Many Built-in Functions, SET CURRENT ISOLATION , multi-site join, MERGE, ARRAY data type,
global variables, Oracle syntax, XML enhancements
Stage1 unlike data types, Multi-row INSERT, FETCH, Multi-row cursor UPDATE, Dynamic Scrollable Cursors, Multiple CCSIDs per statement,
GET DIAGNOSTICS, Enhanced UNICODE, IS NOT DISTINCT FROM, VARBINARY, FETCH CONTINUE, MERGE
SQL Performance Rule Violations & Misuse Cost CPU & I/OCost CPU & I/O Cost time to fixCost time to fix Cost $$$$$$$$$Cost $$$$$$$$$
How much does it cost to misuse SQL?
1. Use joins over subqueries when detail row information is required2. Use subqueries over joins when detail row information is not required3. Use INNER JOIN over LEFT JOIN when exceptions are not expected or
needed4. Use CREATE GLOBAL TEMPORARY TABLE when data is infrequently
accessed5. Use DECLARE GLOBAL TEMPORARY TABLE with a clustered index
when DTT is large and data is frequently accessed
How much does it cost to misuse SQL?
1. Use joins over subqueries when detail row information is required2. Use subqueries over joins when detail row information is not required3. Use INNER JOIN over LEFT JOIN when exceptions are not expected or
needed4. Use CREATE GLOBAL TEMPORARY TABLE when data is infrequently
accessed5. Use DECLARE GLOBAL TEMPORARY TABLE with a clustered index
when DTT is large and data is frequently accessed
DB2 10 z/OS vs. DB2 10 LUW
Inner and Outer Joins, Table Expressions, Subqueries, GROUP BY, Complex Inner and Outer Joins, Table Expressions, Subqueries, GROUP BY, Complex Correlation, Global Temporary Tables, CASE, 100+ Built-in Functions including Correlation, Global Temporary Tables, CASE, 100+ Built-in Functions including
SQL/XML, Limited Fetch, Insensitive Scroll Cursors, UNION Everywhere, MIN/MAX SQL/XML, Limited Fetch, Insensitive Scroll Cursors, UNION Everywhere, MIN/MAX Single Index, Self Referencing Updates with Subqueries, Sort Avoidance for ORDER Single Index, Self Referencing Updates with Subqueries, Sort Avoidance for ORDER
BY, and Row Expressions, 2M Statement Length, GROUP BY Expression, BY, and Row Expressions, 2M Statement Length, GROUP BY Expression, Sequences, Scalar Fullselect, Materialized Query Tables, Common Table Sequences, Scalar Fullselect, Materialized Query Tables, Common Table
Expressions, Recursive SQL, CURRENT PACKAGE PATH, VOLATILE Tables, Star Expressions, Recursive SQL, CURRENT PACKAGE PATH, VOLATILE Tables, Star Join Sparse Index, Qualified Column names, Multiple DISTINCT clauses, ON Join Sparse Index, Qualified Column names, Multiple DISTINCT clauses, ON
COMMIT DROP, Transparent ROWID Column, Call from trigger, statement isolation, COMMIT DROP, Transparent ROWID Column, Call from trigger, statement isolation, FOR READ ONLY KEEP UPDATE LOCKS, SET CURRENT SCHEMA, Client special FOR READ ONLY KEEP UPDATE LOCKS, SET CURRENT SCHEMA, Client special
registers, long SQL object names, SELECT from INSERT, UPDATE or DELETE, registers, long SQL object names, SELECT from INSERT, UPDATE or DELETE, INSTEAD OF TRIGGER, Native SQL Procedure Language, BIGINT, file reference INSTEAD OF TRIGGER, Native SQL Procedure Language, BIGINT, file reference variables, XML, FETCH FIRST & ORDER BY in subselect & fullselect, caseless variables, XML, FETCH FIRST & ORDER BY in subselect & fullselect, caseless
comparisons, INTERSECT, EXCEPT, not logged tables, OmniFind, spatial, range comparisons, INTERSECT, EXCEPT, not logged tables, OmniFind, spatial, range partitions, data compression, session variables, DECIMAL FLOAT, optimistic partitions, data compression, session variables, DECIMAL FLOAT, optimistic
locking, ROLE, TRUNCATE, index & XML compression, created temps, inline LOB, locking, ROLE, TRUNCATE, index & XML compression, created temps, inline LOB, administrative privileges, administrative privileges, implicit cast, date/time changes, currently committed, implicit cast, date/time changes, currently committed, moving sum & average, index include columns, row and column access control, moving sum & average, index include columns, row and column access control,
time travel querytime travel query
21
Updateable UNION in Views, GROUPING SETS, ROLLUP, CUBE, more Built-in Functions, SET CURRENT ISOLATION, multi-site join, MERGE, MDC, XQuery, XML enhancements, additional data type (array, row, cursor), global variables, even more vendor syntax, temp table compression, MODULEs
Multi-row INSERT, FETCH & multi-row cursor UPDATE, Dynamic Scrollable Cursors, GET DIAGNOSTICS, Enhanced UNICODE SQL, join across encoding schemes, IS NOT DISTINCT FROM, VARBINARY, FETCH CONTINUE, MERGE, SELECT from MERGE, routine versioning, timestamps w/timezone
DB2 11 SQL – Standard SQL support (not exhaustive, some features may be missing)
DB2 11 for z/OS and DB2 10.5 Linux, Unix & Windows
Multi-row INSERT, FETCH & multi-row cursor UPDATE, Dynamic Scrollable Cursors, GET DIAGNOSTICS, Enhanced UNICODE SQL, join across encoding schemes, IS NOT DISTINCT FROM, VARBINARY, FETCH CONTINUE, SELECT FROM MERGE, MERGE, routine versioning, transparent archive query
Inner and Outer Joins, Table Expressions, Subqueries, GROUP BY, Complex Correlation, Global Temporary Tables, CASE, 100+ Built-in Functions including SQL/XML, Limited Fetch, Insensitive Scroll Cursors, UNION Everywhere, MIN/MAX Single Index, Self Referencing Updates with Subqueries, Sort Avoidance for ORDER BY, and Row Expressions, 2M Statement Length, GROUP BY Expression, Sequences, Scalar Fullselect, Materialized Query Tables, Common Table Expressions, Recursive SQL, CURRENT PACKAGE PATH, VOLATILE Tables, Star Join Sparse Index, Qualified Column names, Multiple DISTINCT clauses, ON COMMIT DROP, Transparent ROWID Column, Call from trigger, statement isolation, FOR READ ONLY KEEP UPDATE LOCKS, SET CURRENT SCHEMA, Client special registers, long SQL object names, SELECT from INSERT, UPDATE or DELETE, INSTEAD OF TRIGGER, SQL PL in routines, BIGINT, file reference variables, XML, FETCH FIRST & ORDER BY in subselect & fullselect, caseless comparisons, INTERSECT, EXCEPT, MERGE not logged tables, OmniFind, spatial, range partitions, data compression, DECFLOAT, optimistic locking, ROLE, TRUNCATE, index & XML compression, created temps, inline LOB, administrative privileges, implicit cast, increased timestamp precision, currently committed, moving sum & average, index include columns, row and column access controls, time travel query, GROUPING SETS, ROLLUP, CUBE, global variables, Text Search functions, accelerated tables, DROP COLUMN, array data type, XML enhancements
Updateable UNION in Views, more Built-in Functions, SET CURRENT ISOLATION, multi-site join, full MERGE, MDC, XQuery, additional data type (row, cursor), even more vendor syntax, temp table compression, MODULEs
z
luw
common
SQL Skill Self Assessment Level Assessment = YOU CAN FULLY UNDERSTAND THE FEATURE AND PROPER USE OF:
0 You think SQL is a new energy drink
1 Simple SELECT statements, WITH clause, ORDER BY
2
WHERE clauses, BETWEEN, LIKE, IN(list), =, >=, >, <, <=, <>, NOT IN(list), NOT LIKE, NOT BETWEEN, DISTINCT
3
Table joins (inner, outer, full), UNION, UNION ALL, CONCAT, static CURSORs, FOR UPDATE OF, ROW_NUMBER, EXCEPT, INTERCEPT
4
noncorrelated and correlated subqueries, EXISTS, NOT EXISTS, FETCH FIRST x ROWS ONLY, OPTIMIZE FOR x ROWS, MERGE, TRUNCATE
5
Indexable, Stage1 and Stage 2 predicate evaluation, multirow FETCH/INSERT, GET DIAGNOSTICS, Scalar full SELECT
6
Table expressions/common table expressions, GROUP BY, HAVING, IS NOT DISTINCT FROM, embedded dynamic SQL, ORDER BY/FETCH FIRST in subselect, FETCH CONTINUE
7
CASE expressions, Global Temporary Table (GTT), Declared Temporary Table (DTT), Dynamic Scrollable cursors, SEQUENCES/IDENTITY
8
Queries involving > 10 tables, INSERT within (SELECT , UPDATE, DELETE, MERGE), Star Schema, GROUP BY expression
9
MQT (Materialized Query Tables), Recursive SQL, UNION in Views, > 30 useful Built-in Functions, DENSE_RANK, RANK
10
Codes effective and efficient SQL applying performance rules and knows when to use each SQL feature appropriately
Name: __________________
Before:_______
After:________
24© Sheryl M. Larsen, Inc. 2000-2011
When to Tune Queries Not until the query is coded the best it can beNot until the query is coded the best it can be All predicates are the best they can beAll predicates are the best they can be
» Promote Stage 2’s if possiblePromote Stage 2’s if possible» Promote Stage 1’s if possiblePromote Stage 1’s if possible» Apply performance rulesApply performance rules
Check Access Paths of all Query BlocksCheck Access Paths of all Query Blocks Apply data knowledge and program knowledge to Apply data knowledge and program knowledge to
predict response timepredict response time If, and only if, the predicted service levels are not If, and only if, the predicted service levels are not
met - TUNE!met - TUNE!
25© Sheryl M. Larsen, Inc. 2000-2011
Why Tune Queries Performance!Performance! Host variables force the optimizer to guessHost variables force the optimizer to guess IBM optimizer researchers and developers IBM optimizer researchers and developers
provide extremely good guessesprovide extremely good guesses Occasionally (2-7% of queries) they guess Occasionally (2-7% of queries) they guess
wrongwrong Have no fear, changing the optimizer’s mind Have no fear, changing the optimizer’s mind
is easier than you think is easier than you think
26© Sheryl M. Larsen, Inc. 2000-2011
How to Tune Queries Do not change statistics, just keep accurateDo not change statistics, just keep accurate Do not panicDo not panic Choose a proven, low maintenance, tuning Choose a proven, low maintenance, tuning
techniquetechnique IBM’s list:IBM’s list:
» OPTIMIZE FOR n ROWSOPTIMIZE FOR n ROWS» FETCH FIRST n ROWS ONLYFETCH FIRST n ROWS ONLY» No Op (+0, CONCAT ‘ ‘)No Op (+0, CONCAT ‘ ‘)» TX.CX=TX.CXTX.CX=TX.CX» REOPT(VARS) REOPT(VARS) » ON 1=1ON 1=1
SQL Tuning ExamplesWHERE S.SALES_ID > 44 AND S.MNGR = :hv-mngr AND S.REGION BETWEEN
:hvlo AND :hvhi CONCAT ‘ ‘
27© Sheryl M. Larsen, Inc. 2000-2011
SELECT S.QTY_SOLD, S.ITEM_NO, S.ITEM_NAME
FROM SALE SWHERE S.ITEM_NO > :hvORDER BY ITEM_NOFETCH FIRST 22 ROWS ONLY
WHERE B.BID BETWEEN :hvlo AND :hvhi
AND B.BID = D.DID AND B.SID = S.SID AND B.COL2 >= :hv
AND B.COL3 >= :hv
AND B.COL4 >= :hv
No Operation
Limited Fetch
Fake Filter
28© Sheryl M. Larsen, Inc. 2000-2011
Tuning Tools Sheryl’s Extended ListSheryl’s Extended List
» Fake FilteringFake Filtering• COL BETWEEN :hv1 AND :hv2COL BETWEEN :hv1 AND :hv2• COL >= :hvCOL >= :hv
» Table expressions with DISTINCTTable expressions with DISTINCT• FROM (SELECT DISTINCT COL1, COL2 …..)FROM (SELECT DISTINCT COL1, COL2 …..)
» Anti-JoinsAnti-Joins» Extreme ExperimentsExtreme Experiments» Index ChangesIndex Changes» MQT DesignMQT Design
29© Sheryl M. Larsen, Inc. 2000-2011
What Used to Work – z/OS
UNION queries for index access – until V3 UNION queries for index access – until V3 Multiple Index AccessMultiple Index Access
(Predicate OR 0 = 1) – until V5 No Operations(Predicate OR 0 = 1) – until V5 No Operations Layered Table Expressions – until V6 smarter Layered Table Expressions – until V6 smarter
optimizeroptimizer Correlated Subqueries – until V7 query re-writeCorrelated Subqueries – until V7 query re-write No cross table indexes - until V8 Materialized No cross table indexes - until V8 Materialized
Query Tables (more on that later)Query Tables (more on that later)
30
Steps in Tuning Queries
Step 1 - Learn Access PathsStep 1 - Learn Access Paths
Step 2 – Interpret the Optimizer DecisionsStep 2 – Interpret the Optimizer Decisions
Step 3 – Find Top CPU ConsumersStep 3 – Find Top CPU Consumers
Step 4 – Learn Tuning TechniquesStep 4 – Learn Tuning Techniques
Step 5 – Tune if NecessaryStep 5 – Tune if Necessary
Step 6 – Designing Performance Step 6 – Designing Performance StructuresStructures
31
Step 1
Learn the DB2 Access PathsLearn the DB2 Access Pathswww.smlsql.com
www.ca.com/ Google Sheryl Larsen Google Sheryl Larsen
32© Sheryl M. Larsen, Inc. 2000-2011
All the Possible Access Paths
Direct Row
Star
Merge Scan
Hybrid
Nested Loop
Partitioned Table Scan
Table Scan
Limited Partition ScanWith NPI
Limited Partition ScanWith Partitioning Index
Partition ScanWithout Index
Multiple Index Access
List Prefetch
NonMatchingIndex Access
Matching Index Access
One Fetch
IN(list) Index Access
Merge ScanMerge Scan
Star Join: Star Join: Cartesian Cartesian or or Pair-wisePair-wise
Hybrid Join: Hybrid Join: Type C or Type C or Type NType N
Nested LoopNested Loop
Limited Partition ScanLimited Partition ScanUsing Data Partitioned Using Data Partitioned Secondary IndexSecondary Index
Limited Partition ScanLimited Partition ScanUsing Partitioning IndexUsing Partitioning Index
Multiple Index AccessMultiple Index Access
List PrefetchList Prefetch
NonMatchingNonMatchingIndex AccessIndex Access
Matching Index AccessMatching Index Access
Sparse Index AccessSparse Index Access
One FetchOne Fetch
IN(list) Index Access IN(list) Index Access
(Bold names use an Index)
33© Sheryl M. Larsen, Inc. 2000-2011
Variations of Index Accesses
One Fetch IN(list) Index Access
Matching Index Access
NonMatching Index AccessSparse Index Access
List Prefetch
Multiple Index Access
Limited Partition Scan With Partitioning Index
Limited Partition ScanWith NPSI
Limited Partition ScanWith DPSI
Multidimensional Index Access
All can be parallelAll can be parallel All can omit data accessAll can omit data access Clustering index or Clustering index or
nonclustering accessnonclustering access
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O O
Root PageO O O
Rid + value(s)
rang
e
Bid
34© Sheryl M. Larsen, Inc. 2000-2011
One Fetch
Type 2 Index
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O O
Root PageO O O
SELECT MIN(C1)FROM T1
SELECT MIN(C1)FROM T1
SELECT MAX(C3)FROM T1WHERE C1 = ‘L’ AND C2 = 99
SELECT MAX(C3)FROM T1WHERE C1 = ‘L’ AND C2 = 99
C1.C2.C3
3311
22
35© Sheryl M. Larsen, Inc. 2000-2011
Matching Index Access
Type 2 Index
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O O
Root PageO O O
WHERE C1 = ‘L’ AND C2 > 99 AND C3 = :hv
WHERE C1 = ‘L’ AND C2 > 99 AND C3 = :hv
C1.C2.C311
22
33
36© Sheryl M. Larsen, Inc. 2000-2011
NonMatching Index Access
Type 2 Index
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O O
Root PageO O O
WHERE C2 > 99 AND C3 = :hv
WHERE C2 > 99 AND C3 = :hv
C1.C2.C3
11
22
37© Sheryl M. Larsen, Inc. 2000-2011
Type 2 Index
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O O
Root PageO O O
IN(list) Index Access
WHERE C1 IN (‘K’, ‘S’, ‘T’) AND C2 > 99 AND C3 = :hv
WHERE C1 IN (‘K’, ‘S’, ‘T’) AND C2 > 99 AND C3 = :hv
C1.C2.C3
3311 22
38© Sheryl M. Larsen, Inc. 2000-2011
Type 2 Index
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O O
Root PageO O O
IN(list) Index Access -Parallel
WHERE C1 IN (‘K’, ‘S’, ‘T’) AND C2 > 99 AND C3 = :hv
WHERE C1 IN (‘K’, ‘S’, ‘T’) AND C2 > 99 AND C3 = :hv
C1.C2.C3
1111 11
39© Sheryl M. Larsen, Inc. 2000-2011
List Prefetch
WHERE C1 > 50 AND C2 = :hv
WHERE C1 > 50 AND C2 = :hv
COL1 Index
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O O
Root PageO O O
33
11
22
44
40© Sheryl M. Larsen, Inc. 2000-2011
Multiple Index Access
COL1 Index
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O O
Root PageO O O
CO L2 Index
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O
Root PageO O O
AND/OR
WHERE C1 > 50WHERE C1 > 50WHERE C2 = :hvWHERE C2 = :hv
C1 C2
33
11
2244
55
6677
88
41© Sheryl M. Larsen, Inc. 2000-2011
Sparse Index Access
Use Any Single Table Access
MethodAnd Filter
Use Any Single Table Access
MethodAnd Filter
33
11 22
42© Sheryl M. Larsen, Inc. 2000-2011
Type 2 Index
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O O
Root PageO O O
Type 2 Index
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O O
Root PageO O O
Type 2 Index
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O O
Root PageO O O
C1 PartsPartitioning Index – Partitioning Index – ClusteredClustered
Limited Partition ScanUsing Clustered Partitioning Index
WHERE C1 IN (‘K’, ‘S’, ‘T’) AND C2 > 99 AND C3 = :hv
WHERE C1 IN (‘K’, ‘S’, ‘T’) AND C2 > 99 AND C3 = :hv
1111 11
2222 22
3333 33
43© Sheryl M. Larsen, Inc. 2000-2011
WHERE CORP IN (20, 40,60) AND
TICKET_PREFIX = ‘A’ AND CUST_NAME
LIKE ‘%CAMB%’
WHERE CORP IN (20, 40,60) AND
TICKET_PREFIX = ‘A’ AND CUST_NAME
LIKE ‘%CAMB%’
Limited Partition ScanUsing Nonclustered Partitioning Index
Type 2 Index
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O O
Root PageO O O
Type 2 Index
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O O
Root PageO O O
Type 2 Index
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O O
Root PageO O O
CORPPartitioning Index – Partitioning Index – NonclusteredNonclustered
1111 11
2222 22
3333 33
44© Sheryl M. Larsen, Inc. 2000-2011
WHERE TOKEN_NR BETWEEN ? AND ? AND TICKET_PREFIX = ‘A’ AND LAST_NM LIKE ‘LAR%’
WHERE TOKEN_NR BETWEEN ? AND ? AND TICKET_PREFIX = ‘A’ AND LAST_NM LIKE ‘LAR%’
Scan for Last NameUsing Nonclustered Partitioning Index
Type 2 Index
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O O
Root PageO O O
Type 2 Index
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O O
Root PageO O O
Type 2 Index
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O O
Root PageO O O
TOKEN_NR
Partitioning Index – Partitioning Index – NonclusteredNonclustered
1111 11
2222 22
3333 33
45© Sheryl M. Larsen, Inc. 2000-2011
Type 2 Index
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O O
Root PageO O O
Type 2 Index
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O O
Root PageO O O
Type 2 Index
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O O
Root PageO O O
TOKEN_NR PartsPartitioning Index – Partitioning Index –
ClusteredClustered
Limited Partition ScanUsing Clustered Partitioning Index
WHERE TOKEN_NR BETWEEN ? AND ? AND LAST_NM > 99 AND ROLE_CD = ?
WHERE TOKEN_NR BETWEEN ? AND ? AND LAST_NM > 99 AND ROLE_CD = ?
1111 11
2222 22
3333 33
11
33
46© Sheryl M. Larsen, Inc. 2000-2011
Limited Partition Scan Using DPSI
DPSI = DPSI = DData ata PPartitioned artitioned SSecondary econdary IIndexndexClustered byClustered by
LAST_NMLAST_NM
Type 2 Index
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O O
Root PageO O O
Type 2 Index
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O O
Root PageO O O
Type 2 Index
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O O
Root PageO O O
TOKEN_NR
Partitioning by Partitioning by TOKEN_NRTOKEN_NR
111111
2222 22
3333 33
WHERE TOKEN_NR BETWEEN ? AND ?
AND LAST_NM LIKE ‘%LAR
WHERE TOKEN_NR BETWEEN ? AND ?
AND LAST_NM LIKE ‘%LAR
47© Sheryl M. Larsen, Inc. 2000-2011
Helps Improve Performance
Partition/Cluster by Date
Partition by DateCluster by Custid
TOKEN_NR BETWEEN ? AND ?AND LAST_NM_LIKE ?old
New
Partition/Cluster by DatePartition/Cluster
by DatePartition/Cluster by DatePartition/Cluster
by DatePartition/Cluster by TOKEN_NR
Partition by DateCluster by CustidPartition by Date
Cluster by CustidPartition by DateCluster by CustidPartition by TOKEN_NRCluster by LAST_NM
BP03
TOKEN_NR BETWEEN ? AND ?AND LAST_NM_LIKE ?
48© Sheryl M. Larsen, Inc. 2000-2011
Limited Partition Scan Using DPSI
Data PartitionedData PartitionedSecondary IndexSecondary Index
DPSIDPSI
Clustered byClustered byCustomerCustomer
Type 2 Index
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O O
Root PageO O O
Type 2 Index
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O O
Root PageO O O
Type 2 Index
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O O
Root PageO O O
C1 Parts
Partitioning by DatePartitioning by Date
111111
2222 22
3333 33
49© Sheryl M. Larsen, Inc. 2000-2011
Variations of Table Access
All can be parallel All can be parallel
If not enough room in If not enough room in memory, at run time memory, at run time create sparse index create sparse index insteadinstead
SegmentedPartitioned
Limited PartitionedIn Memory Data Cache
50© Sheryl M. Larsen, Inc. 2000-2011
Table Scan
11
WHERE C1 BETWEEN :lowest AND
:highest
WHERE C1 BETWEEN :lowest AND
:highest
51© Sheryl M. Larsen, Inc. 2000-2011
Partitioned Table Scan
Part 1Part 1 Part 2 Part 3 Part 4 Part 5 Part 6 Part 7 Part 8
11
WHERE C1 BETWEEN :lowest AND
:highest
WHERE C1 BETWEEN :lowest AND
:highest
52© Sheryl M. Larsen, Inc. 2000-2011
Limited Partitioned Table Scan
Part 1Part 1 Part 3 Part 4 Part 16 Part 17 Part 18
11
WHERE C1 IN (1, 3, 4, 16, 17, 18)WHERE C1 IN (1, 3, 4, 16, 17, 18)
53© Sheryl M. Larsen, Inc. 2000-2011
Variations of Join Methods
All choose outer table and All choose outer table and filter firstfilter first
All can be parallel (Star All can be parallel (Star CPU only )CPU only )
Worry about join table Worry about join table sequence instead of join sequence instead of join methodmethod
Nested LoopHybrid Join Type CHybrid Join Type NMerge Scan Join
Star Join – CartesianStar Join – Pair Wise
54© Sheryl M. Larsen, Inc. 2000-2011
COL1 Index
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O O
Root PageO O O
Inner Index
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O O
Root PageO O O
11
22
33
COL1 Index
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O O
Root PageO O O
Nested Loop Join
44
WHERE T1.ORDER >
222 AND
T1.ID = T2.IDAND
T2.COLOR IN (‘BLUE’,
‘YELLOW’, ‘GREEN’)
WHERE T1.ORDER >
222 AND
T1.ID = T2.IDAND
T2.COLOR IN (‘BLUE’,
‘YELLOW’, ‘GREEN’)
ID.COLOR index
55© Sheryl M. Larsen, Inc. 2000-2011
Filtered outerRIDS
RIDS
Hybrid Join – Type N
22
CO L2 Index
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O
Root PageO O O
44
11
33
COL1 Index
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O O
Root PageO O O
33
44
55
66
56© Sheryl M. Larsen, Inc. 2000-2011
11
22
33
CO L2 Index
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O
Root PageO O O
Hybrid Join – Type CCOL1 Index
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O O
Root PageO O O
44
57© Sheryl M. Larsen, Inc. 2000-2011
Merge Scan Join
2211
33
44
WHERE T1.C1 = T2.CA ANDT1.C2 = T2.CB AND
T1.C3 = T2.CC
WHERE T1.C1 = T2.CA ANDT1.C2 = T2.CB AND
T1.C3 = T2.CC
58© Sheryl M. Larsen, Inc. 2000-2008
Star Join - Cartesian
33
66
11
44
55
Inner Index
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O O
Root PageO O O
22
Big/Fact Table Index
Small/Dimension Tables
59© Sheryl M. Larsen, Inc. 2000-2011
Pair-Wise Star Join
Type 2 Index
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O O
Root PageO O O
Type 2 Index
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O O
Root PageO O O
Type 2 Index
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O O
Root PageO O O
Type 2 Index
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O O
Root PageO O O
Type 2 Index
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O O
Root PageO O O 33
66
11
44
55
22
77
99
88
60© Sheryl M. Larsen, Inc. 2000-2011
Read Direct Access 1.1. Create table with ROWID Create table with ROWID
type column (DIRECT_ID)type column (DIRECT_ID)
2.2. SELECT DIRECT_IDSELECT DIRECT_IDINTO INTO :direct-id:direct-idFROM TAB12FROM TAB12WHERE UKEY = ‘AMX’WHERE UKEY = ‘AMX’
3.3. UPDATE TAB12UPDATE TAB12WHERE WHERE DIRECT_ID = DIRECT_ID = :direct-id:direct-id
DIRECT_ID
AMX DIRECT_ID
DIRECT_ID
DIRECT_ID33
11
22
61© Sheryl M. Larsen, Inc. 2000-2011
All the Possible Access Paths
Direct Row
Star
Merge Scan
Hybrid
Nested Loop
Partitioned Table Scan
Table Scan
Limited Partition ScanWith NPI
Limited Partition ScanWith Partitioning Index
Partition ScanWithout Index
Multiple Index Access
List Prefetch
NonMatchingIndex Access
Matching Index Access
One Fetch
IN(list) Index Access
Merge ScanMerge Scan
Star Join: Star Join: Cartesian Cartesian or or Pair-wisePair-wise
Hybrid Join: Hybrid Join: Type C or Type C or Type NType N
Nested LoopNested Loop
Limited Partition ScanLimited Partition ScanUsing Data Partitioned Using Data Partitioned Secondary IndexSecondary Index
Limited Partition ScanLimited Partition ScanUsing Partitioning IndexUsing Partitioning Index
Multiple Index AccessMultiple Index Access
List PrefetchList Prefetch
NonMatchingNonMatchingIndex AccessIndex Access
Matching Index AccessMatching Index Access
Sparse Index AccessSparse Index Access
One FetchOne Fetch
IN(list) Index Access IN(list) Index Access
62
Step 2
Interpret the Optimizer’s Interpret the Optimizer’s DecisionsDecisions
63© Sheryl M. Larsen, Inc. 2000-2011
Determine If Tuning Is Necessary
Find out what the general access path is for each Find out what the general access path is for each query block (subselect, union, table expression)query block (subselect, union, table expression)
Calculate execution frequencies of each query Calculate execution frequencies of each query block block » Correlated subselects execute bottom query block once Correlated subselects execute bottom query block once
for each qualifying row in bottom queryfor each qualifying row in bottom query Identify join table sequenceIdentify join table sequence Identify materialization (intermediate or final)Identify materialization (intermediate or final) Identify sorts (even hidden Rid sorts an IN(list)Identify sorts (even hidden Rid sorts an IN(list) Identify User Defined Table or Column functionsIdentify User Defined Table or Column functions
64© Sheryl M. Larsen, Inc. 2000-2011
Using EXPLAIN
NoNo
QblockQblock
NoNo
StepStep
NoNo
MethMeth AccessAccess
TypeType
MatchMatch
ColsCols
PrePre
FetchFetch
JoinJoin
TypeType
IXIX
OnlyOnly
IXIX
NameName
11 11 11 00 RR 00 SS NN
11 11 22 44 II 22 LL NN IX22IX22
22 11 11 00 NN 22 YY
22 11 22 22 II 22 FF NN IX27IX27
22 11 33 44 II 22 LL NN IX42IX42
33 11 11 00 II 44 NN IXF1IXF1
33 22 11 22 RR 00 SS NN
EXPLAIN PLAN SET QUERYNO = n FOR …..;SELECT * FROM PLAN_TABLE ORDER BY …..;
65© Sheryl M. Larsen, Inc. 2000-2011
Using EXPLAIN
SORTNSORTN
U J O GU J O G
SORTCSORTC
U J O GU J O G
PagePage
RangeRange
ColCol
FuncFunc
CorrCorr
NameName
QblockQblock
TypeType
OptOpt
HintHint
HintHint
UsedUsed
TSTS RemarkRemarkss
N N N N N N N N N N Y NN N Y N YY SS ABAB SELECTSELECT GGGG GGGG …….. Good 1Good 1
N N N NN N N N N N N NN N N N SS CDCD UPDATEUPDATE NNNN …….. Bad 1 Bad 1
N Y N NN Y N N N N N NN N N N YY EFEF INSERTINSERT ……..
N N N NN N N N N N N NN N N N YY RR XXXX DELECTDELECT …….. +0+0
N N N NN N N N N N N NN N N N YY GAGA NCOSUBNCOSUB XXXX …….. Bogus 3 Bogus 3
N N N NN N N N N N N NN N N N YY RR SLSL CORSUBCORSUB XXXX XXXX …….. Extra > Extra >
N N N NN N N N N N N NN N N N MLML DELCURDELCUR ……..
N N N NN N N N N N N NN N N N RR KLKL UPDCURUPDCUR …….. parensparens
N N N NN N N N N N N NN N N N SS TLTL SELUPDSELUPD SSSS SSSS …….. helphelp
66© Sheryl M. Larsen, Inc. 2000-2011
Learning EXPLAIN Need IBM’s definition of the PLAN_TABLENeed IBM’s definition of the PLAN_TABLE Need a way to transform PLAN_TABLE into Need a way to transform PLAN_TABLE into
something readable (i.e. QMF Form or something readable (i.e. QMF Form or program)program)
Review PLAN_TABLE contents with Access Review PLAN_TABLE contents with Access Path descriptionsPath descriptions
Study, practice, practice, practiceStudy, practice, practice, practice
67© Sheryl M. Larsen, Inc. 2000-2011
Using DSN_STATEMENT_TABLE
A = No Defaults used in estimateA = No Defaults used in estimate B = Check Reason columnB = Check Reason column
» Host variblesHost varibles» CardinalityCardinality» UDFUDF» TriggersTriggers» RIRI
Explain Explain TimeTime
StmtStmt
TypeType
Cost Cost
CatCat
PROCMSPROCMS PROCSUPROCSU ReasonReason
TimeTime
StampStamp
SELSEL
UPD…UPD…
A orA or
BB
MilliMilli
secondsseconds
ServiceService
UnitsUnits
For Cat For Cat BB
68© Sheryl M. Larsen, Inc. 2000-2011
Service Units http://www-03.ibm.com/servers/eserver/zseries/srm/
zSeries 990
Processor Model STIDP Type STSI Model Name#C
PSU/SEC SRMsec/RealSec
2084-301 2084 301 1 21857.9235 508.1298
2084-302 2084 302 2 20752.2698 508.1298
2084-303 2084 303 3 20075.2823 508.1298
2084-304 2084 304 4 19559.9022 508.1298
69© Sheryl M. Larsen, Inc. 2000-2011
Service Units
2097-701 2097 701 1 47619.0476 1106.1941
2097-702 2097 702 2 44692.7374 1106.1941
2097-703 2097 703 3 43010.7527 1106.1941
2097-704 2097 704 4 41666.6667 1106.1941
2097-501 2097 501 1 24427.4809 568.1815
2097-502 2097 502 2 23021.5827 568.1815
2097-503 2097 503 3 22222.2222 568.1815
Processor Model STIDP TypeSTSI Model
Name#CP SU/SEC SRMsec/RealSec
2097-401 2097 401 1 11291.4608 262.6049
2097-402 2097 402 2 10680.9079 262.6049
IBM System z10
SQL Tuning using EXPLAIN EXPLAIN ALL Expensive Queries EXPLAIN ALL Expensive Queries
-- Query Joins PLAN_TABLE -- Query Joins PLAN_TABLE with DSN_STATEMENTwith DSN_STATEMENT
BLOCK PLAN METH TABLE_NAME PQB BLOCK PLAN METH TABLE_NAME PQB TYPETYPE MCMC ANAME IO ANAME IO
--+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+-------
01 01 00 R10_CLM_FACT12 12 01 01 00 R10_CLM_FACT12 12 M M 00 N 00 N
01 01 00 R10_CLM_FACT12 12 01 01 00 R10_CLM_FACT12 12 MX MX 01 01 X011_10_12_F_IX07X011_10_12_F_IX07 Y Y
01 01 00 R10_CLM_FACT12 12 01 01 00 R10_CLM_FACT12 12 MXMX 01 X011_10_12_F_IX04 Y 01 X011_10_12_F_IX04 Y
01 01 00 R10_CLM_FACT12 12 01 01 00 R10_CLM_FACT12 12 MI MI 00 N 00 N
01 01 00 R10_CLM_FACT12 12 01 01 00 R10_CLM_FACT12 12 MX MX 01 X011_10_12_F_IX04 Y 01 X011_10_12_F_IX04 Y
01 01 00 R10_CLM_FACT12 12 MX 01 X011_10_12_F_IX04 01 01 00 R10_CLM_FACT12 12 MX 01 X011_10_12_F_IX04 YY
01 01 00 R10_CLM_FACT12 12 MU 00 N 01 01 00 R10_CLM_FACT12 12 MU 00 N
01 01 00 R10_CLM_FACT12 12 MI 00 N 01 01 00 R10_CLM_FACT12 12 MI 00 N
01 01 0202 01 OTR_PRTY_LIAB_NET 12 N 03 OPL_NET_IX04 N 01 OTR_PRTY_LIAB_NET 12 N 03 OPL_NET_IX04 N
01 03 01 MEMBER_ELIG_NET 12 N 04 MBR_ELIG_NET_IX06 Y 01 03 01 MEMBER_ELIG_NET 12 N 04 MBR_ELIG_NET_IX06 Y
01 04 01 R10_CHG_FACT12 12 N 03 X007_10_12_F_IX19 N 01 04 01 R10_CHG_FACT12 12 N 03 X007_10_12_F_IX19 N
01 05 01 05 0101 R10_MULTI_CD_FAC12 12 I 02 X021_10_12_F_IX14 N R10_MULTI_CD_FAC12 12 I 02 X021_10_12_F_IX14 N
02 01 00 R10_CHG_FACT12 09 N 03 X007_10_12_F_IX19 Y 02 01 00 R10_CHG_FACT12 09 N 03 X007_10_12_F_IX19 Y
[email protected]©Copyright Sheryl Larsen 2011
Type of Access
# of Matching Columns
Index Name
Index Only?
Step #
Join Method
Sort, Prefetch, & Partition Usage Columns Sort N = New/Inner SortSort N = New/Inner Sort Sort C = Outer/Composite SortSort C = Outer/Composite SortSNSNUU SN SNJJ SN SNOO SN SNGG SCU SCJ SCO SCG PF QB_TYPE STMT_TYPE T_TP SCU SCJ SCO SCG PF QB_TYPE STMT_TYPE T_TP PRPR JT JT
+---------+---------+---------+---------+---------+---------+---------+-------+---------+---------+---------+---------+---------+---------+---------+-------
N N N N N N N N L N N N N N N N N L NCOSUBNCOSUB SELECT T SELECT T
N N N N N N N N S NCOSUB SELECT T N N N N N N N N S NCOSUB SELECT T
N N N N N N N N S NCOSUB SELECT T N N N N N N N N S NCOSUB SELECT T
N N N N N N N N NCOSUB SELECT T N N N N N N N N NCOSUB SELECT T
N N N N N N N N S NCOSUB SELECT T N N N N N N N N S NCOSUB SELECT T
N N N N N N N N N N N N N N N N SS NCOSUB SELECT T NCOSUB SELECT T
N N N N N N N N NCOSUB SELECT T N N N N N N N N NCOSUB SELECT T
N N N N N N N N NCOSUB SELECT T N N N N N N N N NCOSUB SELECT T
N N N N N N N N NCOSUB SELECT T N N N N N N N N NCOSUB SELECT T
N N N N N N N N NCOSUB SELECT T N N N N N N N N NCOSUB SELECT T
N N N N N N N N NCOSUB SELECT T N N N N N N N N NCOSUB SELECT T
N N N N N N N N NCOSUB SELECT T N N N N N N N N NCOSUB SELECT T
N N N N N N N N CORSUB SELECT T N N N N N N N N CORSUB SELECT T
©Copyright Sheryl Larsen 2011
Type of Query Block
Limited Partition Scan?
Prefetch Type
Unique
Join
Order
By
Group
By
Parallelism and Cost Columns Sort N = New/Inner SortSort N = New/Inner Sort Sort C = Outer/Composite SortSort C = Outer/Composite SortPM ADGREE JDGREE COST_CAT PROCMS PM ADGREE JDGREE COST_CAT PROCMS PROCSU PROCSU
--+---------+---------+---------+---------+---------+----+---------+---------+---------+---------+---------+--
C C 4 4 ------ ------ B B 710 21495 710 21495
C ------ ------ B 710 21495 C ------ ------ B 710 21495
C ------ ------ B 710 21495 C ------ ------ B 710 21495
C C ------ ------ B 710 21495 ------ ------ B 710 21495
C ------ ------ B 710 21495 C ------ ------ B 710 21495
C ------ ------ B 710 21495 C ------ ------ B 710 21495
C ------ ------ B 710 21495 C ------ ------ B 710 21495
C ------ ------ B 710 21495 C ------ ------ B 710 21495
C 4 4 B 710 21495 C 4 4 B 710 21495
C 4 4 B 710 21495 C 4 4 B 710 21495
C 4 4 B 710 21495 C 4 4 B 710 21495
C 4 4 B 710 21495 C 4 4 B 710 21495
[email protected]©Copyright Sheryl Larsen 2011
Processor Service Units
# of I/O Streams
Contains :hostvars or cardinality issue
CPU Parallelism
73
Step 3
Fetch Top CPU ConsumersFetch Top CPU Consumers
Tuning SQL FIND ALL Expensive Queries FIND ALL Expensive Queries
---------+---------+------------+---------+---
PROGNAME PROCSU PROGNAME PROCSU
---------+---------+------------+---------+---
EXPNPROG 121,059,664 EXPNPROG 121,059,664 EXPNPROG 21,059,664 EXPNPROG 21,059,664
ONESECPG 79,664 ONESECPG 79,664
SUBSECPG SUBSECPG 9,664 9,664
CHEEPPRG CHEEPPRG 64 64
FREEPROG FREEPROG 4 4
[email protected]©Copyright Sheryl Larsen 2011
PROCSU isTo Expensive to Calculate!
[email protected]©Copyright Sheryl Larsen 2011
75
2,147,483,6472,147,483,6472,147,483,6472,147,483,6472,147,483,6472,147,483,6472,147,483,6472,147,483,6472,147,483,6472,147,483,6472,147,483,6472,147,483,6472,147,483,6472,147,483,6472,147,483,6472,147,483,6472,147,483,6472,147,483,6472,147,483,647
76
Step 4
Learn Traditional and Extreme Learn Traditional and Extreme Tuning TechniquesTuning Techniques
77© Sheryl M. Larsen, Inc. 2000-2011
Tuning Techniques to Apply When Necessary
Experiment with Extreme Tuning TechniquesExperiment with Extreme Tuning TechniquesDISTINCT Table ExpressionsDISTINCT Table ExpressionsCorrelated Table ExpressionsCorrelated Table ExpressionsOdd/old TechniquesOdd/old TechniquesAnti-JoinsAnti-JoinsManual Query Rewrite (X2QBOpt) covered in Manual Query Rewrite (X2QBOpt) covered in
ExtremeExtreme
Learn Traditional Tuning TechniquesLearn Traditional Tuning TechniquesOPTIMIZE FOR n ROWSOPTIMIZE FOR n ROWSNo OpsNo OpsREOPT(ONCE), REOPT(ALWAYS)REOPT(ONCE), REOPT(ALWAYS)Fake FilteringFake FilteringON 1 = 1ON 1 = 1Index & MQT DesignIndex & MQT Design
78© Sheryl M. Larsen, Inc. 2000-2011
OPTIMIZE FOR n ROWSFETCH FIRST n ROWS
Both clauses influence the OptimizerBoth clauses influence the Optimizer» To encourage index access and nested loop join To encourage index access and nested loop join
» To discourage list prefetch, sequential prefetch, and To discourage list prefetch, sequential prefetch, and access paths with Rid processingaccess paths with Rid processing
» Use FETCH n = total rows required for setUse FETCH n = total rows required for set
» Use OPTIMIZE n = number of rows to send across Use OPTIMIZE n = number of rows to send across network for distributed applicationsnetwork for distributed applications
» Works at the statement levelWorks at the statement level
79© Sheryl M. Larsen, Inc. 2000-2011
Fetch First Example Optimizer choose List Prefetch Index Optimizer choose List Prefetch Index
Access + sort for ORDER BY for 50,000 Access + sort for ORDER BY for 50,000 rowsrows
All qualifying rows processed All qualifying rows processed (materialized) before first row returned = (materialized) before first row returned = .81 sec.81 sec
<.1sec response time required<.1sec response time required
SELECT S.QTY_SOLD, S.ITEM_NO, S.ITEM_NAME
FROM SALE SWHERE S.ITEM_NO > :hvORDER BY ITEM_NO
SELECT S.QTY_SOLD, S.ITEM_NO, S.ITEM_NAME
FROM SALE SWHERE S.ITEM_NO > :hvORDER BY ITEM_NOFETCH FIRST 22 ROWS ONLY
• Optimizer now chooses Matching Index Access (first probe .004 sec)
• No materialization• Cursor closed after 22 items displayed (22 * .0008
repetitive access)• .004 + .017 = .021 sec
80© Sheryl M. Larsen, Inc. 2000-2011
No Operation (No Op) +0, CONCAT ‘ ‘ also –0, *1, /1+0, CONCAT ‘ ‘ also –0, *1, /1
» Place no op next to predicatePlace no op next to predicate
» Use as many as neededUse as many as needed
» Discourages index access, however, preserves Discourages index access, however, preserves Stage 1Stage 1
» Can Alter table join sequenceCan Alter table join sequence
» Can fine tune a given access pathCan fine tune a given access path
» Can request a table scanCan request a table scan
» Works at the predicate levelWorks at the predicate levelDoes not Benefit
DB2 on Linux, UNIX or Windows
81© Sheryl M. Larsen, Inc. 2000-2011
No Op Example CONCAT ‘ ‘
• Optimizer chooses Multiple Index Access• The table contains 100,000 rows and there are only 6 regions• Region range qualifies 2/3 of table• <.1sec response time required• No Op allows Multiple Index Access to continue on first 2 indexes• Two Matching index accesses, two small Rid sorts, & Rid intersection• ____unique Rids/16 * .000375 sec
= _____ + sort for Region = ______
SELECT S.QTY_SOLD, S.ITEM_NO, S.ITEM_NAME
FROM SALE SWHERE S.SALES_ID > 44 AND S.MNGR = :hv-mngr AND S.REGION BETWEEN
:hvlo AND :hvhiORDER BY S.REGION
SALES_ID.MNGR.REGION IndexSALES_ID.MNGR.REGION Index MNGR IndexMNGR Index REGION IndexREGION Index
…….FROM SALE SWHERE S.SALES_ID > 44 AND S.MNGR = :hv-mngr AND S.REGION BETWEEN
:hvlo AND :hvhi CONCAT ‘ ‘ORDER BY R.REGION
82© Sheryl M. Larsen, Inc. 2000-2011
No Op Example - Scan
• If you know the predicates do very little filtering, force a table scan
• Use a No Op on every predicate• This forces a table scan• FOR FETCH ONLY encourages parallelism• WITH UR for read only tables to reduce CPU
SELECT S.QTY_SOLD, S.ITEM_NO, S.ITEM_NAME
FROM SALE SWHERE S.SALES_ID > 44 +0 AND S.MNGR = :hv-mngr CONCAT ‘ ‘ AND S.REGION BETWEEN
:hvlo AND :hvhi CONCAT ‘ ‘ORDER BY S.REGIONFOR FETCH ONLYWITH UR
SALES_ID.MNGR.REGION IndexSALES_ID.MNGR.REGION Index MNGR IndexMNGR Index REGION IndexREGION Index
Should this be Documented?
83
REOPT Package/Plan level optionPackage/Plan level option
QUERYOPT-- optimization-level QUERYOPT-- optimization-level • REOPT ONCEREOPT ONCE• REOPT ALWAYS REOPT ALWAYS • REOPT AUTO – DB2 9REOPT AUTO – DB2 9
– Every query’s access path gets re-optimized with host variable contents Every query’s access path gets re-optimized with host variable contents known only when neededknown only when needed
– System Parameter REOPTEXT System Parameter REOPTEXT enables REOPT(AUTO) for allenables REOPT(AUTO) for allunspecified dynamic queries unspecified dynamic queries with parameter markerswith parameter markers
» Long running queries/programs easily benefitLong running queries/programs easily benefit» High volume transactions will reflect the cost of the re-bind (90% of a full High volume transactions will reflect the cost of the re-bind (90% of a full
BIND)BIND)
DynamicStatement
Cache SELECT FROM T1 WHERE C1 > ? , 120
Compares New Values For BIG differences
84© Sheryl M. Larsen, Inc. 2000-2011
Fake Filtering Fake PredicatesFake Predicates
» To encourage index accessTo encourage index access
» To alter table join sequence when nothing else To alter table join sequence when nothing else worksworks
» Works by decreasing filter factor on a certain tableWorks by decreasing filter factor on a certain table
» The filtering is fake and negligible costThe filtering is fake and negligible cost
» Not effective for dynamic queries if the filter contains Not effective for dynamic queries if the filter contains :host variables:host variables
85© Sheryl M. Larsen, Inc. 2000-2011
Fake Filtering Example
SELECT B.BID, D.DID, S.SID,,D.DESC,, S.DESC
FROM BONDS B, DENOM D, SERIAL S
WHERE B.BID BETWEEN :hvlo AND :hvhi
AND B.DID = D.DID AND B.SID = S.SIDORDER BY B.BID
B.BID Index6 billion rows
B.BID Index6 billion rows
D.DID Index12 rows
D.DID Index12 rows
S.SID Index20 rows
S.SID Index20 rows
•Large report query with average of 400,000 row range of BID table•Need to start nested loop with big table•Tools required
Nested Loop
Joins
86© Sheryl M. Larsen, Inc. 2000-2011
Fake Filtering Example
SELECT B.BID, D.DID, S.SID,,D.DESC,, S.DESC
FROM BONDS B, DENOM D, SERIAL S
WHERE B.BID BETWEEN :hvlo AND :hvhi
AND B.BID = D.DID AND B.SID = S.SID
ORDER BY B.BID
B.BID Index6 billion rows
B.BID Index6 billion rows
D.DID Index12 rows
D.DID Index12 rows
S.SID Index20 rows
S.SID Index20 rows
•Keep adding filters until table join sequence changes•Start with index columns
•To preserve index-only access•No limit!
Nested Loop
Joins
AND B.COL6 >= :hv
AND B.COL2 >= :hv
AND B.COL3 >= :hv
AND B.COL4 >= :hv
AND B.COL5 >= :hv
= B.COL2
= B.COL3
= B.COL4
= B.COL5
= B.COL6
For Dynamic
87© Sheryl M. Larsen, Inc. 2000-2011
ON 1 = 1 ON 1=1ON 1=1
» To fill in a required join fieldTo fill in a required join field
» To request a star join To request a star join
» When table ratios are under When table ratios are under the system specified number the system specified number (starts at 1:25)(starts at 1:25)
» Can benefit when large table Can benefit when large table has high selectivityhas high selectivity
88
Experiment with Extreme Techniques
After Traditional Techniques FailAfter Traditional Techniques Fail
89© Sheryl M. Larsen, Inc. 2000-2011
DISTINCT Table Expressions Table expressions with DISTINCTTable expressions with DISTINCT
• FROM (SELECT DISTINCT COL1 FROM T1 …..) AS FROM (SELECT DISTINCT COL1 FROM T1 …..) AS STEP1STEP1 JOIN T2 ON … JOIN T3 ON …. JOIN T2 ON … JOIN T3 ON ….
» Used for forcing creation of logical set of dataUsed for forcing creation of logical set of data• No physical materialization if an index satisfies DISTINCTNo physical materialization if an index satisfies DISTINCT
» Can encourage sequential detectionCan encourage sequential detection
» Can encourage a Merge Scan joinCan encourage a Merge Scan join
Buffer Pool
Data
Index
WorkFile
STEP1 PhysicalSTEP1 Physical
STEP1 LogicalSTEP1 Logical
90© Sheryl M. Larsen, Inc. 2000-2011
DISTINCT Table Expressions Example
SELECT ColumnsSELECT ColumnsFROM FROM TABXTABX, , TABYTABY,,
((SELECT DISTINCT COL1, COL2 …..SELECT DISTINCT COL1, COL2 ….. FROM FROM BIG_TABLE ZBIG_TABLE Z WHERE local conditions WHERE local conditions)) AS BIGZ AS BIGZ
WHERE join conditionsWHERE join conditions
Optimizer is forced to analyze the table expression prior to Optimizer is forced to analyze the table expression prior to joining TABX & TABYjoining TABX & TABY
91© Sheryl M. Larsen, Inc. 2000-2011
Cardinality 1
Typical Join ProblemSELECT COL1, COL2 …..SELECT COL1, COL2 …..
FROM FROM ADDRADDR, , NAMENAME, , TAB3, TAB4, TAB5, TAB3, TAB4, TAB5, TAB6,TAB6, TAB7 TAB7 WHERE join conditionsWHERE join conditions
AND AND TAB6.CODE = :hvTAB6.CODE = :hv
Result is only 1,000 rowsResult is only 1,000 rows ADDR and NAME first two tables in joinADDR and NAME first two tables in join Index scan on Index scan on TAB6TAB6 table table
» Not good because zero filterNot good because zero filter
92© Sheryl M. Larsen, Inc. 2000-2011
Keeps large tables joined last
Gets rid of Index Scan
Tuning TechniqueSELECT COL1, COL2 …..SELECT COL1, COL2 …..
FROM FROM ADDRADDR, , NAMENAME, ,
(SELECT DISTINCT columns(SELECT DISTINCT columns
FROM FROM TAB3, TAB4, TAB5, TAB3, TAB4, TAB5, TAB6,TAB6, TAB7 TAB7
WHERE join conditionsWHERE join conditions
AND (AND (TAB6.CODE = :hv OR 0 = 1)) TAB6.CODE = :hv OR 0 = 1)) AS TEMPAS TEMP
WHERE join conditionsWHERE join conditions
93© Sheryl M. Larsen, Inc. 2000-2011
Correlated Table Expressions Advantage over correlated subquery Advantage over correlated subquery
» Access both sets of dataAccess both sets of data Performance gainsPerformance gains
» Access path changes Access path changes » Encourages Nested Loop JoinEncourages Nested Loop Join» Reduced processing and/or materializationReduced processing and/or materialization
SELECT Columns from either set of dataFROM table1 T1 ,TABLE(SELECT T2.STUDENT_YEAR, SUM(C8) AS SUM8, MAX(C9) AS MAX9
FROM table2 T2WHERE T1.column = T2.columnGROUP BY T2.STUDENT_YEAR) AS T2
94© Sheryl M. Larsen, Inc. 2000-2011
Correlated Table Expressions
Get a student’s class load, and average & Get a student’s class load, and average & max class load of their peersmax class load of their peers
SELECT SR.NAME, SS.CLASS_LOAD, P.AVG_CLASS_LOAD, P.MAX_CLASS_LOADFROM SMLU_STUDENT_ROSTER SR
, TABLE(SELECT SR2.STUDENT_YEAR,AVG(SR2.NUM_OF_CLASSES) AS AVG_CLASS_LOAD,MAX(SR2.NUM_OF_CLASSES) AS MAX_CLASS_LOADFROM SMLU_STUDENT_ROSTER SR2WHERE SR.STUDENT_YEAR = SR2.STUDENT_YEARGROUP BY SR2.STUDENT_YEAR) AS P
LEFT JOIN TABLE(SELECT SS.SID, COUNT(*) AS SS.CLASS_LOADFROM SMLU_STUDENT_SCHED SSWHERE SS.SID = SR.SIDGROUP BY SS.SID) as SS
ON 1=1
95© Sheryl M. Larsen, Inc. 2000-2011
Anti-Join
Inner Join
TABX Exceptions
TABY Exceptions
SELECT ColumnsSELECT Columns
FROM FROM TABX XTABX X
WHERE NOT EXISTSWHERE NOT EXISTS
(SELECT *(SELECT *
FROM FROM TABY YTABY Y
WHERE WHERE XX.COL1 = .COL1 = YY.COL1).COL1)
SELECT ColumnsSELECT Columns
FROM FROM TABY YTABY Y
LEFT JOIN LEFT JOIN TABX XTABX X
ON ON XX.COL1 = .COL1 = YY.COL1.COL1
WHERE WHERE XX.COL1 IS NULL .COL1 IS NULL
SELECT ColumnsSELECT ColumnsFROM FROM TABX XTABX X, , TABY YTABY YWHERE WHERE XX.COL1 = .COL1 = YY.COL1.COL1
96© Sheryl M. Larsen, Inc. 2000-2011
Stage 2 when correlated
Anti-JoinSELECT ColumnsSELECT Columns
FROM FROM TABX XTABX X
WHERE NOT EXISTSWHERE NOT EXISTS
(SELECT *(SELECT *
FROM FROM TABY YTABY Y
WHERE WHERE XX.COL1 = .COL1 = YY.COL1).COL1)
Indexable Stage 1
SELECT ColumnsSELECT Columns
FROM FROM TABX XTABX X
LEFT JOIN LEFT JOIN TABY YTABY Y
ON ON XX.COL1 = .COL1 = YY.COL1.COL1
WHERE WHERE YY.COL1 IS NULL .COL1 IS NULL
TABX Exceptions
Does not Benefit LUW
97
Step 5
Tune if NecessaryTune if Necessary
98© Sheryl M. Larsen, Inc. 2000-2011
Hunting Query IdeasWHERE WHERE PROGNAME LIKE ‘online%’PROGNAME LIKE ‘online%’
ANDAND ACCESSTYPE = ‘I’ ACCESSTYPE = ‘I’ ANDAND PREFETCH = ‘L’PREFETCH = ‘L’
WHERE WHERE ACCESSTYPE = ‘I’ ACCESSTYPE = ‘I’ ANDAND MATCH_COLS = 0MATCH_COLS = 0
WHEREWHERE METHOD = 4METHOD = 4ANDAND SORTN_JOIN = ‘Y’SORTN_JOIN = ‘Y’
WHEREWHERE ACCESSTYPE = ‘R’ACCESSTYPE = ‘R’
WHERE WHERE TNAME LIKE ‘DSNWF%’TNAME LIKE ‘DSNWF%’WHERE WHERE QBLOCK_TYPE = ‘TABLEX’QBLOCK_TYPE = ‘TABLEX’
ANDAND TABLE_TYPE = ‘W’TABLE_TYPE = ‘W’
List OnlinesDoing
List Prefetch
List IndexScans
List NastyHybrid Joins
List Table Scans
Materialization
Table Expression Materialization(V7)
99© Sheryl M. Larsen, Inc. 2000-2011
SYSPACKSTMTWHERE WHERE STMT LIKE ‘%WHERE_:%BETWEEN%’STMT LIKE ‘%WHERE_:%BETWEEN%’
WHERE WHERE STMT LIKE ‘%WHERE%NOT%’STMT LIKE ‘%WHERE%NOT%’
WHEREWHERE STMT LIKE ‘%WHERE%COLX =%’STMT LIKE ‘%WHERE%COLX =%’
WHEREWHERE STMT LIKE ‘STMT LIKE ‘any SQL code problem any SQL code problem previously determinedpreviously determined’’
List UglyBETWEEN
clauses
List NOTs
Check dataTypes of literals
and constants
100
Step 6
Designing Optimal Designing Optimal Performance Structures for Performance Structures for One Query or a WorkloadOne Query or a Workload
101© Sheryl M. Larsen, Inc. 2000-2011
One Query at a Time
Type 2 Index
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Leaf Pageo o o o o o
Non-Leaf PageO O O O
Root PageO O O
SELECT C7, C8, C12, C2, C3WHERE C4 = ‘L’ AND C5 = 99 AND C6 = :hv AND C1 IN (:a,:b,:c) AND C2 NOT LIKE :hv AND C3 BETWEEN
:hvstart AND :hvend AND C9 = (CASE expression)ORDER BY C1.C2.C3
SELECT C7, C8, C12, C2, C3WHERE C4 = ‘L’ AND C5 = 99 AND C6 = :hv AND C1 IN (:a,:b,:c) AND C2 NOT LIKE :hv AND C3 BETWEEN
:hvstart AND :hvend AND C9 = (CASE expression)ORDER BY C1.C2.C3
C4.C5.C6.C1.C2.C3.C9.C7.C8.C12
1.1. Equal predicates firstEqual predicates first2.2. Sequencing columns nextSequencing columns next3.3. Predicates most restrictive to Predicates most restrictive to
least restrictiveleast restrictive
4.4. All remaining SELECT ColumnsAll remaining SELECT Columns
102© Sheryl M. Larsen, Inc. 2000-2008
…. To Build or not to BuildGeneric MQT strategiesGeneric MQT strategies» HUGE MQT with many indexesHUGE MQT with many indexes
» Many small MQTs with few indexesMany small MQTs with few indexes
Optimal MQT strategyOptimal MQT strategy» Most popular queries as MQTsMost popular queries as MQTs
• Common tablesCommon tables• Common predicate setsCommon predicate sets• Common data translationsCommon data translations
» Indexes on the popular ones to allow access to subset of Indexes on the popular ones to allow access to subset of MQTMQT
103© Sheryl M. Larsen, Inc. 2000-2008
Expensive Recursive SyntaxWITH ORG_CHART(BOSS, DIRECT_REPORT, N) ASWITH ORG_CHART(BOSS, DIRECT_REPORT, N) AS
(SELECT (SELECT BOSS, DIRECT_REPORT, 1BOSS, DIRECT_REPORT, 1
FROM FROM ORG_TABLEORG_TABLE
WHEREWHERE BOSS= ‘SAM’BOSS= ‘SAM’
UNION ALLUNION ALL
SELECT SELECT T1.BOSS, T2.DIRECT_REPORT, N+1T1.BOSS, T2.DIRECT_REPORT, N+1
FROM FROM ORG_CHART T2, ORG_TABLE T1ORG_CHART T2, ORG_TABLE T1
WHEREWHERE T2.DIRECT_REPORT = T2.BOSST2.DIRECT_REPORT = T2.BOSS
ANDAND N < 10 0,000)N < 10 0,000)
SELECT BOSS, DIRECT_REPORT, NSELECT BOSS, DIRECT_REPORT, N
FROM FROM ORG_CHARTORG_CHARTGenerated
at Execution
SAM
104© Sheryl M. Larsen, Inc. 2000-2008
MQT
MQT INDEX:BOSS
CREATE TABLE ORG_LIST AS (CREATE TABLE ORG_LIST AS (WITH ORG_CHART(BOSS, DIRECT_REPORT, N) ASWITH ORG_CHART(BOSS, DIRECT_REPORT, N) AS(SELECT BOSS, DIRECT_REPORT, 1(SELECT BOSS, DIRECT_REPORT, 1FROM FROM ORG_TABLEORG_TABLEWHEREWHERE BOSS= ‘SAM’BOSS= ‘SAM’UNION ALLUNION ALLSELECT SELECT T1.BOSS, T2.DIRECT_REPORT, N+1T1.BOSS, T2.DIRECT_REPORT, N+1FROM FROM ORG_CHART T2, ORG_TABLE T1ORG_CHART T2, ORG_TABLE T1WHEREWHERE T2.DIRECT_REPORT = T2.BOSS )T2.DIRECT_REPORT = T2.BOSS )SELECT BOSS, DIRECT_REPORT, NSELECT BOSS, DIRECT_REPORT, NFROM FROM ORG_CHART)ORG_CHART)
DATA INITIALLY DEFERREDDATA INITIALLY DEFERREDREFRESH DEFERREDREFRESH DEFERREDMAINTAINED BY SYSTEMMAINTAINED BY SYSTEMENABLE QUERY OPTMIZATION;ENABLE QUERY OPTMIZATION;
105© Sheryl M. Larsen, Inc. 2000-2008
Query Speed Up
DIRECTBOSS _REPORT NJOHN ADAM 501BETH JIM 634BETH SUE 634BETH MARK 635BETH SAM 635
WITH ORG_CHART (BOSS, DIRECT_REPORT, N) AS(SELECT BOSS, DIRECT_REPORT, 1
FROM ORG_TABLEWHERE BOSS= ‘BETH’UNION ALLSELECT T1.BOSS, T2.DIRECT_REPORT, N+1FROM ORG_CHART T2, ORG_TABLE T1WHERE T2.DIRECT_REPORT = T2.BOSS AND N < 10 0,000)
SELECT BOSS, DIRECT_REPORT, NFROM ORG_CHART
Accessed at
Execution
SELECT * FROM ORG_LISTSELECT * FROM ORG_LISTWHERE WHERE BOSS = ‘BETH’ BOSS = ‘BETH’
ORG_LIST
Designing Indexes
For a WorkloadFor a Workload
106© Sheryl M. Larsen, Inc. 2000-2011
107© Sheryl M. Larsen, Inc. 2000-2011
Table/Column Usage TranTran c1c1 c2c2 c3c3 c4c4 c5c5 c6c6 c7c7 c8c8 c9c9 c10c10 c11c11tran1tran1
q1q1 OO11
GG11
=T4.C2=T4.C2
OO22
GG22
=:hv=:hv
OO33
GG33
>800>800
SS =T2.C1=T2.C1 SS SS SSBTW BTW 2and 2and 77
SUM SUM byby
C1.C2.C1.C2.C3C3
AVG byAVG by
C1.C2.C3C1.C2.C3
MAX MAX byby
C1.C2.C1.C2.C3C3
q2q2 ss ss ww =22=22 oo11=:hv=:hv oo22
q3q3 =T3.C1=T3.C1 ss ss ss ss ss oo11 ss =T3.C11=T3.C11 =T3.C1=T3.C1
tran2tran2
q1q1 OO11 OO22 OO33=:hv=:hv >:hv>:hv
q2q2 ss ss =:hv=:hv ss ss ss ss ss OO11 IN(list)IN(list) LIKE :LIKE :hvhv
O1 = Order By First PositionO1 = Order By First PositionG1 = Group By First PositionG1 = Group By First PositionS = SELECT listS = SELECT listW = Local or Join WHERE/ON or W = Local or Join WHERE/ON or Specify actual local/join filter Specify actual local/join filter Specify Group by Aggregates as: Specify Group by Aggregates as:
Aggr BY Column(s)Aggr BY Column(s)
108© Sheryl M. Larsen, Inc. 2000-2011
Final Ranking
trantran11
100100
42004200 q1q1 OO1 1
GG11
WW
OO22
GG22
ww
OO33
GG33
ww
SS ww SS SS SS
ww
1010 q2q2 ss ss ww ww oo11 ww oo22
99009900 q3q3 ww oo11 ss ww ww
Weighting Based on Importance to the
Business
Frequency * Weight
420042004200
420042004200
10 10 10 10 10 10 10
9900
420042004200
420042004200
4200 4200 4200
9900 9900 9900 9900
WeightWeight
tran2tran2 300
109© Sheryl M. Larsen, Inc. 2000-2011
Determining Weight Copy the frequency reportCopy the frequency report Sort by “business priorities”Sort by “business priorities”
» Use Work Load Manager Goals for guidanceUse Work Load Manager Goals for guidance» Use Business Unit Owner PrioritiesUse Business Unit Owner Priorities
Remove the top 30%Remove the top 30% Sort by FrequencySort by Frequency Pull of the top numberPull of the top number Multiply by 10Multiply by 10 Now you have the frequency number to beatNow you have the frequency number to beat
Let’s say 5,500 is top frequent
Then 55,000 is the goal
Pulls out most critical
110© Sheryl M. Larsen, Inc. 2000-2011
Grand Total By Table, by OperationBy Table, by Operation
TotalTotal
ActivityActivity
c1c1 c2c2 c3c3 c4c4 c5c5 c6c6 c7c7 c8c8
OO1 1 5598055980
GG1 1 47904790
OO2 2 22202220
OO6 6 7070
W 3920W 3920
OO2 2 5598055980
GG2 2 47904790
W 400W 400
OO3 3 5598055980
GG3 3 47904790
W 3W 3
S S 660660
OO1 1
8740 8740
S 8740 S 8740
w w 2209022090
OO2 2
3940 3940
S 480S 480
S 4000S 4000
W 7640 W 7640
S 8390S 8390
W 2020W 2020
Use this information to justify making Use this information to justify making changes to current index designchanges to current index design
111© Sheryl M. Larsen, Inc. 2000-2011
Summary Many Things Impact Query SpeedMany Things Impact Query Speed
• SQL exploitation and proper usSQL exploitation and proper us• Manual Query Rewrite when necessaryManual Query Rewrite when necessary• Optimal Index and MQT design Optimal Index and MQT design
It is Easy to Change the Optimizer’s MindIt is Easy to Change the Optimizer’s Mind The Trick … is to Know WHEN to use:The Trick … is to Know WHEN to use:
Extreme Tuning TechniquesExtreme Tuning TechniquesDISTINCT Table ExpressionsDISTINCT Table ExpressionsCorrelated Table ExpressionsCorrelated Table ExpressionsOR 0 = 1OR 0 = 1Anti-Join TechniqueAnti-Join TechniqueIndex and MQT DesignIndex and MQT DesignX2QBOptX2QBOpt
Traditional Tuning TechniquesTraditional Tuning TechniquesOPTIMIZE FOR n ROWSOPTIMIZE FOR n ROWSNo OpsNo OpsREOPT(VARS)REOPT(VARS)Fake FilteringFake FilteringIndex & MQT RedesignIndex & MQT Redesign
112© Sheryl M. Larsen, Inc. 2000-2011
Recommended SQL Reading
DB2 Developer’s Guide, Sixth Edition SAMS Publishing, Craig S. MullinsCovers DB2 9 & 10 for z/OS!!!!!
DB2 for z/OS: Design Guidelines for High Performance and Availability http://www.redbooks.ibm.com/abstracts/sg247134.html
DB2 for z/OS Version 8 Performance Topics DB2 for z/OS Version 8 Performance Topics Explore the book online at Explore the book online at http://www.redbooks.ibm.com/abstracts/sg246465.html
DB2 9 for z/OS Performance Topics, SG24-7473-00, SG24-7473-00Explore the book online at Explore the book online at http://www.redbooks.ibm.com/abstracts/sg247473.html?Open
DB2 10 for z/OS Performance Topics DB2 10 for z/OS Performance Topics http://www.redbooks.ibm.com/abstracts/sg247942.html