Oracle INDEX Hints

In Oracle, INDEX hints are optimizer hints used to influence the Cost-Based Optimizer (CBO) to access a table using a particular index.

Normally, Oracle automatically decides whether to use an index or perform a Full Table Scan. Using an INDEX hint, we can request a specific index access path.

Let's understand the different INDEX hints with practical examples.

1. INDEX Hint

INDEX ACCESS

The INDEX hint requests Oracle to use a specified index.

Example

First, create an index:

CREATE INDEX idx_dept_id
ON employees(department_id);

Execute the query using the INDEX hint:

SELECT /*+ INDEX(e idx_dept_id) */
       *
FROM employees e
WHERE department_id = 10;

Here:

  • e = Table alias.
  • idx_dept_id = Index name.
  • INDEX = Optimizer hint.

Expected execution plan:

SELECT STATEMENT
  TABLE ACCESS BY INDEX ROWID EMPLOYEES
    INDEX RANGE SCAN IDX_DEPT_ID
Note: The INDEX hint requests index access. The actual index operation depends on the query and index structure.

2. INDEX_FFS Hint (Index Fast Full Scan)

INDEX FAST FULL SCAN

The INDEX_FFS hint requests Oracle to perform an Index Fast Full Scan.

It reads the index blocks without following the index's logical order.

Example

SELECT /*+ INDEX_FFS(e idx_dept_id) */
       department_id
FROM employees e;

Expected execution plan:

SELECT STATEMENT
  INDEX FAST FULL SCAN IDX_DEPT_ID

When is INDEX_FFS useful?

It can be useful when all required columns are available in the index and Oracle can retrieve the result directly from the index without accessing the table.

Important: An Index Fast Full Scan does not guarantee sorted output. Use ORDER BY when a specific result order is required.

3. INDEX_SS Hint (Index Skip Scan)

INDEX SKIP SCAN

The INDEX_SS hint requests an Index Skip Scan.

This is useful when a query does not specify the leading column of a composite index.

Example

Create a composite index:

CREATE INDEX idx_dept_salary
ON employees(department_id, salary);

Execute the query:

SELECT /*+ INDEX_SS(e idx_dept_salary) */
       *
FROM employees e
WHERE salary = 50000;

Here, the leading column department_id is missing from the WHERE condition.

Oracle may use an Index Skip Scan to access the index.

Expected execution plan:

SELECT STATEMENT
  TABLE ACCESS BY INDEX ROWID EMPLOYEES
    INDEX SKIP SCAN IDX_DEPT_SALARY
Remember: Index Skip Scan can be useful when the leading column has relatively few distinct values. Oracle considers the data distribution and estimated cost before choosing this access path.

4. INDEX_DESC Hint (Descending Index Scan)

DESCENDING INDEX ACCESS

The INDEX_DESC hint requests Oracle to scan an index in descending order.

Example

Create an index:

CREATE INDEX idx_emp_id
ON employees(emp_id);

Execute the query:

SELECT /*+ INDEX_DESC(e idx_emp_id) */
       *
FROM employees e
WHERE emp_id > 100;

Expected execution plan may contain:

INDEX RANGE SCAN DESCENDING IDX_EMP_ID

This hint requests descending index access.

5. NO_INDEX Hint

AVOID SPECIFIED INDEX

The NO_INDEX hint requests Oracle not to use a specified index.

Example

SELECT /*+ NO_INDEX(e idx_dept_id) */
       *
FROM employees e
WHERE department_id = 10;

Oracle is requested to avoid idx_dept_id.

It may choose a Full Table Scan or another suitable access path.

Note: NO_INDEX does not necessarily force a Full Table Scan. It only requests that Oracle avoid the specified index.

6. FULL Hint

FULL TABLE SCAN

The FULL hint requests Oracle to perform a Full Table Scan.

Example

SELECT /*+ FULL(e) */
       *
FROM employees e
WHERE department_id = 10;

Expected execution plan:

SELECT STATEMENT
  TABLE ACCESS FULL EMPLOYEES

The FULL hint requests Oracle to read the table using a Full Table Scan.

7. Summary of Oracle INDEX Hints

Hint Purpose
INDEX Requests a particular index.
INDEX_FFS Requests Index Fast Full Scan.
INDEX_SS Requests Index Skip Scan.
INDEX_DESC Requests descending index access.
NO_INDEX Requests avoiding a particular index.
FULL Requests Full Table Scan.

8. How to Verify Whether the Hint Worked?

We can verify whether Oracle used the requested index using EXPLAIN PLAN.

Step 1: Execute EXPLAIN PLAN

EXPLAIN PLAN FOR
SELECT /*+ INDEX(e idx_dept_id) */
       *
FROM employees e
WHERE department_id = 10;

Step 2: Display the Execution Plan

SELECT *
FROM TABLE(DBMS_XPLAN.DISPLAY);

Look for the following operation:

INDEX RANGE SCAN IDX_DEPT_ID

Step 3: Check the Actual Execution Plan

You can also inspect the actual execution plan using DBMS_XPLAN.DISPLAY_CURSOR.

SELECT *
FROM TABLE(
    DBMS_XPLAN.DISPLAY_CURSOR(
        NULL,
        NULL,
        'ALLSTATS LAST'
    )
);

For runtime statistics, execute the query with the appropriate statistics collection.

SELECT /*+ GATHER_PLAN_STATISTICS
           INDEX(e idx_dept_id) */
       *
FROM employees e
WHERE department_id = 10;

You can also check the execution plan's hint report to identify hints that were used or ignored.

9. Important Oracle Interview Questions

Q1. What is the difference between INDEX and INDEX_FFS?

INDEX requests an index access path, such as an Index Range Scan.

INDEX_FFS requests reading the index blocks using a Fast Full Scan.

Q2. What is the difference between INDEX and INDEX_SS?

INDEX requests index access.

INDEX_SS specifically requests an Index Skip Scan, which can be useful when the leading column of a composite index is not specified.

Q3. Can Oracle ignore an INDEX hint?

Yes. Oracle can ignore hints that are invalid, conflicting, unresolved, or inapplicable.

Q4. Can we use multiple INDEX hints in one query?

Yes. We can specify INDEX hints for multiple table aliases in the same SQL statement.

Example:

SELECT /*+ INDEX(e idx_dept_id)
           INDEX(d idx_dept_name) */
       *
FROM employees e
JOIN departments d
ON e.department_id = d.department_id;

Here, we request index access for both table aliases:

  • e = Employees table alias.
  • idx_dept_id = Employees index.
  • d = Departments table alias.
  • idx_dept_name = Departments index.

10. Interview Takeaway

Remember These Five Important Oracle INDEX Hints

  1. INDEX – Request index access.
  2. INDEX_FFS – Request Index Fast Full Scan.
  3. INDEX_SS – Request Index Skip Scan.
  4. INDEX_DESC – Request descending index access.
  5. NO_INDEX – Request avoiding a particular index.

Conclusion

Oracle provides different optimizer hints to influence index access paths and SQL execution plans.

Understanding INDEX, INDEX_FFS, INDEX_SS, INDEX_DESC, NO_INDEX, and FULL hints is important for Oracle SQL developers and database administrators.

Important: INDEX hints should be used carefully. An index access path is not always faster than a Full Table Scan. Always compare actual execution plans and performance statistics before forcing an index.

Happy Learning!

No comments:

Post a Comment