Interaction between FETCH and OPTIMIZE FOR clauses

You can specify FETCH and OPTIMIZE FOR clauses in the same query.

In general, if you specify the FETCH clause but not OPTIMIZE FOR clause in a SELECT statement, Db2 optimizes the query as if you had specified the OPTIMIZE FOR clause (that is, OPTIMIZE FOR is implicit when the FETCH clause is used).

When you specify FETCH FIRST n ROWS ONLY and OPTIMIZE FOR m ROWS, and m is less than n, Db2 optimizes the query for m rows. If m is greater than n, Db2 optimizes the query for n rows.

Suppose that you submit the following SELECT statement:
SELECT * FROM EMP
FETCH FIRST 5 ROWS ONLY
OPTIMIZE FOR 20 ROWS;

Start of changeDb2 optimizes for 5 rows for access path selection.End of change