DB2 For z/OS: Utilities and Application Development
DB2 For z/OS: Utilities and Application Development
DB2 For z/OS: Utilities and Application Development
TABLE OF CONTENTS
UTILITIES & APPLICATION DEVELOPMENT
6
7
8
9
10
13
15
16
17
19
22
24
26
29
33
iii
iv
Objectives :
to introduce the main difference between SQL processing and the execution of utilities
to give an overview of utility functions that can replace SQL processing
to show with simple and more complex examples how and when you can
take advantage from this functionality
Introduction
Utilities and application-related functionality
1.1
UNLOAD:
for READ actions
LOAD:
for WRITE actions
REORG:
for REMOVE actions
REORG PAUSE or UNLOAD - (RE)LOAD:
for REWRITE or COMPLEX actions
ABIS
1.2
SQL modifications:
a lot of overhead:
- logging
- locking
- on the fly:
index maintenance
referential integrity control (and/or actions: cascade/nullifies)
In addition, if large amount of modifications:
---> often, a REORGANISATION needed
For simple actions on 1 table:
---> UTILITIES are much cheaper
most actions directly on VSAM files
ABIS
Usability
1.3
In general:
- for simple actions on high volumes
stand-alone tables and activities
- if application activity is always (or often) combined with utility
processing
e.g. batch job followed by a REORG
- if asynchronous execution is allowed
e.g action is not a part of a larger transaction
ABIS
1.4
ABIS
Supported functionality
UNLOAD utility for READ activities
2.1
UNLOAD
TABLESPACE TBD7971.TBSSTMOD
FROM TABLE STOCKMODIFICATIONS
(SMID, SM_PRNO, SMTIMESTAMP, SMQUANTITY)
WHEN (SMTIMESTAMP BETWEEN 01.01.2004 AND 31.12.2004)
SHRLEVEL CHANGE ISOLATION UR
ABIS
10
Additional possibilities:
UNLOAD
TABLESPACE TBD7971.TBSSTMOD
FROMCOPYDDN DB2COPY1
FROM TABLE STOCKMODIFICATIONS
SAMPLE 5 LIMIT 2500
(SMID,
SMTIMESTAMP TIMESTAMP EXTERNAL,
SMCOMMENT VARCHAR STRIP TRAILING)
WHEN (SMTIMESTAMP BETWEEN 01.01.2004 AND 31.12.2004)
PUNCHDDN DB2PUNCH
DELIMITED COLDEL ,
SHRLEVEL CHANGE ISOLATION UR
ABIS
11
- FROMCOPYDDN
to use an IMAGE COPY as input
- PUNCHDDN
to create corresponding LOAD utility statements
- SAMPLE - LIMIT
to limit the number of output records
- TIMESTAMP EXTERNAL - VARCHAR STRIP TRAILING
to format output fields
- DELIMITED COLDEL ,
to format output records
ABIS
12
2.2
ABIS
13
Additional possibilities:
LOAD DATA
LOG NO NOCOPYPEND
ENFORCE NO
RESUME YES
SHRLEVEL CHANGE
INTO TABLE STOCKMODIFICATIONS
FORMAT UNLOAD or <FIELD-specifications>
ABIS
14
2.3
ABIS
15
2.4
ABIS
16
3.1
Do while
READ input-record
...
INSERT INTO StockModifications
VALUES(:WS-StockModifications:WS-SMInd)
End
Problem:
- large input-files and 4 indexes
--> long execution time
- locking:
from exclusive table lock and low commit frequency to page locks
and high commit frequency:
--> more concurrency
--> +/- doubled elapsed time
ABIS
17
Alternative
LOAD DATA
RESUME YES
SHRLEVEL NONE
INTO TABLE STOCKMODIFICATIONS
<FIELD-specifications>
- Plus:
less execution time (between 25 and 50%)
no programming effort
- Minus:
FIELD-specifications
SHRLEVEL NONE
(SHRLEVEL CHANGE = INSERT)
only KEY and R.I. control
no TRIGGER support
ABIS
18
3.2
Problem:
- time that the table is unavailable (exclusively locked) is too long
- lock is needed: concurrent updates can give a wrong result
ABIS
19
Alternative 1:
UNLOAD DATA
FROM TABLE STOCKMODIFICATIONS
WHEN (SMTIMESTAMP = CURRENT DATE - 1 DAY)
SHRLEVEL REFERENCE
- Plus:
faster than SQL (typical value: 3 times faster)
no programming effort
format of output records is controllable
- Minus:
no complex conditions
ABIS
20
Alternative 2:
COPY
TABLESPACE TBD7971.TBSSTMOD
SHRLEVEL REFERENCE
UNLOAD
TABLESPACE TBD7971.TBSSTMOD
FROMCOPYDDN ...
FROM TABLE STOCKMODIFICATIONS
WHEN (SMTIMESTAMP = CURRENT DATE - 1 DAY)
- Plus:
COPY +/- 2 times faster than UNLOAD (of alternative 1)
- Minus:
second job (UNLOAD)
(but without impact on the base table)
ABIS
21
3.3
DELETE
FROM StockModifications
WHERE SMTimestamp < CURRENT DATE - 2 DAYS
AND SMStatus = 7
Problem:
- +/- 50% of the rows are deleted
- table has 4 indexes
--> very long execution time (60 minutes)
- space management
--> REORG needed after execution
- remarks:
locking: exclusive table lock
--> low CPU-usage, many time-outs
R.I. is no problem (dependent table)
ABIS
22
Alternative
REORG TABLESPACE TBD7971.TBSSTMOD
LOG NO SORTKEYS COPYDDN(SYSCOPY)
STATISTICS TABLE ALL INDEX ALL
SHRLEVEL REFERENCE
DISCARD FROM TABLE STOCKMODIFICATIONS
WHEN (SMTIMESTAMP = CURRENT DATE - 2 DAYS
AND SMSTATUS = 7)
- Plus:
much faster than SQL (few minutes)
no programming effort
REORG and DISCARD: one job
- Minus:
no complex conditions
if parent table: CHKP for dependent tables
ABIS
23
4.1
Process:
- Problem:
index maintenance on LastStockModifications (dependent table)
(index maintenance on LastOrders)
ABIS
24
Alternative
UPDATE LastStockModifications
SET LSMStatus = OK
WHERE EXISTS (SELECT 1 FROM LastOrders
WHERE LOStatus = OK
AND LO_PrNo = LSM_PrNo)
ABIS
25
Use of INCURSOR
4.2
Process:
- information must be copied from a source table to a target table
- Problem:
index maintenance on StockModifications
ABIS
26
Alternative 1:
EXEC SQL
DECLARE GETSTMOD CURSOR FOR
SELECT * FROM LASTSTOCKMODIFICATIONS T
WHERE NOT EXISTS (SELECT 1 FROM PRODUCTS
WHERE PRNO = T.LSM_PRNO
AND PRSTATUS = 7)
ORDER BY LSMTIMESTAMP
ENDEXEC
LOAD DATA
INCURSOR GETSTMOD
RESUME YES
SHRLEVEL NONE
INTO TABLE STOCKMODIFICATIONS
ABIS
27
- Alternative 2:
INSERT INTO TempStockModifications
SELECT * FROM LastStockModifications T
WHERE NOT EXISTS (SELECT 1 FROM Products
WHERE PrNo = T.LSM_PrNo
AND PRStatus = 7)
ABIS
28
4.3
Process:
- uncontrolled information (but correct format) must be copied
from an input-file into a target table
- availability of the target table is critical
- complex control criteria for the input information
SQL solution
Do while
READ input-record
<control activities>
If OK
INSERT INTO StockModifications
VALUES(:WS-StockModifications:WS-SMInd)
End
- Problem:
again index maintenance on StockModifications
ABIS
29
Alternative 1:
LOAD DATA
RESUME YES
SHRLEVEL NONE
INTO TABLE STOCKMODIFICATIONS
<FIELD>-specifications
ABIS
30
Alternative 2:
LOAD DATA
RESUME YES
SHRLEVEL NONE
INTO TABLE NEWSTOCKMODIFICATIONS
<FIELD>-specifications
ABIS
31
- Alternative 2 (cont.):
EXEC SQL
DECLARE GETSTMOD CURSOR FOR
SELECT * FROM NEWSTOCKMODIFICATIONS
WHERE NSMSTATUS IS NOT NULL
ORDER BY NSMTIMESTAMP
ENDEXEC
LOAD DATA
INCURSOR GETSTMOD
RESUME YES
SHRLEVEL NONE
INTO TABLE STOCKMODIFICATIONS
ABIS
32
4.4
Process:
- refresh of a replicated table inside DB2
- at runtime initiated by a remote Java application
Standard solution using SQL:
DELETE FROM LastStockModifications
INSERT INTO LastStockModifications
SELECT *
FROM STOCKMODIFICATIONS
WHERE SMTIMESTAMP = CURRENT DATE
Problem:
- in flight index maintenance
- how to use utilities as alternative?
ABIS
33
ABIS
34
Calling DSNUTILS:
CALL DSNUTILS
(<utility-id>,
<restart-indicator>,
<utility-statements>,
<return-code>,
<ANY / <utility-name>,
<dataset-name1>,
.....)
Utility statements
UNLOAD DATA
FROM TABLE STOCKMODIFICATIONS
WHEN (SMTIMESTAMP = CURRENT DATE)
LOAD DATA REPLACE
INTO TABLE LASTSTOCKMODIFICATIONS
ABIS
35
ABIS
36