Database application design tips: Reduce the number of open operations

The SQL data manipulation language statements must do database open operations in order to create an open data path (ODP) to the data. An open data path is the path through which all input/output operations for the table are performed. In a sense, it connects the SQL application to a table. The number of open operations in a program can significantly affect performance. A database open operation occurs on:

An INSERT statement with a select-statement requires two open operations. Certain forms of subqueries may also require one open per subselect.

To minimize the number of opens, DB2 UDB for AS/400 leaves the open data path (ODP) open and reuses the ODP if the statement is run again, unless:

DB2 UDB for AS/400 only reuses ODPs opened by the same statement. An identical statement coded later in the program does not reuse an ODP from any other statement. If the identical statement must be run in the program many times, code it once in a subroutine and call the subroutine to run the statement.

The ODPs opened by DB2 UDB for AS/400 are closed when any of the following occurs:

You can control whether DB2 UDB for AS/400 keeps the ODPs open in the following ways:

DB2 UDB for AS/400 does an open operation for the first execution of each UPDATE WHERE CURRENT OF when any expression in the SET clause contains an operator or function. The open can be avoided by coding the function or operation in the host language code.

For example, the following UPDATE causes DB2 UDB for AS/400 to do an open operation:

EXEC SQL
 FETCH EMPT INTO :SALARY
END-EXEC.
 
EXEC SQL
 UPDATE CORPDATA.EMPLOYEE
   SET SALARY = :SALARY + 1000
   WHERE CURRENT OF EMPT
END-EXEC.
 

Instead, use the following coding technique to avoid opens:

EXEC SQL
 FETCH EMPT  INTO  :SALARY
END EXEC.
 
ADD 1000 TO SALARY.
 
EXEC SQL
 UPDATE CORPDATA.EMPLOYEE
   SET SALARY = :SALARY
   WHERE CURRENT OF EMPT
END-EXEC.

You can use the CL commands Trace Job (TRCJOB) or Display Journal (DSPJRN) to determine the number of opens that are being performed by an SQL statement.


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]