faster fetch

Download Faster FETCH

Post on 17-Dec-2014

1.336 views

Category:

Documents

2 download

Embed Size (px)

DESCRIPTION

 

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)