Faster FETCH
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Faster FETCH

on

  • 1,756 views

 

Statistics

Views

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

Actions

Likes
0
Downloads
48
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

Faster FETCH Presentation Transcript

  • 1. DB2 ATS (Advanced Technical Support) Faster FETCH & INSERT V8 Multiple Row Processing Christopher J. Crone Updated and presented by John Iczkovits March 13, 2006 Heart of America RUG © 2006 IBM Corporation
  • 2. DB2 ATS (Advanced Technical Support) Presentation Topics Host Variable Arrays Multiple-Row Insert Multiple-Row Fetch DELETE WHERE CURRENT OF UPDATE WHERE CURRENT OF 2 © 2006 IBM Corporation
  • 3. DB2 ATS (Advanced Technical Support) Host Variable Arrays © 2006 IBM Corporation
  • 4. DB2 ATS (Advanced Technical Support) 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 4 © 2006 IBM Corporation
  • 5. DB2 ATS (Advanced Technical Support) COBOL 5 © 2006 IBM Corporation
  • 6. DB2 ATS (Advanced Technical Support) 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. 6 © 2006 IBM Corporation
  • 7. DB2 ATS (Advanced Technical Support) PL/I 7 © 2006 IBM Corporation
  • 8. DB2 ATS (Advanced Technical Support) 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; 8 © 2006 IBM Corporation
  • 9. DB2 ATS (Advanced Technical Support) C/C++ 9 © 2006 IBM Corporation
  • 10. DB2 ATS (Advanced Technical Support) 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; 10 © 2006 IBM Corporation
  • 11. DB2 ATS (Advanced Technical Support) ODBC #define TC 10; 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 */ 11 © 2006 IBM Corporation
  • 12. DB2 ATS (Advanced Technical Support) 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(); } 12 © 2006 IBM Corporation
  • 13. DB2 ATS (Advanced Technical Support) Multiple-Row Insert © 2006 IBM Corporation
  • 14. DB2 ATS (Advanced Technical Support) 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 14 © 2006 IBM Corporation
  • 15. DB2 ATS (Advanced Technical Support) Multiple-Row Insert Changed Statements ƒINSERT ƒPREPARE ƒEXECUTE 15 © 2006 IBM Corporation
  • 16. DB2 ATS (Advanced Technical Support) INSERT 16 © 2006 IBM Corporation
  • 17. DB2 ATS (Advanced Technical Support) INSERT (cont) 17 © 2006 IBM Corporation
  • 18. DB2 ATS (Advanced Technical Support) 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 18 © 2006 IBM Corporation
  • 19. DB2 ATS (Advanced Technical Support) 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 19 © 2006 IBM Corporation
  • 20. DB2 ATS (Advanced Technical Support) 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; 20 © 2006 IBM Corporation
  • 21. DB2 ATS (Advanced Technical Support) INSERT Example 4 Assume that table T3 has three columns: S1 SMALLINT, I1 INTEGER, and M1 CHAR(8) The application allocates one array, :hva2 with 10 elements. :hv1 is a scalar host variable and the value for M1 is a special register In this example, hv1, and CURRENT TIME are used for each row of data, the values for C2 are obtained from host-variable-array :hva2 S1 I1 T1 5 1 13:30:05 :hv1 :hva2 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 5 4 13:30:05 TIME = ’ 13:30:05’ 5 5 5 13:30:05 6 7 5 6 13:30:05 Result of Insert 8 5 7 13:30:05 9 10 5 8 13:30:05 5 9 13:30:05 21 5 10 © 2006 IBM Corporation 13:30:05
  • 22. DB2 ATS (Advanced Technical Support) 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’ 22 © 2006 IBM Corporation
  • 23. DB2 ATS (Advanced Technical Support) Distributed Multiple Row INSERT Flow Application Client Server Intermediate Table C1 C2 Com-Buffer Com-Buffer Buffer Page Distributed Chained INSERT Flow Server Intermediate Table Application Client Com-Buffer Buffer Page Stmt Data Com-Buffer 23 © 2006 IBM Corporation
  • 24. DB2 ATS (Advanced Technical Support) 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 24 © 2006 IBM Corporation
  • 25. DB2 ATS (Advanced Technical Support) 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 25 © 2006 IBM Corporation
  • 26. DB2 ATS (Advanced Technical Support) 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. 26 © 2006 IBM Corporation
  • 27. DB2 ATS (Advanced Technical Support) PREPARE 27 © 2006 IBM Corporation
  • 28. DB2 ATS (Advanced Technical Support) EXECUTE 28 © 2006 IBM Corporation
  • 29. DB2 ATS (Advanced Technical Support) 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. 29 © 2006 IBM Corporation
  • 30. DB2 ATS (Advanced Technical Support) 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. 30 © 2006 IBM Corporation
  • 31. DB2 ATS (Advanced Technical Support) 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 is 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 31 © 2006 IBM Corporation
  • 32. DB2 ATS (Advanced Technical Support) Multiple-Row Fetch © 2006 IBM Corporation
  • 33. DB2 ATS (Advanced Technical Support) Multiple-Row Fetch Changed Statements ƒDECLARE CURSOR ƒOPEN CURSOR ƒALLOCATE CURSOR ƒFETCH ƒPositioned UPDATE (UWCO) ƒPositioned DELETE (DWCO) 33 © 2006 IBM Corporation
  • 34. DB2 ATS (Advanced Technical Support) DECLARE CURSOR 34 © 2006 IBM Corporation
  • 35. DB2 ATS (Advanced Technical Support) 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 35 © 2006 IBM Corporation
  • 36. DB2 ATS (Advanced Technical Support) FETCH 36 © 2006 IBM Corporation
  • 37. DB2 ATS (Advanced Technical Support) FETCH (cont) 37 © 2006 IBM Corporation
  • 38. DB2 ATS (Advanced Technical Support) Rowsets A group of rows for the result table of a query which are returned by a single FETCH statement (or inserted by a single (multi-row) INSERT statement) Program controls how many rows are returned (i.e., size of the rowset) ƒCan be specified on the FETCH statement (maximum rowset size is 32767) Each group of rows are operated on as a rowset Ability to intermix row positioned (single row) and rowset (multiple row) positioned fetches when a cursor is declared WITH ROWSET POSITIONING FETCH FIRST ROWSET STARTING AT ABSOLUTE 10 FROM CURS1 FOR 6 ROWS INTO :hva1, :hva2; 38 © 2006 IBM Corporation
  • 39. DB2 ATS (Advanced Technical Support) 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 39 © 2006 IBM Corporation
  • 40. DB2 ATS (Advanced Technical Support) 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 40 © 2006 IBM Corporation
  • 41. DB2 ATS (Advanced Technical Support) Partial Rowsets (Result Sets) 41 © 2006 IBM Corporation
  • 42. DB2 ATS (Advanced Technical Support) Cursor Positioning: Mixing Row and Rowset Positioned Fetches - Multi Row Fetch (V8) 42 © 2006 IBM Corporation
  • 43. DB2 ATS (Advanced Technical Support) 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 43 © 2006 IBM Corporation
  • 44. DB2 ATS (Advanced Technical Support) 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... 44 © 2006 IBM Corporation
  • 45. DB2 ATS (Advanced Technical Support) 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 45 © 2006 IBM Corporation
  • 46. DB2 ATS (Advanced Technical Support) 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. 46 © 2006 IBM Corporation
  • 47. DB2 ATS (Advanced Technical Support) 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 47 © 2006 IBM Corporation
  • 48. DB2 ATS (Advanced Technical Support) 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 48 © 2006 IBM Corporation
  • 49. DB2 ATS (Advanced Technical Support) Local FETCH Flow x1000 x2000 Internal Buffers ‘N’ Table 1204 0 … … 4 1204 1300 0 1300 1700 0 1700 1900 1900 0 SQLDAID = SQLDA SQLDABC = 104 SQLN = 2 SQLD = 2 SQLTYPE[1] = 496 SQLLEN[1] = 4 SQLDATA[1] = x00001000 SQLIND[1] = x00002000 SQLNAME[1] = x‘0008000000010004’ 49 © 2006 IBM Corporation
  • 50. DB2 ATS (Advanced Technical Support) 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 is 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 50 © 2006 IBM Corporation
  • 51. DB2 ATS (Advanced Technical Support) Locking and Isolation Levels Cursor will be positioned on all rows in current rowset Locks will be held on all rows in rowset depending on isolation level and whether a result table has been materialized As normal, these factors affect whether you will see changes made to the table following open cursor Also affects whether you refetch the same rows when issuing a FETCH CURRENT to refetch current rowset 51 © 2006 IBM Corporation
  • 52. DB2 ATS (Advanced Technical Support) Using Static Scrollable Cursors 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 For example, FETCH PRIOR ROWSET may return update or delete holes in place of rows that were fetched before Row contents can change between fetches 52 © 2006 IBM Corporation
  • 53. DB2 ATS (Advanced Technical Support) If using static scrollable cursors... As holes may occur, ensure at least one indicator variable is defined for a row ƒIf no nullable columns exist add an indicator variable for at least one column ƒIf multiple nullable columns exist each indicator variable will be updated if a hole is found New value of -3 indicates hole SQLCODE +222 will also be returned (via GET DIAGNOSTICS) CUSTNO NULLABLE_COL IND_VAR CUST_TYPE 1000 M P 2000 -1 B -3 4000 F P 53 © 2006 IBM Corporation
  • 54. DB2 ATS (Advanced Technical Support) Considerations with Dynamic Scrollable Cursors Locks will be held on base table for entire rowset with ISO CS, RR and RS Starting point and contents of rowsets may change when scrolling back and forth Refetching current rowset may return different rows ƒIf ISO(RR), other applications cannot affect your rowset ƒYour application can still affect your rowset 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 54 © 2006 IBM Corporation
  • 55. DB2 ATS (Advanced Technical Support) Multiple Row FETCH/INSERT -- DRDA Considerations For remote client one rowset returned in a network request – this is in effect the blocksize Updateable cursors will still be blocked. Supported by any requester or server that supports DRDA Level 3 DB2 for z/OS V8 DB2 LUW V8 FP 4 ODBC client Limit of 10 megabyte buffer for distributed MRF/MRI 55 © 2006 IBM Corporation
  • 56. DB2 ATS (Advanced Technical Support) Positioned DELETE Positioned Update 56 © 2006 IBM Corporation
  • 57. DB2 ATS (Advanced Technical Support) 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. 57 © 2006 IBM Corporation
  • 58. DB2 ATS (Advanced Technical Support) Summary Host Variable Arrays Multiple-Row Insert Multiple-Row Fetch DELETE WHERE CURRENT OF UPDATE WHERE CURRENT OF 58 © 2006 IBM Corporation