You can choose whether to optimize query processing to return the first row quickly, or to minimize the cost of returning the complete result set.
The optimization_goal option controls whether the database server optimizes SQL statements for response time (First-row) or for total resource consumption (All-rows).
If the option is set to First-row, the database server chooses an access plan that is intended to reduce the time to fetch the first row of the query result, possibly at the expense of total retrieval time. In particular, the optimizer typically avoids, if possible, access plans that require the materialization of results to reduce the time to return the first row. With this setting, for example, the optimizer favors access plans that use an index to satisfy a query's ORDER BY clause, rather than plans that require an explicit sorting operation.
The optimization goal used by the optimizer for a particular statement is decided using these rules:
If the main query block has a table in the FROM clause with the table hint set to FASTFIRSTROW, then the statement is optimized using the First-row optimization goal.
If the statement has an OPTION clause containing a setting for the optimization_goal option, then the statement is optimized using this setting.
Else, the optimizer uses the current setting of the option optimization_goal option.
Even if the optimization goal is First-row, the optimizer may be unable to find a plan that can quickly return the first row. For example, statements requiring materialization due to the presence of DISTINCT, GROUP BY, or ORDER BY clauses, and for which a relevant index does not exist to provide the necessary order, are optimized with the All-rows goal.
If the option is set to All-rows (the default), the query is optimized to choose an access plan with the minimal estimated total retrieval time. Setting optimization_goal to All-rows may be appropriate for applications that intend to process the entire result set, such as PowerBuilder DataWindow applications.