What is a Function-Based Index in Oracle

A Function-Based Index (FBI) in Oracle is an index created on an expression or function applied to one or more columns of a table.

In simple words, instead of creating an index directly on a column, we create an index on the result of a function applied to that column.

It is mainly used to improve query performance when we frequently use functions in the WHERE clause.

1. Example of a Function-Based Index

Suppose we have an EMPLOYEE table.

EMP_ID EMP_NAME EMAIL
101 Ravi RAVI@gmail.com
102 Kumar KUMAR@gmail.com
103 Priya PRIYA@gmail.com
104 Arun ARUN@gmail.com

Suppose we want to search employees using email addresses without considering uppercase or lowercase letters.

SQL Query:

SELECT *
FROM employee
WHERE UPPER(email) = 'RAVI@GMAIL.COM';

Here, we are using the UPPER() function on the EMAIL column.

Without a suitable index, Oracle may perform a Full Table Scan.

2. Create a Function-Based Index

To improve query performance, create an index on the UPPER(email) expression.

SQL Query:

CREATE INDEX idx_upper_email
ON employee(UPPER(email));

Now execute the query again:

SELECT *
FROM employee
WHERE UPPER(email) = 'RAVI@GMAIL.COM';

Oracle can use the Function-Based Index to locate the matching employee more efficiently.

Important: The query expression should correspond to the indexed expression. Oracle's Cost-Based Optimizer decides whether using the index is more efficient.

3. Another Example Using LOWER() Function

Suppose we want to search employee names without considering uppercase or lowercase letters.

Create an index:

CREATE INDEX idx_lower_name
ON employee(LOWER(emp_name));

Execute the query:

SELECT *
FROM employee
WHERE LOWER(emp_name) = 'ravi';

Oracle can potentially use the Function-Based Index to retrieve the matching employee.

4. Example Using SUBSTR() Function

Suppose we want to search employees based on the first three characters of their names.

Create an index:

CREATE INDEX idx_emp_substr
ON employee(SUBSTR(emp_name, 1, 3));

Execute the query:

SELECT *
FROM employee
WHERE SUBSTR(emp_name, 1, 3) = 'Rav';

Oracle can potentially use this index to find matching employee records.

5. How to Check Function-Based Indexes in Oracle?

We can use the USER_IND_EXPRESSIONS data dictionary view to identify expressions used in Function-Based Indexes.

SQL Query:

SELECT index_name,
       table_name,
       column_expression
FROM user_ind_expressions
WHERE table_name = 'EMPLOYEE';

This query displays the index name, table name and expression used in the Function-Based Index.

6. Difference Between Normal Index and Function-Based Index

Normal Index Function-Based Index
Created directly on a column. Created on a function or expression.
Example: EMAIL Example: UPPER(EMAIL)
Useful for normal column searches. Useful for searches involving functions.
Indexes the original column values. Indexes the evaluated expression values.

7. Advantages of Function-Based Index

Advantages

  1. Improves query performance when functions are used in WHERE conditions.
  2. Supports case-insensitive searches using UPPER() or LOWER().
  3. Can improve performance for expressions such as SUBSTR() and arithmetic calculations.
  4. Can avoid repeatedly evaluating expressions during suitable index access.

Disadvantages

  • Requires additional storage space.
  • INSERT, UPDATE and DELETE operations may become slower.
  • The query expression should match the indexed expression appropriately.
  • Some Function-Based Indexes require appropriate statistics and configuration.
Interview Tip: Do not create Function-Based Indexes unnecessarily. Create them when your application frequently searches using functions or expressions in SQL conditions.

8. Important Oracle Interview Questions

Q1. What is a Function-Based Index?

An index created on a function or expression rather than directly on a column is called a Function-Based Index.

Q2. Why do we use Function-Based Indexes?

We use them to improve query performance when functions are applied to columns in SQL queries.

Q3. Can we create a Function-Based Index using UPPER() and LOWER()?

Yes. Oracle supports Function-Based Indexes using functions such as UPPER() and LOWER().

CREATE INDEX idx_upper_name
ON employee(UPPER(emp_name));

Q4. Can we create a Function-Based Index using multiple columns?

Yes. We can create an index using multiple columns and expressions.

CREATE INDEX idx_emp_function
ON employee(UPPER(emp_name), salary);

Q5. Does Oracle automatically use a Function-Based Index?

No. Oracle's Cost-Based Optimizer decides whether to use the index based on the query, statistics and estimated execution cost.

Interview Answer:

A Function-Based Index in Oracle is an index created on an expression or function applied to a column. It is used to improve query performance when functions such as UPPER(), LOWER() or SUBSTR() are used in SQL queries.

Easy way to remember:
  • Normal Index → Created on a column.
  • Composite Index → Created on multiple columns.
  • Function-Based Index → Created on a function or expression.
Important Interview Point:

If you create an index on UPPER(email), your query should use the corresponding expression UPPER(email) for Oracle to use that index efficiently.

Conclusion

Function-Based Indexes are useful Oracle database objects that help improve SQL query performance when functions are applied to columns.

Understanding how to create and use Function-Based Indexes is important for Oracle SQL Developers and DBAs, especially when working on SQL performance tuning.

Keep Learning Oracle SQL and Database Performance Tuning!

No comments:

Post a Comment