An index stores column values in a sorted order. A functional index transforms the data in a column and stores the derived values in sorted order.

Read more about functional indexes here.

1 Benefits of a functional index

An index stores column values in a sorted order. A functional index transforms the data in a column and stores the derived values in sorted order.

Suppose a table stores the names of employees in an organization, and the case of the names needs to be preserved. Then a query that requires a case-insensitive search, like the one below, must convert the data:

Copy
SELECT * FROM t1 WHERE toUpper(name) LIKE 'ANTHONY % HOPKINS';

If there is no index on name, the DBMS does a full table scan and applies the toUpper function on the name column for each tuple. Invoking the toUpper function is required in order to determine whether or not the tuple satisfies the query. Performance is negatively impacted when the table is large or when many sessions issue this type of query.

One way to eliminate the toUpper function is to store both the mixed case and the upper case names in the table. The application queries the case-insensitive column:

Copy
SELECT * FROM t1 WHERE ucname like 'ANTHONY % HOPKINS';

If there is no index on ucname, the DBMS still does a full table scan, but it need not do any further processing on the data to determine whether or not it satisfies the query. Though this improves performance, it is not an optimal solution because the table is larger and all applications that manipulate or access the data must include logic that handles ucname.

A better way to improve the performance of this query is to create a functional index on name:

Copy
CREATE FUNCTION toUpper( name VARCHER(100) ) RETURNS VARCHAR(100)
    WITH (NOT VARIANT);
 
    RETURN upper( name );
END FUNCTION;
 
CREATE INDEX ucnameIndex ON t1 ( toUpper(name) );

Now when a query like this is executed, the DBMS can use the functional index ucnameIndex to determine which tuples satisfy the query. The DBMS fetches and returns only those tuples, as in the listing below:

Copy
SELECT * FROM t1 WHERE toUpper(name) LIKE 'ANTHONY % HOPKINS';

The DBMS automatically manages the functional index and applications need not include logic to manage the upper case data. The DBMS ensures that the index is always consistent with the data in the table by updating it during INSERT, UPDATE, and DELETE operations.