Faster FETCH

Download Faster FETCH

Post on 17-Dec-2014

1.335 views

Category:

Documents

2 download

Embed Size (px)

DESCRIPTION

 

TRANSCRIPT

<ul><li> 1. DB2 ATS (Advanced Technical Support) Faster FETCH &amp; INSERT V8 Multiple Row Processing Christopher J. Crone Updated and presented by John Iczkovits March 13, 2006 Heart of America RUG 2006 IBM Corporation </li> <li> 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 </li> <li> 3. DB2 ATS (Advanced Technical Support) Host Variable Arrays 2006 IBM Corporation </li> <li> 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 </li> <li> 5. DB2 ATS (Advanced Technical Support) COBOL 5 2006 IBM Corporation </li> <li> 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 </li> <li> 7. DB2 ATS (Advanced Technical Support) PL/I 7 2006 IBM Corporation </li> <li> 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 </li> <li> 9. DB2 ATS (Advanced Technical Support) C/C++ 9 2006 IBM Corporation </li> <li> 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 </li> <li> 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, &amp;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 </li> <li> 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 &lt; updateCount.length; i++) actualResults.println(UpdateCount[" + i + "] = " + updateCount[i]); stmt.clearBatch(); con.commit(); stmt.close(); } 12 2006 IBM Corporation </li> <li> 13. DB2 ATS (Advanced Technical Support) Multiple-Row Insert 2006 IBM Corporation </li> <li> 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 </li> <li> 15. DB2 ATS (Advanced Technical Support) Multiple-Row Insert Changed Statements INSERT PREPARE EXECUTE 15 2006 IBM Corporation </li> <li> 16. DB2 ATS (Advanced Technical Support) INSERT 16 2006 IBM Corporation </li> <li> 17. DB2 ATS (Advanced Technical Support) INSERT (cont) 17 2006 IBM Corporation </li> <li> 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 </li> <li> 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 </li> <li> 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 </li> <li> 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 </li> <li> 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...</li></ul>