First_rows Hint in Oracle

Posted on 03 March 2008 by Praveen

The FIRST_ROWS hint directs the optimizer to choose the best possible way of executing the query on the basis of retrieving the first rows the fastest. This approach is mainly suitable for Online transaction processing systems to retrieve a single record on to the screen. This approach would be a bad choice for datawarehousing application systems since  a lot of rows are retrieved generally by a query.

1. The FIRST_ROWS HINT instructs the optimizer to use the indexes in highest Possible way.

2. The First_rows Hint is ignored  in UPDATE AND DELETE  statements(subqueies), Since all rows of the query must be updated or deleted.

3. First_rows hint is  ignored  if your query consists of GROUP BY, DISTINCT, INTERSECT, MINUS, UNION clauses since these all clauses require all  rows before returning the  first result.

4. First_rows hint also instructs the optimizer to use Nested loops joins instead of Sort merge joins or HASH joins Since using of  nested loops  would result in  lowest response time.

From Oracle 9i Onwards you can also optimize the query not only for the first row but also for the first 10 rows, first 100 rows and first 1000 rows.
FIRST_ROWS ORACLE HINT SYNTAX

select /*+ FIRST_ROWS(n) */ column1, …

First_rows hint example

select /*+ FIRST_ROWS */ empno, deptno
from emp
where deptno = 10;

First 10 rows hint example

select /*+ FIRST_ROWS(10) */ empno, deptno
from emp
where deptno = 10;

Leave a Reply