Skip to main content

Function-Based Index in Oracle Database

Function-Based Index in Oracle Database

A function-based index is an index created on the result of a function or expression applied to one or more columns of a table. This allows the Oracle optimizer to use the index for queries that involve expressions or functions, improving query performance when such expressions are frequently used.

Why Use Function-Based Indexes?

  • Optimize Complex Queries: When queries frequently use functions or expressions on columns, a function-based index can speed up these queries.
  • Support Case-Insensitive Searches: You can create an index to support case-insensitive searches.
  • Efficient Use of Virtual Columns: You can index virtual columns that are derived from other columns.

Creating Function-Based Indexes

Syntax


CREATE INDEX index_name ON table_name (function_or_expression);

Example Scenarios

Scenario 1: Case-Insensitive Search

Assume you have a table EMPLOYEES with a column LAST_NAME. You frequently perform case-insensitive searches on LAST_NAME.

Step 1: Create the Function-Based Index


CREATE INDEX idx_last_name_upper ON EMPLOYEES (UPPER(LAST_NAME));

Step 2: Use the Index in a Query


SELECT * FROM EMPLOYEES WHERE UPPER(LAST_NAME) = 'SMITH';

Explanation

  • Index Name: idx_last_name_upper is the name of the index.
  • Table Name: EMPLOYEES is the table.
  • Expression: UPPER(LAST_NAME) is the expression applied to the LAST_NAME column.

The optimizer can use idx_last_name_upper to quickly locate rows where the uppercase version of LAST_NAME matches 'SMITH'.

Scenario 2: Expression-Based Index

Assume you have a table ORDERS with columns QUANTITY and UNIT_PRICE, and you frequently query on the total price (QUANTITY * UNIT_PRICE).

Step 1: Create the Function-Based Index


CREATE INDEX idx_total_price ON ORDERS (QUANTITY * UNIT_PRICE);

Step 2: Use the Index in a Query


SELECT * FROM ORDERS WHERE QUANTITY * UNIT_PRICE > 1000;

Explanation

  • Index Name: idx_total_price is the name of the index.
  • Table Name: ORDERS is the table.
  • Expression: QUANTITY * UNIT_PRICE is the expression applied to the columns.

The optimizer can use idx_total_price to efficiently find rows where the total price is greater than 1000.

Function-Based Index with Custom Function

You can also create function-based indexes using custom functions. Assume you have a function get_discount that calculates a discount based on some logic.

Step 1: Create the Function


CREATE OR REPLACE FUNCTION get_discount(quantity NUMBER, unit_price NUMBER) RETURN NUMBER IS BEGIN RETURN (quantity * unit_price) * 0.1; -- 10% discount END;

Step 2: Create the Function-Based Index


CREATE INDEX idx_discount ON ORDERS (get_discount(QUANTITY, UNIT_PRICE));

Step 3: Use the Index in a Query


SELECT * FROM ORDERS WHERE get_discount(QUANTITY, UNIT_PRICE) > 50;

Using the Index with Virtual Columns

Oracle allows the creation of virtual columns which are expressions based on other columns in the table.

Step 1: Add a Virtual Column


ALTER TABLE ORDERS ADD (total_price AS (QUANTITY * UNIT_PRICE));

Step 2: Create the Index on the Virtual Column


CREATE INDEX idx_total_price_vc ON ORDERS (total_price);

Step 3: Use the Index in a Query


SELECT * FROM ORDERS WHERE total_price > 1000;

Explanation

  • Virtual Column: total_price is a virtual column defined as the product of QUANTITY and UNIT_PRICE.
  • Index: idx_total_price_vc is created on the virtual column.

Verifying and Monitoring Index Usage

Check Index Usage

You can use the EXPLAIN PLAN command to see if the index is being used:


EXPLAIN PLAN FOR SELECT * FROM EMPLOYEES WHERE UPPER(LAST_NAME) = 'SMITH'; SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY);

Monitor Index Usage

You can query the V$SQL and V$SQL_PLAN views to monitor the usage of indexes.








Please do like and subscribe to my youtube channel: https://www.youtube.com/@foalabs If you like this post please follow,share and comment

Comments

Popular posts from this blog

WebLogic migration to OCI using WDT tool

WebLogic migration to OCI using WDT tool Oracle WebLogic Deploy Tool (WDT) is an open-source project designed to simplify and streamline the management of Oracle WebLogic Server domains. With WDT, you can export configuration and application files from one WebLogic Server domain and import them into another, making it a highly effective tool for tasks like migrating on-premises WebLogic configurations to Oracle Cloud. This blog outlines a detailed step-by-step process for using WDT to migrate WebLogic resources and configurations. Supported WLS versions Why Use WDT for Migration? When moving Oracle WebLogic resources from an on-premises environment to Oracle Cloud (or another WebLogic Server), WDT provides an efficient and reliable approach to: Discover and export domain configurations and application binaries. Create reusable models and archives for deployment in a target domain. Key Pre-Requisites Source System: An Oracle WebLogic Server with pre-configured resources such as: Applica...

How to Validate TDE Wallet Password in Oracle Database

How to Validate TDE Wallet Password in Oracle Database Validating the Transparent Data Encryption (TDE) wallet password is crucial, especially when ensuring that the password is correct without using the OPEN or CLOSE commands in the database. This blog post explains a straightforward method to validate the TDE password using the mkstore utility. Steps to Validate TDE Wallet Password Follow these steps to validate the TDE wallet password: Step 1: Copy the Keystore/Wallet File Navigate to your existing TDE wallet directory. Copy only the ewallet.p12 file to a new directory. If a cwallet.sso file exists, do not copy it . The absence of cwallet.sso ensures that the wallet does not use auto-login, forcing the utility to prompt for the password. Step 2: Validate Using mkstore Use the mkstore utility to check the contents of the wallet file. The mkstore utility will prompt you for the TDE wallet password, allowing you to validate its correctness. Command Syntax To display the conten...

How to Create macOS Catalina ISO File

  How to Create macOS Catalina ISO File Please note these steps need to be performed on a Apple Mac System to generate an iso. Steps: 1) Download macOS Catalina From App Store or Apple download 2) Please note once you download it will be in dmg format. 3) Open Terminal in MAC.  from the top-right corner open up Spotlight.  Type in Terminal and hit Enter. 4)  Mount it to your macOS hdiutil attach /tmp/Catalina.dmg -noverify -mountpoint /Volumes/Catalina 5) Create macOS Catalina Installer sudo /Applications/Install\ macOS\ Catalina.app/Contents/Resources/createinstallmedia --volume /Volumes/Catalina --nointeraction 6) Unmount Catalina Disk hdiutil detach /volumes/Install\ macOS\ Catalina 7) Convert the DMG file to an ISO file hdiutil convert /tmp/Catalina.dmg -format UDTO -o ~/Desktop/Catalina.cdr 8) Rename and Move to Desktop mv ~/Desktop/Catalina.cdr ~/Desktop/Catalina.iso Now we can use this iso on any system and install on virtual machines.