New Cool SQL Enhancements in DB2 for z/OS Version 8
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

New Cool SQL Enhancements in DB2 for z/OS Version 8

on

  • 1,354 views

 

Statistics

Views

Total Views
1,354
Views on SlideShare
1,354
Embed Views
0

Actions

Likes
0
Downloads
24
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

New Cool SQL Enhancements in DB2 for z/OS Version 8 Presentation Transcript

  • 1. Advanced Technical Support (ATS), Americas New Cool SQL Enhancements in DB2 for z/OS Version 8 CDUG – July 20, 2006 John Iczkovits iczkovit@us.ibm.com © 2004 IBM Corporation
  • 2. Advanced Technical Support (ATS), Americas What's new in V8? Scalar fullselect Multiple DISTINCT clauses Dynamic scrollable cursors Multi-row FETCH and INSERT GET DIAGNOSTICS statement INSERT within SELECT statement Sequence objects Identity column enhancements CURRENT PACKAGE PATH special register Common table expressions and recursive SQL DB2 And maybe more.... DB2 DB2 © 2004 IBM Corporation
  • 3. Advanced Technical Support (ATS), Americas Scalar Fullselect © 2004 IBM Corporation
  • 4. Advanced Technical Support (ATS), Americas Scalar fullselect What is it? ..... –A scalar fullselect is a fullselect, enclosed in parentheses, that returns a single value –Scalar fullselect can be used in an expression –Example: SELECT PRODUCT, PRICE FROM PRODUCTS WHERE PRICE <= 0.7 * (SELECT AVG(PRICE) FROM PRODUCTS); Benefits ..... –Enhances usability and power of SQL –Facilitates portability –Conforms with SQL standards © 2004 IBM Corporation
  • 5. Advanced Technical Support (ATS), Americas Scalar fullselect -- tables used in examples PARTS PARTPRICE PART PROD# SUPPLIER PART _ PROD# SUPPLIER PRICE WIRE 10 ACWF WIRE 10 ACWF 3.50 OIL 160 WESTERN_CHEM OIL 160 WESTERN_CHEM 1.50 MAGNETS 10 BATEMAN MAGNETS 10 BATEMAN 59.50 PLASTIC 30 PLASTIC_CORP PLASTIC 30 PLASTIC_CORP 2.00 BLADES 205 ACE_STEEL BLADES 205 ACE_STEEL 8.90 PRODUCTS INVENTORY PROD# PRODUCT PRICE PART PROD# SUPPLIER ONHAND# 505 SCREWDRIVER 3.70 WIRE 10 ACWF 8 30 RELAY 7.55 OIL 160 WESTERN_CHEM 25 205 SAW 18.90 MAGNETS 10 BATEMAN 3 10 GENERATOR 45.75 PLASTIC 30 PLASTIC_CORP 5 BLADES 205 ACE_STEEL 10 © 2004 IBM Corporation
  • 6. Advanced Technical Support (ATS), Americas Scalar fullselects in a WHERE clause Find which products have the prices of at least twice the lowest price of all the products. SELECT PRODUCT, PRICE FROM PRODUCTS A WHERE PRICE >= 2 * (SELECT MIN(PRICE) FROM PRODUCTS); PRODUCT PRICE RELAY 7.55 SAW 18.90 GENERATOR 45.75 © 2004 IBM Corporation
  • 7. Advanced Technical Support (ATS), Americas Nested scalar fullselects in a SELECT list Find the cost of inventory for each product by calculating the sum of (price * onhand#) for each part in the product. SELECT PRODUCT, (SELECT COALESCE(SUM(X.COST),0) AS INV_COST FROM (SELECT ( (SELECT PRICE FROM PARTPRICE P WHERE P.PART = B.PART) *(SELECT ONHAND# FROM INVENTORY I WHERE I.PART =B.PART) ) AS COST FROM PARTS B WHERE B.PROD# = A.PROD# PRODUCT INV_COST ) X(COST) SCREWDRIVER .00 ) RELAY 10.00 FROM PRODUCTS A; SAW 89.00 GENERATOR 206.50 © 2004 IBM Corporation
  • 8. Advanced Technical Support (ATS), Americas Scalar fullselects -- in CASE expression of UPDATE Give discount to the parts that have the large inventory and raise price on the parts that have the small inventory. CREATE TABLE NEW_PARTPRICE LIKE PARTPRICE; INSERT INTO NEW_PARTPRICE SELECT * FROM PARTPRICE; UPDATE NEW_PARTPRICE N SET PRICE = CASE WHEN( (SELECT ONHAND# FROM INVENTORY WHERE PART=N.PART) < 7) THEN 1.1 * PRICE WHEN( (SELECT ONHAND# FROM INVENTORY WHERE PART=N.PART) > 20) THEN .8 * PRICE PART PROD# SUPPLIER PRICE ELSE PRICE WIRE 10 ACWF 3.50 END; OIL 160 WESTERN_CHEM 1.20 MAGNETS 10 BATEMAN 65.45 SELECT * FROM NEW_PARTPRICE; PLASTIC 30 PLASTIC_CORP 2.20 BLADES 205 ACE_STEEL 8.90 © 2004 IBM Corporation
  • 9. Advanced Technical Support (ATS), Americas Scalar fullselect -- restrictions Scalar fullselect not supported in... a CHECK constraint a grouping expression a view that has a WITH CHECK OPTION a CREATE FUNCTION (SQL scalar) a column function ORDER BY clause join-condition of the ON clause for INNER and OUTER JOINS © 2004 IBM Corporation
  • 10. Advanced Technical Support (ATS), Americas Multiple DISTINCT Clauses © 2004 IBM Corporation
  • 11. Advanced Technical Support (ATS), Americas Multiple DISTINCT clauses What is it? ..... –Allows more than one DISTINCT keyword on the SELECT or HAVING clause for a query –DB2 can now evaluate standard deviation & variance column functions Benefits ..... –Enhances usability and power of SQL –DB2 family compatibility © 2004 IBM Corporation
  • 12. Advanced Technical Support (ATS), Americas Multiple DISTINCT clauses Prior to Version 8 . . . –SELECT DISTINCT C1, C2 FROM T1; –SELECT COUNT(DISTINCT C1) FROM T1; –SELECT C1, COUNT(DISTINCT C2) FROM T1 GROUP BY C1; –SELECT COUNT(DISTINCT(C1)),SUM(DISTINCT C1) FROM T1; -- same col With Version 8 . . . –SELECT DISTINCT COUNT(DISTINCT C1), SUM(DISTINCT C2) FROM T1; –SELECT COUNT(DISTINCT C1), AVG(DISTINCT C2) FROM T1 GROUP BY C1; –SELECT SUM(DISTINCT C1), COUNT(DISTINCT C1), AVG(DISTINCT C2) FROM T1 GROUP BY C1 HAVING SUM(DISTINCT C1) = 1; Not supported in Version 8 . . . –SELECT COUNT(DISTINCT A1,A2) FROM T1 GROUP BY A2; –SELECT COUNT(DISTINCT(A1,A2)) FROM T1 GROUP BY A2; © 2004 IBM Corporation
  • 13. Advanced Technical Support (ATS), Americas Dynamic Scrollable Cursors © 2004 IBM Corporation
  • 14. Advanced Technical Support (ATS), Americas Dynamic scrollable cursors What is it? ..... –Dynamic scrollable cursor: Scrolls directly on base table (TS scan, IX scan & DPSI) No result table materialized at any time Immediate visibility of committed updates, inserts, deletes Positioned updates and deletes allowed when possible Avoid sort by using backward index scan for ORDER BY Benefits ..... –Enhances usability and power of SQL –Facilitates portability –Conforms to SQL standards –Performance improved by sort elimination –Elimination of work file (temporary table) © 2004 IBM Corporation
  • 15. Advanced Technical Support (ATS), Americas Static scrollable cursors -- Version 7 review Cursors can be scrolled –backwards –forwards –to an absolute position –to a position relative to the current cursor FETCH CURSOR... –before/after position Result table in TEMP DB Rows refreshed on demand © 2004 IBM Corporation
  • 16. Advanced Technical Support (ATS), Americas Sensitive and insensitive Static cursors - V7 review DECLARE C1 INSENSITIVE DECLARE C1 SENSITIVE DECLARE C1 SENSITIVE SCROLL.. STATIC SCROLL.. STATIC SCROLL.. OPEN C1 ... OPEN C1 ... OPEN C1 ... FETCH INSENSITIVE... FETCH INSENSITIVE... FETCH SENSITIVE... PROGRAM PROGRAM PROGRAM TEMP TABLE TEMP TABLE TEMP TABLE BASE TABLE BASE TABLE BASE TABLE ƒRead only cursor ƒUpdatable cursor ƒUpdatable cursor ƒNot aware of updates or ƒAware of own updates or ƒAware of own updates and deletes in base table deletes within cursor deletes within cursor ƒOther changes to base ƒsees all committed table not visible to cursor updates and deletes ƒAll inserts not recognized ƒAll inserts not recognized Order by, table join and aggregate functions will force READ ONLY © 2004 IBM Corporation
  • 17. Advanced Technical Support (ATS), Americas STATIC SCROLL Cursors Base Table Result Table DB2 Table DB2 Declared Temp Table SCROLL Base Table & Result Table are kept in sync as each Exclusive access by agent row is fetched if sensitive. Insensitive Fixed number of rows reads off the DTT – no need Goes away at Close Cursor to fetch updated or Requires declared TEMP database and Accessed by many deleted data. predefined table spaces © 2004 IBM Corporation
  • 18. Advanced Technical Support (ATS), Americas What does the TEMP ROW look like? SELECT C1, C2, C1+C2, LOB1 WHERE C3 < 5 Insensitive cursor temp table row 16 byte C1 C2 C1+C2 Lob1 Absolute 120 byte number Length Length Max lob of C1 of C2 Length(C1,C descriptor 2) Sensitive cursor temp table row 16 byte 6 byte 5 byte C1 C2 C1+C2 Lob1 Absolute control Rid 120 byte number information Length Length Max lob of C1 of C2 Length(C1,C descriptor 2) © 2004 IBM Corporation
  • 19. Advanced Technical Support (ATS), Americas Dynamic scrollable cursors -- V8 new function Scrollable cursor that provides access to the base table rather than a DTT (Declared Temporary Table) -- allows visibility of updates and inserts done by you or other users Defaults to single row FETCH, so DDF applications should use: –Multi-row FETCH –Positioned UPDATE/DELETE for multi-row FETCH DECLARE C1 SENSITIVE DYNAMIC SCROLL CURSOR FOR SELECT C1, C2 FROM T1; © 2004 IBM Corporation
  • 20. Advanced Technical Support (ATS), Americas New DECLARE CURSOR statement attributes SENSITIVE DYNAMIC –Specifies that size of result table is not fixed at OPEN cursor time –Cursor has complete visibility to changes All committed inserts, updates, deletes by other application processes All positioned updates and deletes within cursor All inserts, updates, deletes by same application processes, but outside cursor –FETCH executed against base table since no temporary result table created ASENSITIVE –DB2 determines sensitivity of cursor –If read-only... It behaves as an insensitive cursor Cursor is INSENSITIVE if SELECT statement does not allow it to be SENSITIVE (UNION, UNION ALL, FOR FETCH ONLY, FOR READ ONLY) –If not read only, SENSITIVE DYNAMIC is used for maximum sensitivity –Mainlyfor client applications that do not care whether or not the server supports the sensitivity or scrollability © 2004 IBM Corporation
  • 21. Implications on FETCH with Dynamic Scroll Advanced Technical Support (ATS), Americas INSENSITIVE not allowed with FETCH statement (SQLCODE -244) if –Theassociated cursor is declared as SENSITIVE DYNAMIC SCROLL –The cursor is declared ASENSITIVE and DB2 chooses the maximum allowable sensitivity of SENSITIVE DYNAMIC SCROLL There are no "holes" (almost) as there is no temporary result table –Specialcase: If FETCH CURRENT or FETCH RELATIVE +0 requested but row on which cursor is positioned was deleted or updated so that it no longer meets selection criteria (SQLCODE +231) which says "hole" detected –Forexample, can occur with ISOLATION(CS) and CURRENTDATA(NO) Inserts by the application itself are immediately visible -- inserts by others are visible after commit Order is always maintained –Ifcurrent row is updated, the cursor is positioned before the next row of the original location and there is no current row –Ifcurrent row deleted, the cursor is positioned before the next row of the original location and there is no current row © 2004 IBM Corporation
  • 22. Advanced Technical Support (ATS), Americas DECLARE CURSOR Syntax >>-DECLARE-cursor-name---+--------------------------------------------------------------+----> | | | |-ASENSITIVE-------------------| | +---|-INSENSITIVE-------------------|----SCROLL--+ |-SENSITIVE---STATIC--------| |-DYNAMIC-| >-----CURSOR--------+-----------------------+-----FOR---+-select-statement---+--------->< +-WITH HOLD-----+ +-statement-name---+ +-WITH RETURN-+ © 2004 IBM Corporation
  • 23. Advanced Technical Support (ATS), Americas • INSENSITIVE is used for static scrollable cursors only. Keep in mind that for static scrollable cursors you can have a combination of SENSATIVE and INSENSATIVE for the DECLARE and FETCH statements. Make sure you know which one is being discussed on which statement. • SENSITIVE can be used for either static or dynamic scrollable cursors. Which one are you dealing with? • Be careful when using ASENSITIVE, you may not get what you want. • Isolation levels and CURRENTDATA values matter, mostly because of the ‘holes’ issue. Make sure you are getting the data you want. © 2004 IBM Corporation
  • 24. Advanced Technical Support (ATS), Americas FETCH Orientation Syntax +---NEXT------------------------------------------+ >>-FETCH----+--------------------+--+-----------------------------------------------------+---------> +-INSENSITIVE-+ |---PRIOR------------------------------------------| +-SENSITIVE----+ |---FIRST-------------------------------------------| |---LAST--------------------------------------------| |---CURRENT-------------------------------------| |---BEFORE---------------------------------------| |---AFTER------------------------------------------| |---ABSOLUTE--+-host-variable-------+---| | +-integer-constant--+ | |---RELATIVE----+-host-variable-------+---| +-integer-constant--+ +-FROM-+ >---+------------+----cursor-name-------+--------------------------------+------------------>< +--single-fetch-clause---+ single-fetch-clause: +---,----------------------+ |--+-----INTO---v---host-variable-----+-------------------+------------------------------------| +----INTO DESCRIPTOR----descriptor-name-----+ © 2004 IBM Corporation
  • 25. Advanced Technical Support (ATS), Americas Cursor position and scrolling At OPEN CURSOR, cursor is positioned before first row After FETCH, fetched row becomes current row and cursor positioned on current row •When FETCH reaches end of file, cursor positioned after last row if scroll number positive and before first row if scroll number negative •Ability to scroll backwards and forwards through result set which is ever changing –Notethat scroll quantity counts new inserts and has no way of counting deleted rows –Usage example: airline reservation or credit card processing © 2004 IBM Corporation
  • 26. Advanced Technical Support (ATS), Americas Fetch Orientation and Cursor Position Result table FETCH BEFORE 1 Independent of FETCH FIRST 2 start 3 cursor position 4 5 FETCH ABSOLUTE 5 6 7 FETCH RELATIVE - 3 8 Current FETCH PRIOR Ending position 9 Cursor 10 XXXX XXXX XXXX XXXX FETCH CURRENT depends on start Position 11 cursor position FETCH or FETCH NEXT 12 13 FETCH RELATIVE 3 14 15 FETCH ABSOLUTE - 5 16 17 Independent of 18 start 19 cursor position 20 FETCH LAST FETCH AFTER © 2004 IBM Corporation
  • 27. Advanced Technical Support (ATS), Americas Cursor Positioning: Mixing Row and Rowset Positioned Fetches - Multi Row Fetch (V8) © 2004 IBM Corporation
  • 28. Advanced Technical Support (ATS), Americas Rowset Positioned Fetches Result table CUST_NO CUST_TYP CUST_NAME FETCH FIRST ROWSET 1 P Ian FOR 3 ROWS 2 P Mark 3 P John 4 P Karen FETCH NEXT ROWSET 5 P Sarah 6 M Florence 7 M Dylan 8 M Bert FETCH ROWSET STARTING 9 M Jo 10 R Karen AT ABSOLUTE 8 11 R Gary 12 R Bill FETCH NEXT ROWSET 13 R Geoff FOR 10 ROWS 14 R Julia 15 R Sally Partial Result Set © 2004 IBM Corporation
  • 29. Advanced Technical Support (ATS), Americas Partial Rowsets (Result Sets) © 2004 IBM Corporation
  • 30. Advanced Technical Support (ATS), Americas Determining rowset size If FOR n ROWS is NOT specified and cursor is declared for rowset positioning.. Size of rowset will be the same as the previous rowset fetch as long as –It was the previous fetch for this cursor –Or the previous fetch was a FETCH BEFORE or FETCH AFTER and the fetch before that was a rowset fetch Else rowset is 1 FETCH FIRST ROWSET FOR 5 ROWS Returns 5 rows FETCH NEXT ROWSET Returns the next 5 rows FETCH NEXT Returns a single row FETCH NEXT ROWSET Returns a single row FETCH NEXT ROWSET FOR 3 ROWS Returns 3 rows FETCH BEFORE Returns 0 rows FETCH NEXT ROWSET Returns 3 rows © 2004 IBM Corporation
  • 31. Advanced Technical Support (ATS), Americas Fetching beyond the result set If you try to fetch beyond the result set you will receive end of data condition –i.e., When there are only 5 rows left in result table and you request FETCH NEXT ROWSET FOR 10 ROWS, 5 rows will be returned with an SQLCODE +100 This includes where FETCH FIRST n ROWS ONLY has been specified © 2004 IBM Corporation
  • 32. Advanced Technical Support (ATS), Americas Partial Result Sets If you fetch beyond the end of the result set, you will receive an end of data condition ƒi.e., When there are only 5 rows left in result table and you request FETCH NEXT ROWSET FOR 10 ROWS, 5 rows will be returned - SQLCODE +100 ƒSQLERRD(3) will contain the number or rows returned ƒThis includes where FETCH FIRST n ROWS ONLY has been specified If you fetch beyond the beginning of the result set, you will receive an end of data condition ƒ i.e., if you are positioned on rows 3,4,5,6, and 7, and you request FETCH PRIOR ROWSET FOR 10 ROWS, 2 rows will be returned (Rows 1 and 2) - SQLCODE +20237 ƒSQLERRD(3) will contain the number or rows returned © 2004 IBM Corporation
  • 33. Advanced Technical Support (ATS), Americas Fetching Beyond the Result Set ABSOLUTE or RELATIVE If you fetch beyond the end of the result set, or beyond the beginning of the result set, you will receive an end of data condition ƒAssume you are positioned on row 5 in a result set with 10 rows. ƒFETCH ROWSET STARTING AT ABSOLUTE 15 ƒFETCH ROWSET STARTING AT RELATIVE -7 ƒNo rows will be returned - SQLCODE +100 ƒSQLERRD(3) will contain 0 ƒCursor position will be either “BEFORE” or “AFTER” depending on the direction of the FETCH. © 2004 IBM Corporation
  • 34. Advanced Technical Support (ATS), Americas What's my position with Dynamic Scroll? When you get a +100 fetching forward Application 1 Application 2 Cursor fetch doing searched updates –Position is AFTER CS, currentdata(no) Subsequent +100 –FETCH PRIOR will give you the last row, or –FETCH NEXT will give you +100 again Pos Delete When you get a +100 fetching backward Fetch –Position is BEFORE Current Subsequent +231 –FETCH NEXT will give you the first row, or –FETCH PRIOR will give you +100 When you get a +231 (row not found fetch current ) –Position is before the next row FETCH NEXT will give you the next row FETCH PRIOR will give you the previous row FETCH CURRENT will give you +231 +100 © 2004 IBM Corporation
  • 35. Advanced Technical Support (ATS), Americas Resulting Cursor Positions Fetch Current Current Current Current Resulting Resulting Resulting Resulting Orientation Position Position on Position on Position After Position on Position on Position Before Position After before first Last row Delete or First row Last Row first row last row normal Row row Update Hole NEXT OK OK +100 +100 +222 IF OK N/A IF +100 FROM LAST ROW PRIOR +100 +100 OK OK +222 IF OK IF+100 FROM N/A LAST ROW FIRST OK OK OK OK +222 IF OK N/A N/A LAST OK OK OK OK N/A IFOK N/A N/A BEFORE OK OK OK OK N/A N/A IF OK N/A AFTER OK OK OK OK +222 N/A N/A IF OK CURRENT +231 OK OK +231 +222 IF OK N/A N/A RELATIVE 0 ABSOLUTE OK OK OK OK +222 IF OK N/A IF +100 AND N +N OUT OF RANGE ABSOLUTE - OK OK OK OK +222 IF OK IF +100 AND N N/A N OUT OF RANGE RELATIVE OK OK +100 +100 +222 IF OK N/A IF +100 AND N +N OUT OF RANGE RELATIVE -N +100 +100 OK OK +222 IF OK IF +100 AND N/A N OUT OF RANGE © 2004 IBM Corporation
  • 36. Advanced Technical Support (ATS), Americas Locking with dynamic scrollable cursors ISO CS recommended for maximum concurrency –RR and RS severely restrict update of table by other users, therefore defeating purpose of SENSITIVE DYNAMIC Lock is held on current row as base table is always accessed directly - beware of multi row operations! As before, if ISO UR is on BIND option and SELECT statement contains FOR UPDATE OF, ISO promoted to CS As normal, if cursor defined WITH HOLD, locks will be held until first commit after close of cursor unless DSNZPARM SPRMRCHL = NO, in which case locks held for held cursors released at COMMIT © 2004 IBM Corporation
  • 37. Advanced Technical Support (ATS), Americas Serialization Preview Use existing locking mechanisms, added one RR - lock every page/row as it is read RS - lock every row that qualifies stage 1 predicate Review CS - lock each row fetched(currentdata(YES)) - lock only if cursor FOR UPDATE OF if (currentdata(no)) UR - No rows locked New! Optimistic Locking Mechanism (Static cursor only) Provides Maximum concurrency with isolation level of Cursor Stability No locks held between fetches Values Concurrency model (as opposed to the Lock Concurrency model which is pessimistic locking) © 2004 IBM Corporation
  • 38. OPTIMISTIC LOCKING Americas Advanced Technical Support (ATS), We release locks after fetch Because we are optimistic that no positioned update or delete will occur Even if it does, we are optimistic that the pertinent values will not have changed So we compare by value under a new lock to ensure data integrity No SCROLL FETCH row 1 FETCH Update row 2 and dynamic Lock row 1 Unlock row 1 Cursor Lock row 2 Time Row 1 locked Row 2 locked No Locks No Locks Update row 2 FETCH row 1 FETCH Lock row 2 Static Lock row 1 Lock row 2 Reevaluate SCROLL Unlock row 1 Unlock row 2 Compare Cursor Update © 2004 IBM Corporation
  • 39. Advanced Technical Support (ATS), Americas Updating via scrollable cursors Optimistic locking is used by static scrollable cursors –Static scrollable cursors hold no locks on base tables after OPEN CURSOR –Check at update time whether the row has not changed Optimistic locking is not used by dynamic scrollable cursors –Locks against the underlying table while fetching, similar to non-scrollable cursors Positioned UPDATE and DELETE always allowed if cursor not read-only and the page or row lock was acquired successfully © 2004 IBM Corporation
  • 40. Updating Using Static Scrollable Cursors Advanced Technical Support (ATS), Americas Positioned Updates & Deletes based on position on Temporary Result Table DB2 locks corresponding Base Table row on a positioned update or delete and verifies that the row still satisfies the search condition DB2 then matches the old values to the current values, while the row is locked, and allows the update or delete only if the values match Update/Delete disallowed if either the row fails search condition or the values do not match If the row passes the above two conditions then DB2 does the following –For UPDATE Updates the Base Table Refetches from the Base Table to reevaluate the predicate Updates the Result Table –If search condition fails - marks as Update Hole –If search condition satisfied - updates it with latest values –For DELETE Deletes the Base Table Row Updates (deletes row from) the Result Table –If search condition fails - marks as delete Hole –If search condition satisfied – deletes row The next FETCH will see these updates © 2004 IBM Corporation
  • 41. Some Advanced TechnicalScrollable Cursor considerations Dynamic Support (ATS), Americas Dynamic scrollable cursors are supported with stored procedures –SP itself can update via dynamic scrollable cursor but program calling SP is restricted from updating using allocated cursor Scalar functions and arithmetic expressions in SELECT list are reevaluated at every fetch Column functions (AVG, MIN, MAX, etc.) are calculated once at open cursor –Functions may not be meaningful because size of result set can change Use of non-deterministic function (built-in or UDF) in WHERE clause of select statement or statement name of scrollable cursor can cause misleading results –Result of function can vary from one FETCH to subsequent FETCH of same row Cursors requiring use of a workfile cannot be declared SENSITIVE DYNAMIC –e.g. - SELECT COL1, MAX(COL2), COUNT(*) FROM T1 GROUP BY COL1 ORDER BY 3 Changes to tables referenced in subqueries are not reflected Parallelism is not supported with dynamic scrollable cursors © 2004 IBM Corporation
  • 42. Advanced Technical Support (ATS), Americas Cursor Type Comparison Cursor Type Result Table Visibility of Visibility of Updatability Own Changes Others' (*) Changes SCROLL not Fixed, workfile No No No Specified (In V6) SCROLL not No workfile, base No Yes Yes Specified table access (No fetch (In V6) current) INSENSITIVE Fixed, declared No No No SCROLL temp table (In V7) SENSITIVE STATIC Fixed, declared Yes Yes Yes SCROLL temp table (Inserts not (Not Inserts) (In V7) allowed) SENSITIVE No declared temp Yes Yes Yes DYNAMIC SCROLL table, base table New (In V8) access * A cursor can become read-only if the SELECT statement references more than one table, or contains a GROUP BY etc. (read-only by SQL) © 2004 IBM Corporation
  • 43. Advanced Technical Support (ATS), Americas Static Scroll vs. Dynamic Scroll User table Dynamic scroll Static Result Cursor Position Table copy in Unique temp database Index Dynamic scroll Cursor Position Y OP Dynamic scroll C Static scroll Cursor Position Index Cursor Position NPSI Dynamic scroll Cursor Position DPSI Index © 2004 IBM Corporation
  • 44. Advanced Technical Support (ATS), Americas The HOLE Concept In STATIC SCROLL Cursors the number of rows in the result table is fixed but deletes and updates to the underlying base table can create DELETE HOLES or UPDATE HOLES Holes are detected on a Sensitive FETCH –DELETE HOLE - Created when the corresponding base table row has been deleted Once a delete hole, always a delete hole, delete holes are not refetched - HOWEVER, You can remove a delete hole only by opening the scrollable cursor, setting a savepoint, executing a positioned DELETE statement with the scrollable cursor, and rolling back to the savepoint. –UPDATE HOLE - Created when the corresponding base table row has been modified such that the values of the rows do not qualify the row for the query any longer Every SENSITIVE FETCH reevaluates the row against the predicate, if the evaluation fails, the row is marked as an Update Hole and a SQLCODE is returned An Update Hole can turn into a row again on a subsequent sensitive fetch of an Update hole. Basically, the row is updated back to its original value therefore the hole no longer exists. © 2004 IBM Corporation
  • 45. Advanced Technical Support (ATS), Americas Removing an Update Hole 1 EXEC SQL OPEN C4; 2 2 3 EXEC SQL 3 4 4 UPDATE A 5 Update 5 SET COL1=COL1+1; 6 hole EXEC SQL UPDATE A SET COL1=COL1-1; 1 2 3 Update 4 hole 5 disappears © 2004 IBM Corporation
  • 46. Advanced Technical Support (ATS), Americas Savepoints and Rollback Declare Cursor CUR1 SENSITIVE STATIC SCROLL for SELECT... Set EXT Savepoint SVPT1; (1) Open CURSOR CUR1; Fetch from CUR1 into...; Update ....where current of CUR1; (2) Fetch from CUR1 into...; (3) Rollback to Savepoint SVPT1; The update (2) is rolled back Cursor stays open Cursor keeps position from fetch (3) © 2004 IBM Corporation
  • 47. Advanced Technical Support (ATS), Americas Insensitive scrolling and holes Returns the row(s) from the result table as it is. DECLARE C1 INSENSITIVE SCROLL CURSOR FOR ACCOUNT ACCOUNT_NAME TYPE SELECT ACCOUNT, ACCOUNT_NAME FROM ACCOUNT ABC010 BIG PETROLEUM C WHERE TYPE = 'P' BWH450 HUTH & DAUGHTERS C FOR UPDATE OF ACCOUNT_NAME ZXY930 MIGHTY BEARS PLC C MNP230 MR P TENCH P OPEN C1 BMP291 BASEL FERRARI C XPM673 SCREAM SAVER PTY LTD C ULP231 MS S FLYNN P RID ACCOUNT ACCOUNT_NAME XPM961 MICHAL LINGERIE C A04 MNP230 MR P TENCH A07 ULP231 MS S FLYNN ACCOUNT table Cursor temporary table DELETE FROM ACCOUNT WHERE ACCOUNT = 'MNP230'; COMMIT; ACCOUNT ACCOUNT_NAME TYPE FETCH C1 INTO :hv_account, :hv_account_name ABC010 BIG PETROLEUM C BWH450 HUTH & DAUGHTERS C ZXY930 MIGHTY BEARS PLC C MNP230 MR P TENCH C ƒFETCH / FETCH ... ROWSET will not BMP291 BASEL FERRARI C recognize the delete XPM673 SCREAM SAVER PTY LTD C ƒhv_account will contain MNP230 ULP231 MS S FLYNN P XPM961 MICHAL LINGERIE C ƒhv_account_name will contain MR P ACCOUNT table TENCH ƒreturns 0 SQLCODE © 2004 IBM Corporation
  • 48. Update and delete holes for sensitive static Advanced Technical Support (ATS), Americas Updates the fetched row in the result table from the corresponding row in the base table of the cursor’s SELECT statement and returns the current values. Thus, it reflects changes made outside this cursor. DECLARE C1 SENSITIVE STATIC SCROLL CURSOR ACCOUNT ACCOUNT_NAME TYPE FOR SELECT ACCOUNT, ACCOUNT_NAME ABC010 BIG PETROLEUM C FROM ACCOUNT BWH450 HUTH & DAUGHTERS C WHERE TYPE = 'P' ZXY930 MIGHTY BEARS PLC C FOR UPDATE OF ACCOUNT_NAME MNP230 MR P TENCH P BMP291 BASEL FERRARI C OPEN C1 XPM673 SCREAM SAVER PTY LTD C ULP231 MS S FLYNN P RID ACCOUNT ACCOUNT_NAME XPM961 MICHAL LINGERIE C A04 MNP230 MR P TENCH A07 ULP231 MS S FLYNN ACCOUNT table Cursor temporary table DELETE FROM ACCOUNT WHERE ACCOUNT = 'MNP230'; COMMIT; ACCOUNT ACCOUNT_NAME TYPE ABC010 BIG PETROLEUM C FETCH C1 INTO :hv_account, :hv_account_name BWH450 HUTH & DAUGHTERS C ZXY930 MIGHTY BEARS PLC C ƒFETCH / FETCH ... ROWSET positions MNP230 MR P TENCH C at first row in temp table BMP291 BASEL FERRARI C XPM673 SCREAM SAVER PTY LTD C ƒrow has been deleted from base table ULP231 MS S FLYNN P ƒDB2 flags that FETCH is positioned on XPM961 MICHAL LINGERIE C ACCOUNT table a delete hole by returning SQL code +222 ƒhost variables are not reset © 2004 IBM Corporation
  • 49. Advanced Technical Support (ATS), Americas Considerations using static scrollable cursors - multi row operations When scrolling between rowsets: –With insensitive fetches, updates by the application itself could cause changes and holes –With sensitive fetches, updates by other applications could cause changes and holes –Forexample, FETCH PRIOR ROWSET may return update or delete holes in place of rows that were fetched before Row contents can change between fetches © 2004 IBM Corporation
  • 50. Advanced Technical Support (ATS), Americas Using Multi-Row FETCH with Scrollable Cursors As holes may occur, ensure at least one indicator variable array is defined for a column –Even if no nullable columns exist add an indicator variable array for at least one column –Ifnullable columns exist all indicator variable arrays provided are updated if a hole is found New value of -3 indicates hole •SQLCODE +222 will also be returned CUSTNO NULLABLE_COL IND_VAR CUST_TYPE 1000 M P 2000 -1 B -3 4000 F P © 2004 IBM Corporation
  • 51. Advanced Technical Support (ATS), Americas Considerations with dynamic scrollable cursors - multi row operations Starting point and contents of rowsets will change when scrolling back and forth Note that just after fetching the CURRENT ROWSET, other applications can insert rows in between the rows being returned as part of the rowset –Refetching current rowset would return different rows, unless ISOLATION(RR) FETCH PRIOR ROWSET will return the previous n rows that qualify from the start of the current cursor position –Therefore n rows will be returned as long as start of rowset is not reached © 2004 IBM Corporation
  • 52. Retrieve Data Backward with a Cursor Advanced Technical Support (ATS), Americas * Declare a cursor to retrieve the data backward * ********************************************* * from the EMP table. The cursor has access to * * Fetch the previous row in the table. * * changes by other processes. * ********************************************* EXEC SQL EXEC SQL DECLARE THISEMP SENSITIVE STATIC FETCH SENSITIVE PRIOR FROM THISEMP SCROLL CURSOR FOR INTO :EMP-NUM, :NAME2, :DEPT, :JOB-NAME SELECT EMPNO, LASTNAME, END-EXEC. WORKDEPT, JOB ********************************************* FROM DSN8810.EMP * Check that the fetched row is not a hole * END-EXEC. * (SQLCODE +222). If not, print the contents. * * Open the cursor * ********************************************* EXEC SQL IF SQLCODE IS GREATER THAN OR EQUAL TO 0 OPEN THISEMP AND SQLCODE IS NOT EQUAL TO +100 END-EXEC. AND SQLCODE IS NOT EQUAL TO +222 * Indicate what action to take when all rows * THEN PERFORM PRINT-RESULTS. * in the result table have been fetched. * ... EXEC SQL * Branch back to fetch the previous row. * WHENEVER NOT FOUND GO TO ... CLOSE-THISEMP * Close the cursor * END-EXEC. CLOSE-THISEMP. * Position the cursor after the last row of the result* EXEC SQL * table * CLOSE THISEMP EXEC SQL END-EXEC. FETCH AFTER FROM THISEMP END-EXEC. © 2004 IBM Corporation
  • 53. Advanced Technical Support (ATS), Americas Considerations for positioned update/delete It is possible for another application process to update or delete a row in the base table of the SELECT statement so that the specified row of the cursor no longer has a corresponding row in the base table –Ifthe cursor is non-scrollable, this could happen if the cursor is ISOLATION(CS) CURRENTDATA(NO) and lock avoidance succeeds –Ifthe cursor is static scrollable, this could happen, since the result set is materialized into a temporary table and all underlying locks are released (unless ISOLATION(RR) or ISOLATION(RS) –Ifthe cursor is dynamic scrollable, this could happen if the cursor is ISOLATION(CS) CURRENTDATA(NO) and lock avoidance succeeds. © 2004 IBM Corporation
  • 54. Advanced Technical Support (ATS), Americas Cursor Related SQLCODEs +222 - Update/delete hole detected +231- Cursor position invalid -222 - Update or delete attempted against an update or delete hole -224 - Result table does not agree with base table -225 - FETCH statement incompatible with Cursor definition -228 - For update of clause specified for Read-only cursor -243 - Sensitive Cursor cannot be defined for specific SELECT stmt -244 - SENSITIVE specified on FETCH is not valid -5012 - Host variable not exact numeric with scale zero Existing SQLCODEs in new situations +100, -104, -151, -199, -508, -510, -628 © 2004 IBM Corporation
  • 55. Advanced Technical Support (ATS), Americas Determining Attributes of a Cursor By Checking the SQLCA - for V8 consider GET DIAGNOSTICS After you open a cursor, you can determine the following attributes of the cursor by checking the following SQLWARN and SQLERRD fields of the SQLCA: SQLWARN1 Indicates whether the cursor is scrollable or non-scrollable. SQLWARN4 Indicates whether the cursor is insensitive (I), sensitive static (S), or sensitive dynamic (D). SQLWARN5 Indicates whether the cursor is read-only, readable and deletable, or readable, deletable, and updatable. SQLERRD(1) The number of rows in the result table of a cursor when the cursor position is after the last row (when SQLCODE is equal to +100). This field is not set for dynamic scrollable cursors. SQLERRD(2) The number of rows in the result table of a cursor when the cursor position is after the last row (when SQLCODE is equal to +100). This field is not set for dynamic scrollable cursors. SQLERRD(3) The number of rows in the result table of an INSERT when the SELECT statement of the cursor contains the INSERT statement. If the OPEN statement executes with no errors or warnings, DB2 does not set SQLWARN0 when it sets SQLWARN1, SQLWARN4, or SQLWARN5. See Appendix D of DB2 SQL Reference for specific information about fields in the SQLCA. © 2004 IBM Corporation
  • 56. Advanced Technical Support (ATS), Americas Determining Attributes of a Cursor By Using the GET DIAGNOSTICS Statement After you open a cursor, you can determine the following attributes of the cursor by checking these GET DIAGNOSTICS items: DB2_SQL_ATTR_CURSOR_HOLD Indicates whether the cursor can be held open across commits (Y or N) DB2_SQL_ATTR_CURSOR_ROWSET Indicates whether the cursor can use rowset positioning (Y or N) DB2_SQL_ATTR_CURSOR_SCROLLABLE Indicates whether the cursor is scrollable (Y or N) DB2_SQL_ATTR_CURSOR_SENSITIVITY Indicates whether the cursor is asensitive, insensitive, or sensitive to changes that are made by other processes (A, I, or S) DB2_SQL_ATTR_CURSOR_TYPE Indicates whether the cursor is declared static (S for INSENSITIVE or SENSITIVE STATIC) or dynamic (D for SENSITIVE DYNAMIC) © 2004 IBM Corporation
  • 57. Advanced Technical Support (ATS), Americas Example - How would you Improve this? * TABLE TXN ( TXNID CHAR(8), * TXNDATE CHAR(8) Sample application * DESC VARCHAR(8), * AMT INTEGER) program to DECLARE CURSOR CU1 display the last five SENSITIVE STATIC SCROLL FOR rows from a table SELECT TXNDATE, AMT WHERE TXNID = 'SMITH' ORDER BY TXNDATE; e.g. Last 5 transactions OPEN CURSOR CU1; from checking account FETCH LAST FROM CU1 INTO :HV1,:HV2 FETCH RELATIVE -4 FROM CU1 INTO :HV1,:HV2 App logic to Output row to screen DO I = 1 TO 3 FETCH NEXT FROM CU1 INTO :HV1,:HV2 App logic to Output row to screen END CLOSE CURSOR CU1 © 2004 IBM Corporation
  • 58. Advanced Technical Support (ATS), Americas Example - Alternative Sample application * TABLE TXN ( TXNID CHAR(8), * TXNDATE CHAR(8) program to * DESC VARCHAR(8), display the last five * AMT INTEGER) rows from a table DECLARE CURSOR CU1 e.g. Last 5 transactions SENSITIVE STATIC SCROLL FOR from checking account SELECT TXNDATE, AMT WHERE TXNID = 'SMITH' ORDER BY TXNDATE; OPEN CURSOR CU1; FETCH INSENSITIVE ABSOLUTE -5 FROM CU1 INTO :HV1,:HV2 DO I = 1 TO 4 FETCH NEXT FROM CU1 INTO :HV1,:HV2 App logic to Output row to screen END CLOSE CURSOR CU1 © 2004 IBM Corporation
  • 59. Advanced Technical Support (ATS), Americas ZPARM Considerations (V8) • MAX_NUM_CUR (MAX OPEN CURSORS) – Number of cursors, including allocated cursors, that are open at a given DB2 site per thread. – RDS will keep a total of currently open cursors. – If an application attempts to open a thread after the maximum is reached, the statement will fail. – In a data sharing group, this parameter has member scope. – Acceptable values: 0 to 99999 – Default (review default setting): 500 © 2004 IBM Corporation
  • 60. Advanced Technical Support (ATS), Americas Limit Result Table Size Scrollable Cursors support FETCH FIRST n ROWS ONLY –New function to limit number of rows in the result table –Limits the result table to FIRST n ROWS © 2004 IBM Corporation
  • 61. Advanced Technical Support (ATS), Americas REXX SUPPORT REXX to DB2 interface has changes for Scrollable Cursors V7 REXX to DB2 interface for V8 Dynamic Scrollable Cursors not implemented © 2004 IBM Corporation
  • 62. Advanced Technical Support (ATS), Americas DRDA considerations DB2 Linux, UNIX, and Windows V8.1 clients support dynamic scrollable cursors with FP4 – Only via the ODBC interface – Only when calling DB2 for z/OS V8 Dynamic scrollable cursors do not support the DRDA limited block fetch protocol – Toachieve blocking, use multi-row fetch operations. This is currently supported between: DB2 for z/OS V8 systems You can use any DB2 application programming interface on the requesting mainframe which supports the use of multi-row in DB2 V8. DB2 ODBC distributed clients using dynamic scrollable cursors and DB2 for z/OS V8 © 2004 IBM Corporation
  • 63. Advanced Technical Support (ATS), Americas Intersection with Other New V8 Functions Insert within SELECT –Adynamic cursor is NOT allowed with a SELECT statement containing INSERT, but STATIC cursor is allowed. E.g.: SELECT CHAR(MYROWID), MYIDENT into :HV1, HV2 FROM FINAL TABLE (INSERT INTO VEHICLE (COLOR , TYPE) VALUES (:HV3,:HV4)); Multi Row Fetch –Adynamic cursor can have a SELECT statement with a multi row fetch option. E.g.: DECLARE C1 SENSITIVE DYNAMIC CURSOR WITH ROWSET POSITIONING FOR SELECT * FROM EMP; FETCH ROWSET STARTING AT -3 FROM C1 FOR 3 ROWS INTO..... Data Partitioning Secondary Index(DPSI) –Dynamicscrolling is supported using a DPSI if selected by the optimizer. Nuance - Order may or may not be important. –Externally, DPSI is like any other index, however, DB2 has several LOC (Lines Of Code) to accommodate scrolling with DPSI, specially when the request includes an ORDER BY. © 2004 IBM Corporation
  • 64. Advanced Technical Support (ATS), Americas Index Manager Backward Scan Support Backward Scan in Index Manager is enabled in V7 Used for performance improvement of Max and Min Functions –e.g.. When MutualFundsTable has an ascending index on RateOfReturn SELECT FundName, MAX(RateOfReturn) WHERE Sector = 'Technology' FROM MutualFundTable; The ascending index is scanned backwards starting from the end until the WHERE clause is satisfied. Eliminates need for descending indexes for Max function performance (or ascending indexes for MIN) •Backward Index Scan is used more for DYNAMIC SCROLL Cursors during index scan in V8 No indexes on static table. © 2004 IBM Corporation
  • 65. Advanced Technical Support (ATS), Americas Backward index scan enabled In V8, DB2 selects an ascending index and can use a backward scan to avoid the sort for the descending order In V8, DB2 uses the descending index to avoid the sort and can scan the descending index backwards to provide the ascending order To be able to use an index for backward scan, –Index must be defined on the same columns as ORDER BY and –Ordering must be exactly opposite of what is requested in ORDER BY. –i.e., if index defined as DATE DESC, TIME ASC, can do: Forward scan for ORDER BY DATE DESC, TIME ASC Backward scan for ORDER BY DATE ASC, TIME DESC – But must sort for ORDER BY DATE ASC, TIME ASC ORDER BY DATE DESC, TIME DESC © 2004 IBM Corporation
  • 66. Advanced Technical Support (ATS), Americas Avoid Sort by using Backward Index Scan with ORDER BY in V8 Backward SELECT STATUS_DATE, STATUS index scan FROM ACCT_STAT Same WHERE ACCT_NUM = :HV Index is ORDER BY STATUS_DATE DESC, STATUS_TIME DESC; used. Forward SELECT STATUS_DATE, STATUS index scan FROM ACCT_STAT WHERE ACCT_NUM = :HV For ORDER BY STATUS_DATE ASC, STATUS_TIME ASC; scrollable and non DB2 optimizer will select an ascending index to scrollable provide a descending sort order by traversing the cursors index backwards rather than do a sort Index on ACCT_STAT is ACCT_NUM, STATUS_DATE, STATUS_TIME © 2004 IBM Corporation
  • 67. Advanced Technical Support (ATS), Americas IFCID Updates and SQL CODES in V8 IFCID 65 for OPEN CURSOR –adding information for Scrollability -scrollable/non-scrollable Sensitivity - sensitive/insensitive/not known Result Table Type -static/dynamic/not known IFCID 59 for FETCH –adding information for Sensitivity -sensitive/insensitive/not specified Fetch Orientation - first/last/before/after/next/previous/absolute/relative/unknown No new SQLCODES Updated SQLCODES: –+231 - Current position is not valid for fetch current Only possible with dynamic scrollable cursors with isolation level CS, CURRENTDATA(NO) when FETCH CURRENT is issued and the current row has been deleted or updated so that it does not meet the selection criterion –-244 - Keyword INSENSITIVE cannot be specified on the FETCH statement when fetching from a SENSITIVE DYNAMIC SCROLL cursor © 2004 IBM Corporation
  • 68. Basic Advanced Technical Support Application Design Decisions for (ATS), Americas What Result Types? –STATIC - fixed membership in result table –DYNAMIC - result table membership changes as rows are inserted and deleted in the table Restricted with SELECT statement No ORDER BY with UPDATE No Joins, Functions, complex SQL What Sensitivity? –ASENSITIVE - you don't know what you'll get INSENSITIVE SENSITIVE DYNAMIC –INSENSITIVE - explicitly specified (membership is fixed) –SENSITIVE - explicitly specified (membership depends on STATIC or DYNAMIC specification) • What Isolation Level and Lock size? –CS with CURRENTDATA(YES) recommended if doing updates. –CS with CURRENTDATA(NO) if not doing updates –Row level locking will give max concurrency, however at a CPU cost, so be careful. –But all locking semantics supported © 2004 IBM Corporation
  • 69. Advanced Technical Support (ATS), Americas Performance based on: DB2 UDB for z/OS Version 8 Performance Topics SG24-6465 © 2004 IBM Corporation
  • 70. Advanced Technical Support (ATS), Americas Hardware and software environment All the test cases use the following environment: • Hardware: IBM z900 Series 2064-216 (z900 turbo) – One LPAR with 2 dedicated processors – 7 GB real storage – Dedicated ESS model 800 (ESS) – 4 dedicated FICON channels • Software – z/OS V1.4 – DB2 V8 – Non-data sharing • Measurement data collected with DB2 Performance Expert V2 – Accounting trace class 1,2,3 – Statistics trace class 1,3,4,5,6,8 © 2004 IBM Corporation
  • 71. Advanced Technical Support (ATS), Americas Measurement methodology • One table defined in one segmented table space is used for all measurements. • All the measurements involved only table space scans (1 index is defined). • First time effect – Bring up the DB2 subsystem before each measurement to clear the data in the buffer pool, eliminating the inconsistency of different buffer hit ratios from run to run. • No concurrency issues – There is no other program accessing the table space during the measurements. • Read programs are bound with ISOLATION(CS) and CURRENT DATA(NO) • Update programs are bound with ISOLATION(RS) • Each program issues one commit after closing the cursor © 2004 IBM Corporation
  • 72. Advanced Technical Support (ATS), Americas Measurement cases • Read cursor (single row fetch) - Static vs. dynamic • Update cursor (single row fetch and update) - Static vs. dynamic • Multi-row FETCH using static or dynamic cursors - Single row vs. multi-row • Multi-row FETCH and UPDATE or DELETE using static or dynamic cursors - Single row vs. multi-row © 2004 IBM Corporation
  • 73. Advanced Technical Support (ATS), Americas Test scenario 1: Read cursor - Static vs. dynamic This test uses two types of FETCH programs. The intent is to compare insensitive with asensitive cursors for static and dynamic. The first program declares an insensitive scrollable cursor to retrieve data from the table. It means this program is strictly read-only (does not allow positioned update and delete). DECLARE C1 INSENSITIVE SCROLL CURSOR WITH HOLD FOR SELECT COL1, COL2, COL3, COL4, COL5, COL6, COL7, COL8, COL9, COL10 FROM TABLE WHERE COL2 < ‘ROWNNNNNNNNN’; DO I = 1 TO 50000; FETCH INSENSITIVE NEXT C1 INTO :COL1,:COL2,:COL3,:COL4,:COL5, :COL6,:COL7,:COL8,:COL9,:COL10; END; © 2004 IBM Corporation
  • 74. Advanced Technical Support (ATS), Americas The second program declares an ASENSITIVE SCROLLABLE cursor, which is the default in DECLARE CURSOR of V8. ASENSITIVE scrollable cursors allow DB2 to choose the maximum sensitivity of a cursor as possible, and in this case the following SELECT statement does not indicate read-only cursor (that is, a UNION or UNION ALL, ORDER BY, FOR READ ONLY, FOR FETCH ONLY) so DB2 determines dynamic as the maximum sensitivity. DECLARE C1 ASENSITIVE SCROLL CURSOR WITH HOLD FOR SELECT COL1, COL2, COL3, COL4, COL5, COL6, COL7, COL8, COL9, COL10 FROM TABLE WHERE COL2 < ‘ROWNNNNNNNNN’; DO I = 1 TO 50000; FETCH NEXT C1 INTO :COL1,:COL2,:COL3,:COL4,:COL5, :COL6,:COL7,:COL8,:COL9,:COL10; END; © 2004 IBM Corporation
  • 75. Advanced Technical Support (ATS), Americas TEST 1 • The measurements have been done varying the number of FETCHes and the number of qualified rows opened as a cursor. This is done to show how the performance is affected by different result set sizes (the size of the temporary table in the case of the static cursor examples). The summary of our test cases is: • Use ISO(CS) and CD(NO) as a bind parameter • For the read program these options provide maximum performance and concurrency. • 50k or 10 FETCHes • FETCH of 10 columns for a total of 50 bytes • Qualified 1 million or 100k rows opened as a cursor • In the program we specified 1,000,000 or 100,000 in • ‘ROWnnnnnnnnn’ row-id. • test results for static and dynamic cursors from the output of the DB2 PE trace record. © 2004 IBM Corporation
  • 76. Test 1 - Read cursor (single row fetch) - Static vs. dynamic Advanced Technical Support (ATS), Americas 95% performance 99% performance 85% performance improvement over improvement over improvement over Static! Static! Static! Fetch of 50K and 10 rows take The amount of FETCHes Times are almost the same time. This is becomes the deciding factor much lower due to populating the temp for the dynamic cursor’s because we table with 1 million rows. performance, whereas the only Class 3 time is about the same, number of qualifying rows populate overhead for opening and was a deciding factor 100K rows. © 2004 IBM Corporation formatting the temp table. in the static cursor model.
  • 77. Advanced Technical Support (ATS), Americas Test scenario 2: Update cursor - Static vs. dynamic • This case tests positioned updates and deletes. The intent is to compare sensitive cursors for static and dynamic. © 2004 IBM Corporation
  • 78. Advanced Technical Support (ATS), Americas DECLARE C1 SENSITIVE STATIC SCROLL CURSOR WITH HOLD FOR SELECT COL1, COL2, COL3, COL4, COL5,COL6 FROM TABLE WHERE COL2 < ‘ROWNNNNNNNNN’ FOR UPDATE OF COL6; DO I = 1 TO 1000; FETCH SENSITIVE NEXT C1 INTO :COL1,:COL2,:COL3,:COL4,:COL5, :COL6; UPDATE TABLE SET COL6 = :COL6 +10 WHERE CURRENT OF C1; END; (Same for Deletes) DECLARE C1 SENSITIVE DYNAMIC SCROLL CURSOR WITH HOLD FOR SELECT COL1, COL2, COL3, COL4, COL5, COL6 FROM TABLE WHERE COL2 < ‘ROWNNNNNNNNN’ FOR UPDATE OF COL6; DO I = 1 TO 1000; FETCH SENSITIVE NEXT C1 INTO :COL1,:COL2,:COL3,:COL4,:COL5, :COL6; UPDATE TABLE SET COL6 = :COL6 +10 WHERE CURRENT OF C1; END; (Same for Deletes) © 2004 IBM Corporation
  • 79. Advanced Technical Support (ATS), Americas Test 2 • As in the previous test case, we now examine UPDATEs and DELETEs with different numbers of qualified rows. The summary description of this test case is: • Use ISO(RS) as a bind parameter – This option protects data integrity (not for performance). • 100k or 10 FETCHes followed by 100k or 10 UPDATEs • 100k or 10 FETCHes followed by 100k or 10 DELETEs • FETCH of 6 columns for a total of 37 bytes • Qualified 1 M or 50k rows opened as a cursor © 2004 IBM Corporation
  • 80. Advanced Technical Support (ATS), Americas Test 2 - Update cursor (single row fetch and update) - Static vs. dynamic NOTE – same type of results as in test 1! © 2004 IBM Corporation
  • 81. Test 3 - Multi-row FETCH using static or dynamic cursors – Advanced Technical Support (ATS), Americas Single row vs. multi-row DECLARE C1 INSENSITIVE SCROLL CURSOR WITH ROWSET POSITIONING WITH HOLD FOR SELECT COL1, COL2, COL3, COL4, COL5, COL6, COL7, COL8, COL9, COL10 FROM TABLE WHERE COL2 < ‘ROWNNNNNNNNN’; FETCH INSENSITIVE FIRST ROWSET FROM C1 FOR 100 ROWS INTO :COL1,:COL2,:COL3,:COL4,:COL5, :COL6,:COL7,:COL8,:COL9,:COL10; DO I = 1 TO 499; FETCH INSENSITIVE NEXT ROWSET C1 INTO :COL1,:COL2,:COL3,:COL4,:COL5, :COL6,:COL7,:COL8,:COL9,:COL10; END; DECLARE C1 ASENSITIVE SCROLL CURSOR WITH ROWSET POSITIONING WITH HOLD FOR SELECT COL1, COL2, COL3, COL4, COL5, COL6, COL7, COL8, COL9, COL10 FROM TABLE WHERE COL2 < ‘ROWNNNNNNNNN’; FETCH FIRST ROWSET FROM C1 FOR 100 ROWS INTO :COL1,:COL2,:COL3,:COL4,:COL5, :COL6,:COL7,:COL8,:COL9,:COL10; DO I = 1 TO 499; FETCH NEXT ROWSET C1 INTO :COL1,:COL2,:COL3,:COL4,:COL5, :COL6,:COL7,:COL8,:COL9,:COL10; END; © 2004 IBM Corporation
  • 82. Test 3 - Multi-row FETCH using static or dynamic cursors – Advanced Technical Support (ATS), Americas Single row vs. multi-row Multi-row FETCH static No great difference Since the 1 million row temporary table is built row by row, regardless of the fetch implementation. Multi-row FETCH dynamic Reducing the number of trips between the application and database engine by a factor of 100 (50,000 fetches vs. 500 multi-fetches) accounts for the performance gains observed. © 2004 IBM Corporation
  • 83. Advanced Technical Support (ATS), Americas Test 4 - Single row vs. multi-row FETCH & UPDATE or DELETE The summary of this test case is: •Use ISO (RS) as a bind parameter •These options protect data integrity (not for performance). •40 FETCHes followed by UPDATE on a rowset of 25 rows •40 FETCHes followed by DELETE on a rowset of 25 rows •Qualified 1 million rows at open cursors © 2004 IBM Corporation
  • 84. Advanced Technical Support (ATS), Americas DECLARE C1 SENSITIVE STATIC SCROLL CURSOR WITH ROWSET POSITIONING WITH HOLD FOR SELECT COL1, COL2, COL3, COL4, COL5,COL6 FROM TABLE WHERE COL2 < ‘ROWNNNNNNNNN’ FOR UPDATE OF COL6; FETCH SENSITIVE FIRST ROWSET FROM C1 FOR 25 ROWS INTO :COL1,:COL2,:COL3,:COL4,:COL5,:COL6; UPDATE TABLE SET COL6 = :COL6+10 WHERE CURRENT OF C1; DO I = 1 TO 39; FETCH SENSITIVE NEXT ROWSET FROM C1 INTO :COL1,:COL2,:COL3,:COL4,:COL5,:COL6; UPDATE TABLE SET COL6 = :COL6 +10 WHERE CURRENT OF C1; END; (Same for Deletes) DECLARE C1 SENSITIVE DYNAMIC SCROLL CURSOR WITH ROWSET POSITIONING WITH HOLD FOR SELECT COL1, COL2, COL3, COL4, COL5, COL6 FROM TABLE WHERE COL2 < ‘ROWNNNNNNNNN’ FOR UPDATE OF COL6; FETCH SENSITIVE FIRST ROWSET FROM C1 FOR 25 ROWS INTO :COL1,:COL2,:COL3,:COL4,:COL5,:COL6; UPDATE TABLE SET COL6 = :COL6+10 WHERE CURRENT OF C1; DO I = 1 TO 39; FETCH SENSITIVE NEXT ROWSET FROM C1 INTO :COL1,:COL2,:COL3,:COL4,:COL5,:COL6; UPDATE TABLE SET COL6 = :COL6 +10 WHERE CURRENT OF C1; END; (Same for Deletes) © 2004 IBM Corporation
  • 85. Test 4 - Single row vs. multi-row FETCH & UPDATE or DELETE Advanced Technical Support (ATS), Americas Multi-row FETCH and UPDATE or DELETE static No significant changes in performance because of DTT build and population Multi-row FETCH and UPDATE or DELETE dynamic 17.8% performance improvement in DB2 class 2 CPU time (0.135 vs. 0.111) and a 7% performance improvement in DB2 class 2 elapsed time (0.589 vs. 0.547). © 2004 IBM Corporation
  • 86. Advanced Technical Support (ATS), Americas Performance Considerations – Page 1 of 2 Choosing the right Cursor Type –If you just want to blast out your data, choose forward only cursors –If you want to maintain position, go back and forth - choose Scrollable Cursors –If you don't care about the freshness of data, choose INSENSITIVE CURSORS –Ifyou want fresh some times, choose SENSITIVE on DECLARE and SENSITIVE or INSENSITIVE on FETCH –If you want to see inserts and not deletes, choose DYNAMIC CURSORS –Ifyou only want to scroll only between the first few rows of the result set - use FIRST n ROWS –Use stored procedures to OPEN in distributed environment if update not required Choose the appropriate isolation level - usually CS (CURRENTDAT(NO)) –RR/RS provides low concurrency - may not be feasible –Take advantage of optimistic locking (static only) Provide sufficient declared TEMP database table space storage for STATIC and INSENSITIVE cursors Commit as often as practical, use CURSOR HOLD © 2004 IBM Corporation
  • 87. Advanced Technical Support (ATS), Americas Performance Considerations – Page 2 of 2 The summary of the recommendations for static and dynamic cursors is: •Static scrollable cursors can be the better solution if: – You need to scroll backwards to obtain initial values – You do not care about constant changes (such as business intelligence or representative samples). - Optimistic locking if more concurrency is required. •Dynamic scrollable cursors can be preferable in situations where: – It is important for the application to access/see updated/inserted rows – Performance is important •Use multi-row operations whenever possible, particularly for the dynamic cursor model. © 2004 IBM Corporation
  • 88. Cursor Attributes on(ATS), Americas Advanced Technical Support PREPARE Statement EXEC SQL DECLARE mycursor CURSOR FOR mystmt; Flexibility EXEC SQL PREPARE mystmt ATTRIBUTES :attrvar FROM :stmttxt; EXEC SQL DESCRIBE mystmt INTO :mysqlda; EXEC SQL OPEN mycursor; EXEC SQL FETCH FROM mycursor USING DESCRIPTOR :mysqlda ; Applications can pass a host variable containing a list of attributes, such as SCROLL, WITH HOLD, WITH RETURN, INSENSITIVE, SENSITIVE STATIC, optimize-clause, isolation-clause, etc. © 2004 IBM Corporation
  • 89. Advanced Technical Support (ATS), Americas ODBC Scrollable Cursors Usage DefineCursor Type –Determine which cursor types supported by driver SQLGetInfo() SQL_CURSOR_SENSITIVITY SQL_SCROLL_OPTIONS SQL_FORWARD_ONLY_CURSOR_ATTRIBUTES1 SQL_FORWARD_ONLY_CURSOR_ATTRIBUTES2 SQL_KEYSET_CURSOR_ATTRIBUTES1 SQL_KEYSET_CURSOR_ATTRIBUTES2 SQL_STATIC_CURSOR_ATTRIBUTES1 SQL_STATIC_CURSOR_ATTRIBUTES2 SQL_DYNAMIC_CURSOR_ATTRIBUTES1 (Not in V7) SQL_FORWARD_ONLY_CURSOR_ATTRIBUTES2 (Not in V7) –Set Cursor Characteristic and Type e.g.. SQLSetStmtAttr SQL_ATTR_CURSOR_TYPE SQL_ATTR_CURSOR_STATIC Specify Rowset Size – e.g.. SQLSetStmtAttr SQL_ATTR_ROW_ARRAY_SIZE ROWSET_SIZE Fetch – e.g.. SQLFetchScroll SQL_FETCH_RELATIVE 5 © 2004 IBM Corporation
  • 90. Advanced Technical Support (ATS), Americas ODBC and Scrollable Cursors SQLFetchScroll(fetch orientation, offset) SQL_FETCH_NEXT SQL_FETCH_PRIOR SQL_FETCH_FIRST SQL_FETCH_LAST SQL_FETCH_ABSOLUTE SQL_FETCH_RELATIVE SQL_FETCH_BOOKMARK SQLSetPos(rownumber, operation, locktype) SQL_POSITION SQL_REFRESH SQL_UPATE SQL_DELETE SQL_ADD SQLBulkOperations(operation) SQL_ADD SQL_UPDATE_BY_BOOKMARK SQL_DELETE SQL_FETCH © 2004 IBM Corporation
  • 91. Advanced Technical Support (ATS), Americas JDBC Scrollable Cursors Usage Method to get type of Result Set (which is like a cursor) –int getType() TYPE_FORWARD_ONLY TYPE_SCROLL_INSENSITIVE TYPE_SCROLL_SENSITIVE Methods for Positioning –boolean next() –boolean previous() –boolean first() –boolean last() –boolean absolute() –boolean relative() –void afterLast() –void beforeFirst() Methods for retrieving column values –string getString(String columnName) –int getInt(int columnIndex) –... © 2004 IBM Corporation
  • 92. Advanced Technical Support (ATS), Americas JDBC Scrollable Cursors Example Connection con = DriverManager.getConnection( "jdbc:my_subprotocol:my_subname"); con.setTransactionIsolation(TRANSACTION_READ_COMMITTED); PreparedStatement pstmt = con.prepareStatement( "SELECT EMP_NO, SALARY FROM EMPLOYEES WHERE EMP_NO =?", ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); pstmt.setFetchSize(25); pstmt.setString(1,"1000010"); boolean b = rs.beforeFirst(); ResultSet rs3 = pstmt.executeQuery(); © 2004 IBM Corporation
  • 93. Advanced Technical Support (ATS), Americas Multi-row FETCH and INSERT © 2004 IBM Corporation
  • 94. Advanced Technical Support (ATS), Americas Multi-Row FETCH and INSERT What is it? ..... –Multi-row FETCH: A single FETCH statement can retrieve multiple rows of data from the result table of a query as a rowset – A rowset is a group of rows of data that are grouped together and operated on as a set –Supports dynamic and static SQL (Fetch always static) –Multi-row INSERT: A single SQL statement can insert one or more rows into a table or view Multi-row INSERT can be implemented as either static or dynamic SQL Benefits ..... –Enhances usability and power of SQL –Performance is improved by eliminating multiple trips between application and database engine; for distributed access, reduced network traffic –Combined with scrollable cursors important for browse applications © 2004 IBM Corporation
  • 95. Advanced Technical Support (ATS), Americas Rowsets A ROWSET is a group of rows from the result table of a query, which are returned by a single FETCH statement (or inserted by a single (multi-row) INSERT statement) •The program controls how many rows are returned in a rowset (it controls the size of the rowset) –Can be specified on the FETCH ... FOR n ROWS statement (n is the rowset size and can be up to 32767) Each group of rows are operated on as a rowset •Ability to intertwine single row and multiple row fetches for a multi-fetch cursor FETCH FIRST ROWSET STARTING AT ABSOLUTE 10 FROM CURS1 FOR 6 ROWS INTO :hva1, :hva2; © 2004 IBM Corporation
  • 96. Advanced Technical Support (ATS), Americas DECLARE CURSOR © 2004 IBM Corporation
  • 97. Advanced Technical Support (ATS), Americas FETCH examples EXAMPLE 1: Fetch the previous rowset and have the cursor positioned on that rowset EXEC SQL FETCH PRIOR ROWSET FROM C1 FOR 3 ROWS INTO... -- OR -- EXEC SQL FETCH ROWSET STARTING AT RELATIVE -3 FROM C1 FOR 3 ROWS INTO... EXAMPLE 2: Fetch 3 rows starting with row 20 regardless of the current position of the cursor EXEC SQL FETCH ROWSET STARTING AT ABSOLUTE 20 FROM C1 FOR 3 ROWS INTO... © 2004 IBM Corporation
  • 98. Advanced Technical Support (ATS), Americas DECLARE CURSOR - Example Declare C1 as the cursor of a query to retrieve a rowset from the table DEPT. The prepared statement is MYCURSOR EXEC SQL DECLARE CURSOR C1 CURSOR WITH ROWSET POSITIONING FOR MYCURSOR; Rowset positioning specifies whether multiple rows of data can be accessed as a rowset on a single FETCH statement – default is WITHOUT ROWSET POSITIONING © 2004 IBM Corporation
  • 99. Advanced Technical Support (ATS), Americas Distributed Multiple Row Fetch Flow Application Client Server Intermediate Table C1 C2 Com-Buffer Com-Buffer Buffer Page SQLCA Diagnostic Diagnostic Diagnostic Info Info Info Limited Block Fetch Flow Client Server Intermediate Table Application Com-Buffer Com-Buffer Buffer Page C1 C2 SQLCA Diagnostic Diag Info Info SQLCA Diag Info SQLCA © 2004 IBM Corporation
  • 100. Advanced Technical Support (ATS), Americas Local FETCH Flow x1000 x2000 Internal Buffers ‘N’ Table 1204 0 … … 4 1204 1300 0 1300 1700 0 1700 1900 0 1900 SQLDAID = SQLDA SQLDABC = 104 SQLN = 2 SQLD = 2 SQLTYPE[1] = 496 SQLLEN[1] = 4 SQLDATA[1] = x00001000 SQLIND[1] = x00002000 SQLNAME[1] = x‘0008000000010004’ © 2004 IBM Corporation
  • 101. Advanced Technical Support (ATS), Americas MRF Performance Considerations • Up to 50% faster FETCH performance Performance improvement largely due to savings of API costs Performance variable based on • Number of rows fetched • Number of columns fetched • Class 2 accounting (on or off) – savings larger if Class 2 is on • Examples – DSNTEP4 – 35% improvement on FETCH of 1000 rows with 5 cols and 20 cols – DSNTIAUL – 50% improvement on FETCH of 10000 rows with 5 cols and 20 cols • Up to 50% reduction in CPU cost for LBF –vs- V7 © 2004 IBM Corporation
  • 102. Advanced Technical Support (ATS), Americas Multiple Row INSERT Different Forms of INSERT ƒINSERT via VALUES is used to insert a single row into the table or view using values provided or referenced ƒINSERT via SELECT is used to insert one or more rows into table or view using values from other tables or views ƒINSERT via VALUES… FOR "n" ROWS form is used to insert multiple rows into table or view using values provided in host variable array FOR "n" ROWS ƒFor static, specify FOR "n" ROWS on INSERT statement (for dynamic INSERT, you may also specify FOR "n" ROWS on EXECUTE statement) ƒMaximum value of n is 32767 specified as host-variable, parameter marker, or literal value ƒInput provided with literal, host variable, or host variable array -- each array represents cells for multiple rows of a single column VALUES… FOR “n” ROWS clause allows specification of multiple rows of data ƒHost variable arrays used to provide values for a column on INSERT ƒExample: VALUES (:hva1, :hva2) FOR 10 ROWS © 2004 IBM Corporation
  • 103. Advanced Technical Support (ATS), Americas Host Variable Arrays Host variable array is an array in which each element of the array contains a value for the same column Changes have been made to allow host variable arrays in: ƒCOBOL ƒPL/1 ƒC++ ƒNOTE: Assembler support is limited to cases where USING DESCRIPTOR is allowed. Assembler pre-compiler does not recognize declaration of host variable arrays. The programmer is responsible for allocating storage correctly, etc. Can only be referenced in multi-row fetch or insert In general, arrays may not be arrays of structures © 2004 IBM Corporation
  • 104. Advanced Technical Support (ATS), Americas COBOL © 2004 IBM Corporation
  • 105. Advanced Technical Support (ATS), Americas COBOL Example 1: Declare a CURSOR C1 and fetch 10 rows using a multi-row FETCH statement 01 OUTPUT-VARS. 05 NAME OCCURS 10 TIMES. 49 NAME-LEN PIC S9(4)COMP-4 SY C. 49 NAME-DATA PIC X(40). 05 SERIAL-NUMBER PIC S9(9)COMP-4 OCCURS 10 TIMES. PROCEDURE DIVISION. EXEC SQL DECLARE C1 CURSOR WITH ROWSET POSITIONING FOR SELECT NAME, SERIAL# FROM CORPORATE.EMPLOYEE END-EXEC. EXEC SQL OPEN C1 END-EXEC. EXEC SQL FETCH FIRST ROWSET FROM C1 FOR 10 ROWS INTO :NAME, :SERIAL-NUMBER END-EXEC. © 2004 IBM Corporation
  • 106. Advanced Technical Support (ATS), Americas PL/I © 2004 IBM Corporation
  • 107. Advanced Technical Support (ATS), Americas PL/1 Example 2: You can retrieve 10 rows from the table CORPDATA.DEPARTMENT with: DCL DEPTNO(10) CHAR(3), DEPTNAME(10) CHAR(29) VAR, MGRNO(10) CHAR(6), ADMRDEPT(10) CHAR(3); DCL IND_ARRAY1(10)BIN FIXED(15); DCL IND_ARRAY2(10)BIN FIXED(15); ... EXEC SQL DECLARE C1 CURSOR WITH ROWSET POSITIONING FOR SELECT * FROM CORPDATA.DEPARTMENT; ... EXEC SQL FETCH FIRST ROWSET FROM C1 FOR 10 ROWS INTO :DEPTNO :IND_ARRAY1, :DEPTNAME :IND_ARRAY2, :MGRNO :IND_ARRAY3, :ADMRDEPT :IND_ARRAY4; © 2004 IBM Corporation
  • 108. Advanced Technical Support (ATS), Americas C/C++ © 2004 IBM Corporation
  • 109. Advanced Technical Support (ATS), Americas C++ Example 3: Declare an integer and varying character array to hold columns retrieved from a multi-row fetch statement long serial_num(10); struct { short len; char data [18]; }name [10]; ... EXEC SQL DECLARE C1 CURSOR FOR SELECT NAME, SERIAL# FROM CORPDATA.EMPLOYEE WITH ROWSET POSITIONING; ... EXEC SQL OPEN C1; EXEC SQL FETCH FIRST ROWSET FROM C1 FOR 10 ROWS INTO :NAME, :SERIAL_NUM; © 2004 IBM Corporation
  • 110. Advanced Technical Support (ATS), Americas #define TC 10; ODBC SQLWCHAR SAVE_H1WCHR[TC][1025]; SQLINTEGER LNSAVE_H1WCHR[TC]; /* Main Program */ int main() { /* initialize data */ wcscpy(SAVE_H1WCHR[0], (SQLWCHAR *)"abc 1"); ... wcscpy(SAVE_H1WCHR[9], (SQLWCHAR *)“abc 10"); hstmt=0; rc=SQLAllocStmt(hdbc, &hstmt); rc=SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_WCHAR, SQL_DBCLOB 1024, 0, SAVE_H1WCHR, (SQLINTEGER) 2050, (SQLINTEGER *) LNSAVE_H1WCHR); /* Set number of rows to insert */ rc=SQLParamOptions(hstmt, TC, NULL); /* Insert rows into DBCLOB column via SQLBindParameter */ rc=SQLPrepareW(hstmt,(wchar_t *) "INSERT INTO TABLECU (C1) VALUES (?)",SQL_NTS); rc=SQLExecute(hstmt); rc=SQLTransact(henv, hdbc, SQL_COMMIT); } /* End Main */ © 2004 IBM Corporation
  • 111. Advanced Technical Support (ATS), Americas JDBC try { stmt = con.prepareStatement ("insert into T1 values (?, ?)"); stmt.setInt (1, 1); stmt.setInt (2, 1); stmt.addBatch(); stmt.setInt (1, 2); stmt.setInt (2, 2); stmt.addBatch(); stmt.setInt (1, 3); stmt.setInt (2, 3); stmt.addBatch(); int[] updateCount = stmt.executeBatch(); for (int i = 0; i < updateCount.length; i++) actualResults.println(UpdateCount[" + i + "] = " + updateCount[i]); stmt.clearBatch(); con.commit(); stmt.close(); } © 2004 IBM Corporation
  • 112. Advanced Technical Support (ATS), Americas Multiple Row INSERT Different Forms of INSERT ƒINSERT via VALUES is used to insert a single row into the table or view using values provided or referenced ƒINSERT via SELECT is used to insert one or more rows into table or view using values from other tables or views ƒINSERT via VALUES… FOR "n" ROWS form is used to insert multiple rows into table or view using values provided in host variable array FOR "n" ROWS ƒFor static, specify FOR "n" ROWS on INSERT statement (for dynamic INSERT, you may also specify FOR "n" ROWS on EXECUTE statement) ƒMaximum value of n is 32767 specified as host-variable, parameter marker, or literal value ƒInput provided with literal, host variable, or host variable array -- each array represents cells for multiple rows of a single column VALUES… FOR “n” ROWS clause allows specification of multiple rows of data ƒHost variable arrays used to provide values for a column on INSERT ƒExample: VALUES (:hva1, :hva2) FOR 10 ROWS © 2004 IBM Corporation
  • 113. Advanced Technical Support (ATS), Americas INSERT © 2004 IBM Corporation
  • 114. Advanced Technical Support (ATS), Americas INSERT (cont) © 2004 IBM Corporation
  • 115. Advanced Technical Support (ATS), Americas INSERT Example 1 Insert a variable number of rows using host variable arrays for column values. Assume that the table T1 has one column and that a variable (:hv) number of rows of data are to be inserted into T1 table. EXEC SQL INSERT INTO T1 VALUES (:hva :hvind) FOR :hv ROWS ATOMIC; In this example, :hva represents the host variable array and :hvind represents the array of indicator variables © 2004 IBM Corporation
  • 116. Advanced Technical Support (ATS), Americas INSERT Example 2 Insert 10 rows into a table T2, and return the rows that have been inserted using a multiple row fetch statement. DECLARE CS1 ASENSITIVE SCROLL CURSOR WITH RETURN WITH ROWSET POSITIONING FOR SELECT T2.C1, T2.C2 FROM FINAL TABLE (INSERT INTO T2 VALUES (:hvai1 :hvindi1, :hva2 :hvinid2) FOR 10 ROWS); EXEC SQL OPEN CS1; /* INSERT OCCURS HERE */ EXEC SQL FETCH FIRST ROWSET FROM CS1 FOR 10 ROWS INTO :hvao1 :hvindo1, :hvao2 :hvindo2 © 2004 IBM Corporation
  • 117. Advanced Technical Support (ATS), Americas INSERT Example 3 Assume that table T3 has two columns: C1 (SMALLINT) and C2(INTEGER) The application allocates two arrays, :hva1 with 5 elements, and :hva2 with 10 elements :hva1 :hva2 INSERT INTO T3 (C1, C2) VALUES (:hva1, :hva2) FOR 5 ROWS; © 2004 IBM Corporation
  • 118. Advanced Technical Support (ATS), Americas INSERT Example 4 Assume that table T3 has three columns: S1 SMALLINT, I1 INTEGER, and T1 CHAR(8) The application allocates one array, :hva2 with 10 elements. :hv1 is a scalar host variable and the value for T1 is a special register In this example, hv1, and CURRENT TIME are used for each row of data, the values for I1 are obtained from host-variable-array :hva2 S1 I1 T1 :hv1 :hva2 5 1 13:30:05 5 1 5 2 13:30:05 INSERT INTO T3 VALUES (:hv1, 2 :hva2,CURRENT TIME) FOR 10 5 3 13:30:05 3 ROWS -- Assume CURRENT 4 TIME = ’ 13:30:05’ 5 4 13:30:05 5 5 5 13:30:05 6 5 6 13:30:05 7 Result of Insert 8 5 7 13:30:05 9 5 8 13:30:05 10 5 9 13:30:05 © 2004 IBM Corporation 5 10 13:30:05
  • 119. Advanced Technical Support (ATS), Americas Local INSERT Flow x1000 x2000 x3000 Internal Buffers Table 1204 0 4 … … 4 1204 1300 0 1300 1700 0 1700 1900 0 1900 SQLDAID = SQLDA SQLDABC = 104 SQLN = 2 SQLD = 2 SQLTYPE[1] = 496 SQLTYPE[2] = 496 SQLLEN[1] = 4 SQLLEN[2] = 4 SQLDATA[1] = x00001000 SQLDATA[2] = x00003000 SQLIND[1] = x00002000 SQLIND[2] = x00000000 SQLNAME[1] = x‘0008000000010004’ SQLNAME[2] =x‘0008000000020000’ © 2004 IBM Corporation
  • 120. Advanced Technical Support (ATS), Americas Distributed Multiple Row INSERT Flow Application Client Server Intermediate Table C1 C2 Com-Buffer Com-Buffer Buffer Page Distributed Chained INSERT Flow Application Client Server Intermediate Table Com-Buffer Buffer Page Stmt Data Com-Buffer © 2004 IBM Corporation
  • 121. Advanced Technical Support (ATS), Americas ATOMIC -vs- NOT ATOMIC ATOMIC ƒTraditional behavior ƒAll rows being inserted must successfully be inserted. If the insert for any row fails, all changes made to database by that INSERT statement are undone. NOT ATOMIC CONTINUE ON SQLEXCEPTION ƒInsert rows that are successful ƒReject rows that are not successful GET DIAGNOSTICS can be used to determine which rows were not successful ƒSQLCODE will indicate if all failed, all were successful or at least one failed © 2004 IBM Corporation
  • 122. Advanced Technical Support (ATS), Americas NOT ATOMIC CONTINUE ON SQLEXCEPTION – GET DIAGNOSTICS • NOT ATOMIC CONTINUE ON SQLEXCEPTION – Diagnostics are available for each failed row through GET DIAGNOSTICS – SQLCODE indicates if: • All failed – SQLSTATE 22530, SQLCODE -254 • All were successful, but warnings – SQLSTATE 01659, SQLCODE +252 • At least one failed – SQLSTATE 22529, SQLCODE -253 © 2004 IBM Corporation
  • 123. Advanced Technical Support (ATS), Americas ATOMIC -vs- NOT ATOMIC with Triggers •Trigger Behavior on Multiple Row Insert • ATOMIC •The inserts are processed as a single statement. •Any statement level triggers fire once for the statement, and the transition tables will include all of the rows that were inserted. • NOT ATOMIC CONTINUE ON SQLEXCEPTION •Inserts are processed separately. •Any statement level triggers are processed for each row that is inserted •Transition tables include the individual row that is inserted. •When errors are encountered with this option in effect, processing continues, and some of the specified rows will not be inserted. •In this case, if an insert trigger is defined on the underlying base table, the trigger transition table will only include rows that were successfully inserted. © 2004 IBM Corporation
  • 124. Advanced Technical Support (ATS), Americas PREPARE © 2004 IBM Corporation
  • 125. Advanced Technical Support (ATS), Americas EXECUTE © 2004 IBM Corporation
  • 126. Advanced Technical Support (ATS), Americas PREPARE and EXECUTE - Example Assume that the prog table has 9 columns. Prepare and execute a dynamic INSERT statement which inserts 5 rows of data into the prog table. stmt = 'INSERT INTO prog (iwhid, updated_by, update_ts, name, short_description, orderNo, parmData, parmDataLong, VWProgKey) VALUES (?,?,?,?,?,?,?,?,?)'; attrvar = ’FOR MULTIPLE ROWS ’; NROWS = 5; EXEC SQL PREPARE ins_stmt ATTRIBUTES :attrvar FROM :stmt; EXEC SQL EXECUTE ins_stmt FOR :NROWS ROWS USING :V1,:V2,:V3,:V4,:V5,:V6,:V7,:V8,:V9; In this example, each host variable in the USING clause represents an array of values for the corresponding column of the target of the INSERT statement. © 2004 IBM Corporation
  • 127. Advanced Technical Support (ATS), Americas INSERT SQLDA Considerations SQLDA must contain a valid description of the host variable arrays or buffers which contain the values to be inserted ƒEach SQLVAR describes a host variable or host variable array which represents a buffer which contains value(s) for a column of target table ƒSQLDA must have enough storage to contain SQLVAR for each target column for which values are provided, plus an additional SQLVAR entry for use by DB2 for z/OS ƒPrior to the multi-row insert, the SQLDA fields must be set correctly to include number of SQLVAR occurrences, number of variables used, pointer to arrays, indicator variables etc. © 2004 IBM Corporation
  • 128. Advanced Technical Support (ATS), Americas MRI Performance Considerations • Up to 30% faster INSERT performance Performance improvement largely due to savings of API costs Savings flattens out quickly, for example, savings (as a percentage) was equal • For 100 Rows, 500 Rows, 1000 Rows… • Reasonable “n” for MRI is about 200 – no additional savings above that, and downside (rollback) increases. Distributed MRI performance up to 70% Elapsed Time and 50% Server CPU time reductions seen Performance variable based on • Number of rows INSERTed • Number of columns INSERTed • Number of INDEXes on table • Class 2 accounting (on or off) – savings larger if Class 2 is on Note: Don’t use MRI to INSERT 1 row due to overhead to set up for MRI Similar improvement with UPDATE and DELETE WHERE CURRENT OF when updating/deleting the entire rowset. This is in addition to the savings provided by MRF © 2004 IBM Corporation
  • 129. Advanced Technical Support (ATS), Americas Multi-row FETCH / INSERT -- DRDA Considerations Can be implemented by any requester or server that supports DRDA Version 3 Between DB2 for z/OS V8 systems –Multi-row INSERT does not affect blocking a single rowset is inserted in a single INSERT statement –For remote client one rowset returned in a network request 32K block size is ignored –A single network multi-row fetch or insert request can maximum be 10 MB Between DB2 on distributed platforms and DB2 for z/OS V8 –No support for multi-row operations in embedded SQL –In ODBC/CLI driver Limited support for multi-row FETCH Support for multi-row INSERT © 2004 IBM Corporation
  • 130. Advanced Technical Support (ATS), Americas Positioned DELETE Positioned Update © 2004 IBM Corporation
  • 131. Advanced Technical Support (ATS), Americas GET DIAGNOSTICS © 2004 IBM Corporation
  • 132. Advanced Technical Support (ATS), Americas GET DIAGNOSTICS statement Enables more diagnostic information to be returned than can be contained in SQLCA Returns SQL error information –for overall statement –for each condition (when multiple conditions occur) Supports SQL error message tokens greater than 70 bytes (SQLCA limitation) Must be embedded - cannot be dynamically prepared INSERT INTO T1 FOR 5 ROWS VALUES (:array); GET DIAGNOSTICS :errcount = NUMBER; DO || = 1 TO ERR_COUNT; GET DIAGNOSTICS FOR CONDITION :|| :rc = RETURNED_SQLSTATE; END; © 2004 IBM Corporation
  • 133. Advanced Technical Support (ATS), Americas GET DIAGNOSTICS Examples To determine how many rows were updated in an UPDATE statement – GET DIAGNOSTICS :rcount = ROW_COUNT; To handle multiple SQL errors during a NOT ATOMIC multi-row insert –GET DIAGNOSTICS :numerrors = NUMBER; –Then code a loop to execute the following for the number of errors GET DIAGNOSTICS CONDITION :i :retstate = RETURNED_SQLSTATE To see all diagnostic information for a SQL statement –GET DIAGNOSTICS :diags = ALL STATEMENT –Sample output in :diags Number=1; Returned_SQLSTATE=02000; DB2_RETURNED_SQLCODE=+100; Would continue for all applicable items and for all conditions Items are delimited by semicolons © 2004 IBM Corporation
  • 134. Advanced Technical Support (ATS), Americas INSERT within SELECT Statement © 2004 IBM Corporation
  • 135. Advanced Technical Support (ATS), Americas INSERT within SELECT statement What is it? ..... –Users can automatically retrieve column values inserted in tables by DB2 such as: Identity columns, sequence values User-defined defaults, expressions Columns modified by BEFORE INSERT triggers ROWIDs Benefits ..... –Enhances usability and power of SQL –Cuts down on network cost in application programs –Cuts down on procedural logic in stored procedures © 2004 IBM Corporation
  • 136. Advanced Technical Support (ATS), Americas INSERT within SELECT Examples ROWID NOT NULL DECLARE CS1 CURSOR FOR GENERATED ALWAYS SELECT EMP_ROWID FROM FINAL TABLE (INSERT INTO DSN8810.EMP_PHOTO_RESUME (EMPNO) SELECT EMPNO FROM DSN8810.EMP)); NOT NULL WITH DEFAULT 'PROJECT NAME UNDEFINED' SELECT PROJNAME INTO :name_hv FROM FINAL TABLE (INSERT INTO DSN8810.PROJ (PROJNO,DEPTNO,RESPEMP) VALUES(:projno_hv,:deptno_hv,:respemp_hv)); © 2004 IBM Corporation
  • 137. Advanced Technical Support (ATS), Americas INSERT within SELECT - Ordering Example CREATE TABLE EMPLOYEE (EMPNO INTEGER GENERATED ALWAYS AS IDENTITY, INTEGER GENERATED ..... ALWAYS AS IDENTITY DECLARE CS2 CURSOR WITH ROWSET POSITIONING FOR SELECT EMPNO FROM FINAL TABLE (INSERT INTO EMPLOYEE (NAME, TELE) VALUES(:HVA1, :HVA2)) ORDER BY INPUT SEQUENCE; Result Input EMPNO 1 HVA1 HVA2 2 Liz 555-1212 3 David 555-9876 Jessica 555-0110 © 2004 IBM Corporation
  • 138. Advanced Technical Support (ATS), Americas Trigger example CREATE TRIGGER TRIG1 NO CASCADE BEFORE INSERT ON EMPLOYEE REFERENCING NEW AS NEWSALARY FOR EACH ROW MODE DB2SQL WHEN (NEWSALARY.LEVEL = 'Associate') SET NEWSALARY.SALARY = NEWSALARY.SALARY + 5000.00 SELECT NAME,SALARY INTO :name_hv, :salary_hv FROM FINAL TABLE :name_hv = 'New (INSERT INTO EMPLOYEE(NAME,SALARY,LEVEL) Hire' :salary_hv = VALUES('New Hire',35000.00,'Associate')) 40000.00 © 2004 IBM Corporation
  • 139. Advanced Technical Support (ATS), Americas Result table from INSERT statement Result table is internal work file -- not temporary table Contains all of the rows inserted, includes all of the columns requested in the SELECT list –After any BEFORE triggers –Generated columns (identity, ROWID, columns based on expressions) –After enforcing constraints (check, unique index, referential integrity) –Before any AFTER triggers © 2004 IBM Corporation
  • 140. Advanced Technical Support (ATS), Americas Using WITH HOLD Option Using WITH HOLD option on cursor –Example: user declares cursor, opens cursor, fetches 2 rows, performs commit, then fetches 3rd row successfully DECLARE CS1 CURSOR WITH HOLD FOR SELECT EMP_ROWID FROM FINAL TABLE (INSERT INTO DSN881.EMP_PHOTO_RESUME(EMPNO) SELECT EMPNO FROM DSN881.EMP); OPEN CS1; <------------------- inserts 5 rows FETCH FROM CS1 INTO :hv1; <------------------ receives row id for 1st row FETCH FROM CS1 INTO ::hv2; <------------------ receives row id for 2nd row COMMIT; <------------------ all 5 inserts are committed FETCH FROM CS1 INTO : HV3; <------------------ receives row id for 3rd row © 2004 IBM Corporation
  • 141. Advanced Technical Support (ATS), Americas Other considerations Only scrollable cursors defined as INSENSITIVE are valid –Ifdefined as ASENSITIVE a warning is returned indicating that it is being treated as INSENSITIVE – If SENSITIVE DYNAMIC or SENSITIVE STATIC specified, an error is returned Views must be defined WITH CASCADED CHECK OPTION if WHERE clause is used in view definition Example: CREATE VIEW VW1 AS SELECT I1 FROM T1 WHERE I1 > 10 WITH CASCADED CHECK OPTION; COMMIT –Allchanges made by INSERT (including those embedded in OPEN CURSORS) and SELECT INTO containing INSERT executed during unit of recovery are committed and savepoints that were set with unit of recovery are released © 2004 IBM Corporation
  • 142. Advanced Technical Support (ATS), Americas Using SAVEPOINT and ROLLBACK If application sets a save point prior to opening cursor and then rolls back to that savepoint, all inserts are undone Example: DECLARE CS2 CURSOR FOR SELECT EMP_ROWID FROM FINAL TABLE (INSERT INTO DSN881.EMP_PHOTO_RESUME(EMPNO) SELECT EMPNO FROM DSN881.EMP); SAVEPOINT A ON ROLLBACK RETAIN CURSORS; <--- sets 1st savepoint OPEN CS2; <--- inserts the rows SAVEPOINT B ON ROLLBACK RETAIN CURSORS; <--- sets 2nd savepoint ... ROLLBACK TO SAVEPOINT B; <--- rows still exist in table DSN881.EMP_PHOTO_RESUME ROLLBACK TO SAVEPOINT A; <--- all inserted rows are undone © 2004 IBM Corporation
  • 143. Advanced Technical Support (ATS), Americas Interaction with other statements and clauses FETCH FIRST clause –Does not affect which rows are inserted –Allof the rows from the INSERT statement will be inserted into the target object –Theresult table will only contain those rows for which the FETCH FIRST clause satisfies •DECLARE CURSOR –The cursor will be read-only •OPEN CURSOR –SQLERRD3 will be set to reflect the effects of the INSERT statement (number of rows inserted) © 2004 IBM Corporation
  • 144. Advanced Technical Support (ATS), Americas Interaction with other V8 features Multiple CCSID Sets per SQL Statement Example: DECLARE CS1 CURSOR FOR SELECT C1 FROM FINAL TABLE (INSERT INTO EBCDIC_T1 SELECT C2 FROM ASCII_T1); ASCII data is selected and converted into EBCDIC data © 2004 IBM Corporation
  • 145. Advanced Technical Support (ATS), Americas EXPLAIN output –New Information Stored in Plan Table –ACCESSTYPE -- V indicates that buffers will be used for singleton INSERT VALUES clause for INSERT within SELECT –TABLE_TYPE -- B indicates buffers will be used Example: SELECT EMPNO INTO :HV1 FROM FINAL TABLE (INSERT INTO EMPLOYEE (NAME, SALARY,LEVEL) VALUES('The New Guy', 50000.00, 'Staff')); QBLOCK_T QBLOCKNO PLANNO ACCESSTYPE TABLE_TYPE YPE SELECT 1 1 V B INSERT 2 0 © 2004 IBM Corporation
  • 146. Advanced Technical Support (ATS), Americas Identity Column Enhancements © 2004 IBM Corporation
  • 147. Advanced Technical Support (ATS), Americas Identity column What's new? ..... –Identity column introduced in Version 6 Automatically generated by DB2 Unique, sequential, recoverable values Useful for generating unique primary key values –in Version 8, ALTER TABLE statement is extended to include identity column specifications to allow modifying the attributes of an existing identity column Benefits ..... –Betterperformance and concurrency than application generated counters –Guaranteed uniqueness both within an individual subsystem and a data sharing group –Recoverability in the event of a DB2 system failure –Failureof one data sharing member will not impact the other members from generating key values –Simple implementation which is internal to DB2 © 2004 IBM Corporation
  • 148. Advanced Technical Support (ATS), Americas Identity column -- Version 8 enhancements Dynamic alter of identity column attributes –ALTER TABLE . . . ALTER COLUMN extended to: Enable modification of identity column attributes, and Specify continuation of the sequence associated with identity column from a new point in the range of values that is different from where the column values would otherwise have continued –Only future values of column affected by change –Cannot alter data type of identity column –Unused cache values may be lost when column attributes are altered New keyword support to aid porting from other vendor platforms –NO MINVALUE –NO MAXVALUE –NO ORDER, ORDER Allows: – INCREMENT BY to be 0 – MINVALUE = MAXVALUE Prevent loss of unused cache values at end of LOAD for identity column © 2004 IBM Corporation
  • 149. Advanced Technical Support (ATS), Americas Identity column -- considerations Before Version 8, if you had a requirement to unload and reload your tables, you were forced to specify GENERATED BY DEFAULT for the identity column –Ifyou had specified GENERATED ALWAYS at table design, the only option to unload / reload would be to: Unload the table DROP the table Re-CREATE the table using GENERATED BY DEFAULT Reload the table –Otherwise, DB2 will generate new values for the rows during the reload, which is probably not what you want With Version 8, if you specify GENERATED ALWAYS and later have a requirement to unload / reload your tables, you could: – ALTER TABLE . . . ALTER COLUMN . . . SET GENERATED BY DEFAULT –Unload the table –Reload the table –ALTER TABLE . . . ALTER COLUMN . . . SET GENERATED ALWAYS © 2004 IBM Corporation
  • 150. Advanced Technical Support (ATS), Americas Sequence Objects © 2004 IBM Corporation
  • 151. Advanced Technical Support (ATS), Americas Sequence objects What is it? ..... –A way for applications to have the DBMS generate unique numeric key values and to coordinate keys across multiple rows and tables –Sequence object: User-defined object that generates a sequence of numeric values Stand-alone object -- NOT TIED TO A TABLE Generated values returned to user -- can be used for anything by user –DB2 does not wait for transaction that has incremented a sequence to commit before allowing the sequence to be incremented again by another transaction Benefits ..... –Better performance and concurrency than application generated counters –Guaranteed uniqueness –Recoverability in the event of a DB2 system failure –Can be shared across members of data sharing group –Failureof one data sharing member will not impact the other members from generating key values –Family compatibility and porting of applications from other vendor platforms © 2004 IBM Corporation
  • 152. Advanced Technical Support (ATS), Americas Problems with application generated sequence numbers Common application-level implementation is to maintain a one row table that contains the sequence number –Each transaction locks this table, increments the number, commits (that is, one transaction at a time can increment the sequence number) –OR a variation of this theme is to use SELECT MAX( ) + 1...WITH RR followed by INSERT using retrieved key –Problem: Page containing counter constitutes hot spot in data base resulting in unpredictable transaction delays caused by inter-system P-lock negotiation for that page and by buffer invalidation and refresh. Contention inhibits transaction throughput and application processing power If one member fails, retained locks held by failed member can prevent access to shared counter from surviving members Identity columns provide a partial solution © 2004 IBM Corporation
  • 153. Advanced Technical Support (ATS), Americas Sequence Objects solution Avoid the concurrency and performance problems that can result when user applications generate their own sequence numbers (i.e., hotspots) •DB2 sequences allow multiple transactions to concurrently increment sequence number and guarantee each number will be unique •Sequence can be accessed and incremented by many users without waiting –DB2 does not wait for a transaction that has incremented a sequence to commit before allowing the sequence to be incremented again by another transaction Failure of one DB2 member in group will never prevent access (R/W) to sequence from surviving members –No retained locks to prevent access to sequence © 2004 IBM Corporation
  • 154. Advanced Technical Support (ATS), Americas Sequence objects vs. identity columns Sequences Identity columns Stand-alone object Tied to a table Can use one sequence for many tables One to one relationship between identity or many sequences in one table and tables Retrieved via NEXT VALUE FOR / Retrieved via IDENTITY_VAL_LOCAL PREVIOUS VALUE FOR expressions function -- within agents scope only Can be altered via ALTER SEQUENCE Can be altered via ALTER TABLE (ALTER COLUMN) Prior to V8 could not be altered © 2004 IBM Corporation
  • 155. Advanced Technical Support (ATS), Americas SQL to Support Sequence Objects CREATE SEQUENCE –Creates a sequence at the application server –Can be embedded in application program or used interactively ALTER SEQUENCE –Canbe used to change INCREMENT BY, MIN VALUE, MAXVALUE, CACHE, CYCLE and to RESTART WITH different sequence –Only future values affected and only after COMMIT of ALTER –Cannot alter data type of sequence –Unused cache values may be lost DROP SEQUENCE COMMENT ON SEQUENCE GRANT / REVOKE ON SEQUENCE NEXT VALUE FOR and PREVIOUS VALUE FOR © 2004 IBM Corporation
  • 156. Advanced Technical Support (ATS), Americas Next and Previous Values Applications can refer to the named sequence object to get its current or next value –NEXT VALUE FOR < sequence-name > –PREVIOUS VALUE FOR < sequence-name > Returns most recently generated value for sequence for previous statement within current session NEXT VALUE must have been invoked within current session Can be invoked during: –SELECT statement or SELECT INTO statement within select-clause (except if statement contains DISTINCT or UNION keyword, GROUP BY or ORDER BY) –INSERT statement within VALUES clause –INSERT statement within select-clause of fullselect –Update statement within SET clause (except NEXT VALUE cannot be in select-clause of fullselect of expression) –VALUES or VALUES INTO statement (except within select-clause of fullselect of expression) –CREATE PROCEDURE, FUNCTION, TRIGGER –Set :hv = NEXT VALUE FOR sequence © 2004 IBM Corporation
  • 157. Advanced Technical Support (ATS), Americas Ranges and Cycles Sequence can be optionally defined to CYCLE after reaching it's maximum value (or minimum if descending) –Sequence will wrap around to other end of range and start new cycle of values –Default is NOCYCLE First cycle always starts with START WITH value but all subsequent cycles start with MINVALUE (asc sequence) or MAXVALUE (desc sequence) If NO CYCLE in effect, sequence value generation will stop when sequence reaches end of logical range of values © 2004 IBM Corporation
  • 158. Advanced Technical Support (ATS), Americas Duplicate Sequences Sequences are guaranteed to be unique within a cycle However, duplicates can occur if : –Sequence cycles while data from previous cycles still exist –Sequence is restarted with a value that has already been generated –Ascending/descending direction of sequence is reversed by ALTER statement (i.e., changing INCREMENT BY from positive to negative number, vice versa) –System crashes followed by COLD START or CONDITIONAL RESTART and skips forward recovery leaving SYSIBM.SYSSEQUENCES table in inconsistent state –A point-in-time recovery of SYSIBM.SYSSEQ table space regresses SYSIBM.SYSSEQUENCES table to point in time, causing MAXASSIGNEDVAL to become inconsistent with actual current point of the sequence © 2004 IBM Corporation
  • 159. Advanced Technical Support (ATS), Americas Caching sequence numbers CACHE option allows DB2 to cache preallocated values in memory for fast access –Reduces synchronous I/O to the catalog table SYSIBM.SYSSEQUENCES –I/O to table only required when cached values are exhausted Recommended value is 20 –Assigning a higher value gives no benefit and increases size of gap should a failure occur In data sharing, each member will have its own set of cached values for a single sequence –Numbers will not be allocated in sequence Use NOCACHE if values must be assigned in strict numeric order –Each assignment of sequence value results in update of Catalog © 2004 IBM Corporation
  • 160. Advanced Technical Support (ATS), Americas Data sharing example SYSIBM.SYSSEQUENCES NAME VALUE CACHE SEQ1 41 20 DB2A DB2B 1-20 in cache 21-40 in cache Once 20 is used If member fails 61-80 will requests another 20 be cached on NEXTVAL (Maximum of 20 values will be lost) © 2004 IBM Corporation
  • 161. Advanced Technical Support (ATS), Americas Sequences and gaps Gaps are possible if: –Transaction advances sequence and then rolls back –SQL statement leading to generation of next value fails after value generated –NEXTVAL used in SELECT statement of cursor in DRDA where client uses block- fetch and not all retrieved rows are FETCHed –Sequence or identity column associated with sequence is altered and then ALTER rolled back –Sequence or identity column table DROPped and then DROP rolled back –SYSIBM.SYSSEQ tablespace is stopped, leading to loss of unused cache values –DB2 system failure or shut-down leading to loss of unassigned cache values lost causing gap in sequence Note that a transaction incrementing a sequence twice may not be assigned consecutive values Big gaps could be removed by Altering sequence using RESTART WITH parameter © 2004 IBM Corporation
  • 162. Advanced Technical Support (ATS), Americas Recoverability of sequences If DB2 fails, sequence status is recovered from catalog and logs, thereby guaranteeing unique sequence values continue to be generated Unassigned sequence values kept in cache of failing members are lost –With efficient use of sequence, gaps can be minimized DB2 may generate duplicate sequence numbers after restart if no log records are processed in forward recovery If there is a gap between first value assigned before system crash and value assigned after system crash –ALTER sequence to RESTART WITH value that is next value in sequence to be assigned –DROP and then reCREATE sequence specifying a START WITH value that is next value in sequence to be assigned –SELECT MAX(colname) or SELECT MIN(colname) may give actual last assigned value (colname is column to which sequence numbers were being assigned) -- works only if every value generated goes into one table -- won't work if CYCLE used © 2004 IBM Corporation
  • 163. Advanced Technical Support (ATS), Americas Sequence Objects -- Catalog and Directory SYSIBM.SYSSEQ tablespace – SYSIBM.SYSSEQUENCES -- Records all data about SEQUENCE attributes MAXASSIGNEDVAL column -- last possible assigned value New columns: –PRECISION -- Precision of sequence object's decimal or other numeric data type –RESTART WITH -- RESTART WITH numeric constant value specified during ALTER of the sequence or NULL SYSIBM.SYSSEQ2 tablespace – SYSIBM.SYSSEQUENCESDEP New Column: –DTYPE –BSCHEMA / BNAME –SYSIBM.SYSSEQUENCEAUTH NEW TABLE © 2004 IBM Corporation
  • 164. Advanced Technical Support (ATS), Americas CURRENT PACKAGE PATH Special Register © 2004 IBM Corporation
  • 165. Advanced Technical Support (ATS), Americas CURRENT PACKAGE PATH special register What is it? ..... –CURRENT PACKAGE PATH special register: Used for package collection resolution Means for application to specify a list of package collections to DB server (similar to PKLIST) DB server (rather than application requester) can search through list and find first package that exists with specified package name Control for applications that do not run under a DB2 plan Benefits ..... –Reduce network traffic and improve CPU/elapsed time for application using DRDA from a z/OS requester –Allows nested procedure, user-defined function to be implemented without concern for invoker's runtime environment and allows multiple collections to be specified –Easier to switch to / from JDBC and SQLJ © 2004 IBM Corporation
  • 166. Advanced Technical Support (ATS), Americas Package Resolution today Given that multiple collections can be used for packages, how is package resolution managed today?... – CURRENT PACKAGESET special register Set to single collection id to indicate any package to be invoked belongs to that collection Application must issue SET CURRENT PACKAGESET before each package is invoked if collection for the package is different from previous package –BIND PLAN PKLIST Ability to specify list of collection ids for packages for local OS/390 applications that use plans at execution time © 2004 IBM Corporation
  • 167. Advanced Technical Support (ATS), Americas Package Resolution - SQLJ What about SQLJ?... –Requirement SQLJ increases need for package switching and versioning With SQLJ, people bind multiple packages for the same thing each with different bind options (i.e., different ISOs) People want to be able to switch between different versions of package depending on what has been implemented at server (i.e., different qualifiers) –Before Version 8 Application does not know which one to use so has to issue multiple SET CURRENT PACKAGE SETS and retry until it finds the right one -- laborious and time consuming –With Version 8 New CURRENT PACKAGE PATH special register allows list of package collections to be specified, which results in reduction in network traffic and CPU where applications use DB2 for z/OS as requester Also helps with stored procedures and UDF's and with versioning To execute statement, requester must specify new DRDA flow Special register is 4K (4096), separate collections with commas -- no checks are made until runtime so ensure they are spelled correctly © 2004 IBM Corporation
  • 168. Advanced Technical Support (ATS), Americas Package Resolution - DRDA What about remote clients connected via DRDA... Before Version 8 PKLIST bind option allows them to search package collections at runtime –PKLIST is a requester function -- remote server doesn't know value set for PKLIST because it is part of PLAN and only packages are used at remote server –Message sent to server requesting package with first collection from PKLIST; if results in SQLCODE -805, another message sent requesting package with next collection in PKLIST –With Version 8 New CURRENT PACKAGE PATH special register means crossing network only once to resolve package collection id at server, instead of crossing once per collection id to perform resolution at client © 2004 IBM Corporation
  • 169. Advanced Technical Support (ATS), Americas Package Resolution - procedures What about procedures and user-defined functions? –Before Version 8 Users cannot identify package resolution schemes independent from rules established by caller's plan SET CURRENT PACKAGESET or COLLID option for routine can be used within a procedure or function to change package resolution rule from invokers, but can only specify one package collection at a time –With Version 8 New CURRENT PACKAGE PATH special register allows nested procedure, user-defined function to be implemented without concern for invoker's runtime environment and allows multiple collections to be specified © 2004 IBM Corporation
  • 170. Advanced Technical Support (ATS), Americas Package resolution today -- local application Local application (CICS, IMS, TSO, batch, etc.) with static SQL PROGA DB2 BIND PLAN plana PKLIST ... Plan / Package ( MAINT_COLLECT.*, SELECT col1 ... PROD_COLLECT.*, ... TEST_COLLECT.* ) MAINT_COLLECT.PROGA PROD_COLLECT.PROGA DB2 package TEST_COLLECT.PROGA search © 2004 IBM Corporation
  • 171. Advanced Technical Support (ATS), Americas Package resolution today -- remote DRDA server DRDA client DB2 for z/OS server SET CURRENT PACKAGESET (through DDF) -- no plan with at server PKLIST PROGA DB2 SET CURRENT PACKAGESET = 'MAINT_COLLECT' Search for SELECT col1 . . . MAINT_COLLECT.PROGA -805 (not found) SET CURRENT PACKAGESET = 'PROD_COLLECT' Search for PROD_COLLECT.PROGA SELECT col1 . . . (not found) etc. . . . -805 © 2004 IBM Corporation
  • 172. Advanced Technical Support (ATS), Americas Package resolution today -- remote DRDA server DB2 for z/OS client DB2 for z/OS server plan with PKLIST for accessing (through DDF) -- no plan with local packages PKLIST PROGA DB2 SET CURRENT PACKAGESET = 'MAINT_COLLECT' Search for SELECT col1 . . . MAINT_COLLECT.PROGA -805 (not found) SET CURRENT PACKAGESET = 'PROD_COLLECT' Search for PROD_COLLECT.PROGA SELECT col1 . . . (not found) etc. . . . -805 © 2004 IBM Corporation
  • 173. Advanced Technical Support (ATS), Americas Package resolution with CURRENT PACKAGE PATH DRDA client or DB2 for z/OS client DB2 for z/OS server plan with PKLIST for accessing (through DDF) -- no plan with local packages PKLIST PROGA DB2 SET CURRENT PACKAGE MAINT_COLLECT.PROGA PATH = "MAINT_COLLECT", "PROD_COLLECT", "TEST_COLLECT" PROD_COLLECT.PROGA SELECT col1 . . . TEST_COLLECT.PROGA etc. . . . © 2004 IBM Corporation
  • 174. Advanced Technical Support (ATS), Americas Common Table Expressions and Recursive SQL © 2004 IBM Corporation
  • 175. Advanced Technical Support (ATS), Americas Common Table Expression (CTE) and Recursion Refer to table-name later in SQL Allow in SELECT, CREATE VIEW, INSERT WITH table-name [ column-list ] AS fullselect For recursion –fullselect that does not refer to CTE UNION ALL fullselect that refers to CTE –fullselect of CTE refers to itself © 2004 IBM Corporation
  • 176. Advanced Technical Support (ATS), Americas Common Table Expression example WITH DTOTAL (deptno, totalpay) AS ( SELECT deptno, sum (salary + bonus) FROM employee GROUP BY deptno ) SELECT deptno FROM DTOTAL WHERE totalpay = (SELECT max ( totalpay ) FROM DTOTAL ) © 2004 IBM Corporation
  • 177. Advanced Technical Support (ATS), Americas Recursive SQL Example AAA WITH PARENT ( PKEY, CKEY ) AS ( SELECT PKEY, CKEY BBB CCC DDD FROM HIERARCHY WHERE PKEY = 'AAA' EEE FFF UNION ALL SELECT C.PKEY, C.CKEY GGG FROM HIERARCHY C, PARENT P WHERE P.CKEY = C.PKEY ) PKEY CKEY AAA BBB SELECT PKEY, CKEY AAA CCC FROM PARENT; AAA DDD CCC EEE DDD EEE DDD FFF FFF GGG © 2004 IBM Corporation
  • 178. Advanced Technical Support (ATS), Americas Summary le u ltip NC M TI Mu l DI S DI T ti- AG GE INS INS row NO T T w ER ERT ST SE /in IC LE Indentity S T C a l ar c enhancement Sc sele Dy ll s fu t scr man Multi-row olla ic ble Se FETCH qu enc T R EN es C UR AGE CK PA H P AT Recursive SQL DB2 V8 © 2004 IBM Corporation
  • 179. Advanced Technical Support (ATS), Americas SG24-6079 SG24-6079 © 2004 IBM Corporation