When the Optimizer Needs Guidance
Oracle’s cost-based optimizer (CBO) does an excellent job choosing execution plans for most queries. But occasionally it makes a suboptimal choice due to stale statistics, unusual data distributions, or complex query structures. Optimizer hints let you influence the plan without restructuring your query. For APEX developers, understanding a few key hints can solve stubborn performance problems in report queries and page processes.
The Most Useful Hints
FIRST_ROWS(n): Optimizes for returning the first n rows quickly rather than optimizing for total throughput. This is ideal for paginated APEX reports where you only need the first page:
SELECT /*+ FIRST_ROWS(25) */
employee_id, first_name, last_name, salary
FROM employees
ORDER BY last_name
FETCH FIRST 25 ROWS ONLY;
INDEX: Forces the optimizer to use a specific index:
SELECT /*+ INDEX(e idx_emp_dept_id) */
employee_id, first_name
FROM employees e
WHERE department_id = :P10_DEPT_ID;
PARALLEL: Enables parallel query execution for large table scans in batch processes:
SELECT /*+ PARALLEL(o, 4) */
order_id, customer_id, total
FROM orders o
WHERE order_date BETWEEN :start_date AND :end_date;
NO_MERGE: Prevents the optimizer from merging a subquery into the outer query, which sometimes produces a better plan for complex views:
SELECT /*+ NO_MERGE(v) */
v.employee_name, v.total_sales
FROM top_performers_view v
WHERE v.region = 'EAST';
Hints Are Not Directives
Despite the name, the optimizer may ignore a hint if it is invalid, if the hinted path is impossible, or if the referenced object does not exist. Always verify that your hint is being followed by checking the execution plan with EXPLAIN PLAN or DBMS_XPLAN.DISPLAY_CURSOR.
When Not to Use Hints
Hints should be a last resort. Before adding a hint, check that table and index statistics are current (DBMS_STATS.GATHER_TABLE_STATS), that the query is well-structured, and that appropriate indexes exist. Hints create a maintenance burden because they lock in a specific plan that may become suboptimal as data volumes or distributions change. Always document why a hint was added so future developers understand the context.