Enabling block fetch for distributed applications
Block fetch can significantly decrease the number of messages sent across the network.
About this task
With block
fetch, Db2 groups
the rows that are retrieved by an SQL query into as large a block
of
rows as can fit in a message buffer. Db2 then
transmits the block over the network, without requiring a separate
message for each row.
Db2 can use different types of block fetch:
- Limited block fetch
- Continuous block fetch
To enable limited or continuous block fetch, Db2 must determine that the cursor is not used for updating or deleting. Block fetch is used only with cursors that do not update or delete data.
Db2 triggers block fetch for static SQL only when it can detect that no updates or deletes are in the application. For dynamic statements, because Db2 cannot detect what follows in the program, the decision to use block fetch is based on the declaration of the cursor.
- The cursor is referred to in the statement DELETE WHERE CURRENT OF elsewhere in the program.
- The cursor statement appears that it can be updated at the requesting system. (Db2 does not check whether the cursor references a view at the server that cannot be updated.)
Procedure
To ensure that Db2 uses block fetch:
EXEC SQL
DECLARE THISEMP CURSOR FOR
SELECT EMPNO, LASTNAME, WORKDEPT, JOB
FROM DSN8C10.EMP
WHERE WORKDEPT = 'D11'
FOR FETCH ONLY
END-EXEC.
- The cursor is a non-scrollable cursor, and the result table of the cursor is read-only. This applies to static and dynamic cursors except for read-only views.
- For limited block fetch or SQL-based continuous block fetch only:
- The cursor is a scrollable cursor that is declared as INSENSITIVE, and the result table of the cursor is read-only.
- The cursor is a scrollable cursor that is declared as SENSITIVE, the result table of the cursor is read-only, and the value of bind option CURRENTDATA is NO.
- The result table of the cursor is not read-only, but the cursor
is ambiguous, and the value of bind option CURRENTDATA is NO. A cursor
is ambiguous when:
- It is not defined with the clauses FOR FETCH ONLY, FOR READ ONLY, or FOR UPDATE OF.
- It is not defined on a read-only result table.
- It is not the target of a WHERE CURRENT clause on an SQL UPDATE or DELETE statement.
- It is in a plan or package that contains the SQL statements PREPARE or EXECUTE IMMEDIATE.
Results
The following tables summarize the conditions under which a Db2 server uses block fetch.
Isolation level | CURRENTDATA | Cursor type | Block fetch |
---|---|---|---|
CS, RR, or RS | Yes | Read-only | Yes |
Updatable | No | ||
Ambiguous | No | ||
No | Read-only | Yes | |
Updatable | No | ||
Ambiguous | Yes | ||
UR | Yes | Read-only | Yes |
No | Read-only | Yes |
Isolation level | Cursor sensitivity | CURRENTDATA | Cursor type | Block fetch |
---|---|---|---|---|
CS, RR, or RS | INSENSITIVE | Yes | Read-only | Yes |
No | Read-only | Yes | ||
SENSITIVE | Yes | Read-only | No | |
Updatable | No | |||
Ambiguous | No | |||
No | Read-only | Yes | ||
Updatable | No | |||
Ambiguous | Yes | |||
UR | INSENSITIVE | Yes | Read-only | Yes |
No | Read-only | Yes | ||
SENSITIVE | Yes | Read-only | Yes | |
No | Read-only | Yes |
Isolation level | Cursor sensitivity | CURRENTDATA | Cursor type | Block fetch |
---|---|---|---|---|
CS, RR, or RS | INSENSITIVE | Yes | Read-only | Yes |
No | Read-only | Yes | ||
SENSITIVE | Yes | Read-only | No | |
No | Read-only | Yes | ||
UR | INSENSITIVE | Yes | Read-only | Yes |
No | Read-only | Yes | ||
SENSITIVE | Yes | Read-only | Yes | |
No | Read-only | Yes |