Demystifying The DB2 Dynamic Statement Cache
Demystifying The DB2 Dynamic Statement Cache
01/07/16
SQL
70%
Subsystem
10%
Object
Analysis
20%
play
More focus on measuring the workload
and rapidly reacting
Access Path
To the Data
Hardware
Configuration
TABLE
Schema Definitions
Catalog
Statistics
Tablespace
DB04.TS1
Index
App1.Index1
PREPARE(DEFER)
DB2
Configuration
SQL Statement
Text
Access Path
To the Data
Hardware
Configuration
TABLE
Schema Definitions
Catalog
Statistics
Tablespace Index
App1.Index1
DB04.TS1
To access DB2 data, an SQL statement requires an access path. Two big factors in the performance of an SQL statement are the amount of time that DB2 uses to determine the access path at run time and whether the access path is
efficient. DB2 determines the access path for a statement at either of these times: When you bind the plan or package that contains the SQL statement
If you specify REOPT(NONE), DB2 determines the access path at bind time, just as it does when there are no input variables.
DB2 ignores REOPT(ONCE) for static SQL statements because DB2 can cache only dynamic SQL statements
If you specify REOPT(ALWAYS), DB2 determines the access path at bind time and again at run time, using the values in these types of input variables:
Host variables
Parameter markers
Special registers
This means that DB2 must spend extra time determining the access path for statements at run time, but if DB2 determines a significantly better access path using the variable values, you might see an overall performance improvement. In
general, using REOPT(ALWAYS) can make static SQL statements with input variables perform like dynamic SQL statements with constants.
Dynamic SQL statements with input host variables: When you bind applications that contain dynamic SQL statements with input host variables, use the REOPT(ALWAYS) option, the REOPT(ONCE) option, or the REOPT(AUTO) option.
Use REOPT(ALWAYS) when you are not using the dynamic statement cache. DB2 determines the access path for statements at each EXECUTE or OPEN of the statement. This ensure the best access path for a statement, but using
REOPT(ALWAYS) can increase the cost of frequently used dynamic SQL statements.
Use REOPT(ONCE) or REOPT(AUTO) when you are using the dynamic statements cache: If you specify REOPT(ONCE), DB2 determines and the access path for statements only at the first EXECUTE or OPEN of the statement. It saves that
access path in the dynamic statement cache and uses it until the statement is invalidated or removed from the cache. This reuse of the access path reduces the cost of frequently used dynamic SQL statements that contain input host
variables; however, it does not account for changes to parameter marker values for dynamic statements.
If you specify REOPT(AUTO), DB2 determines the access path at run time. For each execution of a statement with parameter markers, DB2 generates a new access path if it determines that a new access path will improve performance.
You should code your PREPARE statements to minimize overhead. With REOPT(AUTO), REOPT(ALWAYS), and REOPT(ONCE), DB2 prepares an SQL statement at the same time as it processes OPEN or EXECUTE for the statement. That
is, DB2 processes the statement as if you specify DEFER(PREPARE). However, in the following cases, DB2 prepares the statement twice: If you execute the DESCRIBE statement before the PREPARE statement in your program
If you use the PREPARE statement with the INTO parameter
For the first prepare, DB2 determines the access path without using input variable values. For the second prepare, DB2 uses the input variable values to determine the access path. This extra prepare can decrease performance.
If you specify REOPT(ALWAYS), DB2 prepares the statement twice each time it is run.
If you specify REOPT(ONCE), DB2 prepares the statement twice only when the statement has never been saved in the cache. If the statement has been prepared and saved in the cache, DB2 will use the saved version of the statement to
complete the DESCRIBE statement.
If you specify REOPT(AUTO), DB2 initially prepares the statement without using input variable values. If the statement has been saved in the cache, for the subsequent OPEN or EXECUTE, DB2 determines if a new access path is needed
according to the input variable values.
For a statement that uses a cursor, you can avoid the double prepare by placing the DESCRIBE statement after the OPEN statement in your program.
If you use predictive governing, and a dynamic SQL statement that is bound with either REOPT(ALWAYS) or REOPT(ONCE) exceeds a predictive governing warning threshold, your application does not receive a warning SQLCODE.
However, it will receive an error SQLCODE from the OPEN or EXECUTE statement.
SELECT Operations
Parameter marker
provides placeholder
for later substitution
Other operations
Cause the INSERT statement to be prepared and executed
immediately
application
Built on the fly based on search
criteria selected
A complex statement with
unpredictable input
Default statement syntax includes minimal
number of search criteria
More search criteria the statement
expands to include those search
arguments
If using static SQL could require over 100
cursor definitions in the program
Dynamic SQL always uses current catalog statistics for access path selection
Changes in DB2 statistics can cause unpredictable changes in access paths
Access path analysis difficult because access path is not available prior to
execution
statements
Implementation
Four kinds of caching
No caching
Local Dynamic Statement Caching
Global Dynamic Statement Caching
Full Caching
Cache prepared SQL statement and statement text for dynamic SQL statements in
DBM1address space
Local Statement Cache
Global Dynamic Statement Cache
Full
Prepa
re
Used
Thread Storage
Prepared Statement
STMT2(Version 1)
d
arde
Disc
Full
Prepa
re
Used
Prepared Statement
STMT2(Version 2)
Program RRS01
Full
Prepa
r
Used
a
sc
Di
rd
ed
Implicit
Prepare
Thread Storage
Prepared Statement
STMT2(Version 1)
Statement Text
Retained
Prepared Statement
STMT2(Version 2)
Short Prepare
Program RRS01
Full
Prepa
re
RRS01 Local
Thread Storage
Prepared Statement
STMT2(Version 1)
SKDS
SKDS
Program RRS01
SKDS
Shor
t
Prep
a
re
Prepared Statement
STMT2(Version 1)
RRS01 Local
Thread Storage
Prepare Avoidance
RRS01 Local
Thread Storage
Prepared Statement
STMT2(Version 1)
e
par
Pre
d
ide
Avo
Short Prepare
Dynamic statement (SKDS) in the
global cache
Global caching active
Avoided Prepare
Local and global caching active
DBM1
DBD
DBD
SKCT
SKCT
PT
CT
CT
SKDS
SKPT
SKDS
SKDS
CT
CT
CT
SKPT
structure definitions
This pool contains
DBDs database descriptors
Skeleton Package and Cursor Tables
(SKPT & SKCT)
Package and Cursor Tables (PT/CT)
Authorization cache block for each
plan (optional)
SKDS - Skeletons of dynamic SQL for
CACHE DYNAMIC SQL (optional)
Optionally stored in a dataspace
Trigger Packages
DBDs
EDMDBDC DBDs
SKDS
SKDS
SKCT
SKPT
PT
CT
EDMPOOL
DBDs
SKCT
SKPT
CT
PT
EDMPOOL
SKDS
CT
SKDS
PT
2GB Bar
Statement Text
SQLDA DESCRIBE output
Portion of native SQL PL package
Statement Discarded
Shoot for 0
Increase MAXKEEPD
DROP or ALTER
Authorization Revoked
LRU
RUNSTATS
DB2 is recycled
IFCID 317 can then be used to retrieve the entire SQL statement from the cache once
you have identified the statement of interest
EXPLAIN STMTCACHE
V8 feature that exports Dynamic Statement Cache information to the
DSN_STATEMENT_CACHE_TABLE
Nearly identical to the detail in IFCID 316 & 317
Multiple options including ALL, stmt-id, and stmt-token
Bind Options
Statement Statistics (more
later)
....
....
DSN_STATEMENT_CACHE_TABLE
....
PLAN TABLE
....
DSN_STATEMENT_CACHE_TABLE
DSN_STATEMENT_TABLE
....
DSN_STATEMENT_CACHE_TABLE
SQL statements
Turn on collection with Monitor
trace IFCID 318
Begins collecting statistics and
accumulates them for the length
of time the monitor trace is on
Stop Monitor trace resets all
statistics
2-4% overhead per dynamic SQL
statement stored in the cache
Recommended approach
Run the trace only when actively
monitoring the cache
Use EXPLAIN STMTCACHE to
externalize data for evaluation
Acknowledgements
DB2 for z/OS and OS/390 : Squeezing the Most Out of Dynamic SQL
DB2 UDB for z/OS V8: Through the Looking Glass and What SAP Found
There
Summary
Dynamic SQL is growing in usage
ERP Vendors
Distributed applications
DB2 9 will see big changes in the way the SQL statement execution
statistics discussed in this session will be used captured and used