How to Find All Indexes in a Schema in Oracle Database

In Oracle Database, you can find all indexes in a schema using Oracle data dictionary views such as USER_INDEXES, ALL_INDEXES, and DBA_INDEXES.

These views help Oracle DBAs identify index names, associated tables, index types, uniqueness, and index status.

In this tutorial, we will learn how to find all indexes in the current schema, a particular schema, and across the entire database with practical SQL examples.

1. Find All Indexes Using USER_INDEXES

If you want to find all indexes owned by your current schema, use the USER_INDEXES data dictionary view.

Execute the following query:

SQL
SELECT
    INDEX_NAME,
    TABLE_NAME,
    INDEX_TYPE,
    UNIQUENESS,
    STATUS
FROM USER_INDEXES
ORDER BY TABLE_NAME;

Example Output

INDEX_NAME TABLE_NAME INDEX_TYPE UNIQUENESS STATUS
IDX_EMP_ID EMPLOYEE NORMAL UNIQUE VALID
IDX_EMP_NAME EMPLOYEE NORMAL NONUNIQUE VALID
IDX_DEPT_ID DEPARTMENT NORMAL NONUNIQUE VALID

Important Columns

  • INDEX_NAME: Name of the index.
  • TABLE_NAME: Name of the table associated with the index.
  • INDEX_TYPE: Type of the index, such as NORMAL or BITMAP.
  • UNIQUENESS: Indicates whether the index is UNIQUE or NONUNIQUE.
  • STATUS: Indicates the index status, such as VALID or UNUSABLE.

2. Find Indexes Using ALL_INDEXES

If you want to find indexes belonging to a particular schema or other schemas accessible to your current user, use ALL_INDEXES.

For example, to find all indexes in the HR schema:

SQL
SELECT
    OWNER,
    INDEX_NAME,
    TABLE_NAME,
    INDEX_TYPE,
    UNIQUENESS,
    STATUS
FROM ALL_INDEXES
WHERE OWNER = 'HR'
ORDER BY TABLE_NAME;

Here, HR is the schema name.

Find Indexes for Multiple Schemas

You can also use the IN operator to find indexes belonging to multiple schemas:

SQL
SELECT
    OWNER,
    INDEX_NAME,
    TABLE_NAME,
    INDEX_TYPE,
    UNIQUENESS,
    STATUS
FROM ALL_INDEXES
WHERE OWNER IN ('HR', 'SCOTT', 'OE')
ORDER BY OWNER, TABLE_NAME;
Note: ALL_INDEXES displays indexes accessible to the current user. It does not necessarily display every index in the database.

3. Find All Indexes Using DBA_INDEXES

If you have the necessary DBA privileges and want to find indexes across the entire database, use DBA_INDEXES.

Execute the following query:

SQL
SELECT
    OWNER,
    INDEX_NAME,
    TABLE_NAME,
    INDEX_TYPE,
    UNIQUENESS,
    STATUS
FROM DBA_INDEXES
ORDER BY OWNER, TABLE_NAME;

This query displays indexes belonging to different schemas across the database.

Find Indexes for a Particular Schema Using DBA_INDEXES

SQL
SELECT
    OWNER,
    INDEX_NAME,
    TABLE_NAME,
    INDEX_TYPE,
    UNIQUENESS,
    STATUS
FROM DBA_INDEXES
WHERE OWNER = 'HR'
ORDER BY TABLE_NAME;
Important: Access to DBA_INDEXES requires appropriate privileges. If you do not have access, use USER_INDEXES or ALL_INDEXES depending on your requirement.

4. Find All Indexes on a Particular Table

If you want to find all indexes created on a specific table, use the USER_INDEXES view.

For example, to find all indexes on the EMPLOYEE table:

SQL
SELECT
    INDEX_NAME,
    TABLE_NAME,
    UNIQUENESS,
    STATUS
FROM USER_INDEXES
WHERE TABLE_NAME = 'EMPLOYEE';

If the table belongs to another accessible schema, use ALL_INDEXES:

SQL
SELECT
    OWNER,
    INDEX_NAME,
    TABLE_NAME,
    UNIQUENESS,
    STATUS
FROM ALL_INDEXES
WHERE OWNER = 'HR'
AND TABLE_NAME = 'EMPLOYEE';

5. Difference Between USER_INDEXES, ALL_INDEXES and DBA_INDEXES

View Description
USER_INDEXES Shows indexes owned by the current user.
ALL_INDEXES Shows indexes accessible to the current user.
DBA_INDEXES Shows indexes across the database, subject to appropriate privileges.

6. Oracle Index Interview Question

Question: How do you find all indexes in a particular schema in Oracle?

Answer:

I use the ALL_INDEXES data dictionary view with the OWNER condition to find all indexes belonging to a particular schema.

For example, to find all indexes in the HR schema:

SQL
SELECT *
FROM ALL_INDEXES
WHERE OWNER = 'HR';

7. Important Points to Remember

  • USER_INDEXES: Displays indexes owned by the current schema.
  • ALL_INDEXES: Displays indexes accessible to the current user.
  • DBA_INDEXES: Displays indexes across the database with appropriate privileges.
  • Use the OWNER column to filter indexes by schema.
  • Use the TABLE_NAME column to find indexes on a particular table.
  • Use the STATUS column to identify valid and unusable indexes.
  • Use the UNIQUENESS column to identify unique and nonunique indexes.
DBA Tip: To investigate the columns included in an index, you can also use USER_IND_COLUMNS, ALL_IND_COLUMNS, or DBA_IND_COLUMNS.

Conclusion

Oracle provides several data dictionary views to find indexes in a database.

Depending on your requirement, you can use:

  • USER_INDEXES to find indexes in your current schema.
  • ALL_INDEXES to find indexes accessible to your user.
  • DBA_INDEXES to find indexes across the entire database.

Understanding these dictionary views is very useful for Oracle DBAs, developers, and database performance tuning professionals.


Related Oracle DBA Topics:

  • How to Check Index Usage in Oracle
  • How to Monitor Index Usage
  • Oracle Index Types
  • How to Find Index Columns in Oracle
  • Oracle Index Rebuild
  • Oracle DBA Interview Questions

How to Check Whether an Index Is Being Used in Oracle Database

In Oracle Database, you can check whether an index is being used by SQL queries using different methods.

The two commonly used methods are:

  1. Using V$OBJECT_USAGE / USER_OBJECT_USAGE for index usage monitoring.
  2. Using EXPLAIN PLAN and DBMS_XPLAN.DISPLAY_CURSOR to analyze execution plans.

In this tutorial, we will understand all three methods with practical SQL examples.

1. Check Index Usage Using V$OBJECT_USAGE

Oracle provides the USER_OBJECT_USAGE view to check whether an index has been used during the monitoring period.

Suppose we have an EMPLOYEE table with an index named IDX_EMP_ID.

Step 1: Enable Index Monitoring

Execute the following statement to enable index usage monitoring:

SQL
ALTER INDEX IDX_EMP_ID MONITORING USAGE;

This statement enables monitoring for the specified index.

Step 2: Execute a SQL Query

Run a query that may use the index:

SQL
SELECT *
FROM EMPLOYEE
WHERE EMP_ID = 101;

If Oracle's optimizer chooses the index, its usage will be recorded.

Step 3: Check Whether the Index Is Used

Execute the following query:

SQL
SELECT
    INDEX_NAME,
    TABLE_NAME,
    MONITORING,
    USED
FROM USER_OBJECT_USAGE
WHERE INDEX_NAME = 'IDX_EMP_ID';

Sample Output

INDEX_NAME TABLE_NAME MONITORING USED
IDX_EMP_ID EMPLOYEE YES YES

Understanding the Output

  • MONITORING = YES: Index usage monitoring is enabled.
  • USED = YES: The index was used at least once during the monitoring period.
  • USED = NO: The index was not recorded as used during the monitoring period.
Note: Index monitoring does not show how many times an index was used. It only indicates whether the index was used during the monitoring period.

2. Check Index Usage Using EXPLAIN PLAN

EXPLAIN PLAN is another useful method to check whether Oracle's optimizer plans to use an index.

It displays the estimated execution plan for a SQL statement.

Step 1: Generate the Execution Plan

Execute the following statement:

SQL
EXPLAIN PLAN FOR
SELECT *
FROM EMPLOYEE
WHERE EMP_ID = 101;

Step 2: Display the Execution Plan

Execute:

SQL
SELECT *
FROM TABLE(DBMS_XPLAN.DISPLAY);

Example Execution Plan Output

TEXT OUTPUT
---------------------------------------------------------
| Id | Operation                   | Name       |
---------------------------------------------------------
|  0 | SELECT STATEMENT            |            |
|  1 | TABLE ACCESS BY INDEX ROWID | EMPLOYEE   |
|  2 | INDEX UNIQUE SCAN           | IDX_EMP_ID |
---------------------------------------------------------

Understanding Important Execution Plan Operations

  • INDEX UNIQUE SCAN: Oracle accesses a unique index entry to locate a row.
  • INDEX RANGE SCAN: Oracle scans a range of index entries.
  • INDEX FULL SCAN: Oracle scans all entries in an index.
  • TABLE ACCESS FULL: Oracle reads the table using a full table scan.
  • TABLE ACCESS BY INDEX ROWID: Oracle accesses table rows using ROWIDs obtained from an index.
Interview Tip: If you see INDEX RANGE SCAN or INDEX UNIQUE SCAN in the execution plan, Oracle is accessing the index for that operation.

3. Check the Actual Execution Plan Using DBMS_XPLAN.DISPLAY_CURSOR

EXPLAIN PLAN displays the optimizer's estimated execution plan. To analyze the execution plan of an executed SQL statement, you can use DBMS_XPLAN.DISPLAY_CURSOR.

Step 1: Execute the Query with GATHER_PLAN_STATISTICS

SQL
SELECT /*+ GATHER_PLAN_STATISTICS */
       *
FROM EMPLOYEE
WHERE EMP_ID = 101;

The GATHER_PLAN_STATISTICS hint collects runtime plan statistics for this execution.

Step 2: Display the Actual Execution Plan

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

This displays the execution plan and available runtime statistics for the last executed cursor in your session.

What Should You Check?

  • Operation
  • Name
  • Starts
  • E-Rows (estimated rows)
  • A-Rows (actual rows)
  • Buffers
Important: DBMS_XPLAN.DISPLAY_CURSOR may require appropriate privileges to access cursor information. Make sure you are checking the correct SQL cursor.

4. Difference Between These Methods

Method Purpose
USER_OBJECT_USAGE Checks whether an index was used during monitoring.
EXPLAIN PLAN Displays the optimizer's estimated execution plan.
DBMS_XPLAN.DISPLAY_CURSOR Displays the execution plan of an executed SQL cursor and available runtime statistics.

5. Important Oracle Index Interview Question

Question: How do you check whether an index is being used in Oracle?

Answer:

I use EXPLAIN PLAN and DBMS_XPLAN.DISPLAY_CURSOR to check whether Oracle is performing an index scan or a full table scan.

I can also enable index usage monitoring using:

SQL
ALTER INDEX IDX_EMP_ID MONITORING USAGE;

Then I check the index usage using USER_OBJECT_USAGE:

SQL
SELECT
    INDEX_NAME,
    TABLE_NAME,
    USED
FROM USER_OBJECT_USAGE
WHERE INDEX_NAME = 'IDX_EMP_ID';

6. Important Points to Remember

  1. An index may exist on a table, but Oracle does not always use it.
  2. Oracle's optimizer decides whether to use an index or perform a full table scan.
  3. Table statistics and index statistics influence execution plan decisions.
  4. Index selectivity and query conditions can affect index usage.
  5. EXPLAIN PLAN shows an estimated execution plan, not proof of actual execution.
  6. DBMS_XPLAN.DISPLAY_CURSOR helps analyze the executed SQL cursor.
  7. USER_OBJECT_USAGE helps monitor index usage over a monitoring period.
DBA Tip: Never drop an index only because it shows USED = NO. First analyze application SQL, execution plans, constraints, and overall database workload.

Conclusion

Checking index usage is an important activity for Oracle DBAs and performance tuning professionals.

Oracle provides different methods to identify whether an index is being used:

  • USER_OBJECT_USAGE: Monitors index usage.
  • EXPLAIN PLAN: Displays the estimated execution plan.
  • DBMS_XPLAN.DISPLAY_CURSOR: Analyzes the execution plan of an executed SQL statement.

Understanding these methods will help you troubleshoot SQL performance issues, analyze execution plans, and make better index management decisions.


Related Oracle DBA Topics:

  • Oracle Index Types
  • Oracle Index Monitoring
  • Oracle Execution Plan
  • Oracle Performance Tuning
  • Oracle DBMS_XPLAN
  • Oracle Full Table Scan vs Index Scan

How to Monitor Index Usage in Oracle Database

In Oracle Database, you can monitor index usage using the ALTER INDEX ... MONITORING USAGE statement.

Oracle provides the V$OBJECT_USAGE view to check whether an index is being used by SQL queries.

In this tutorial, we will learn how to enable index monitoring, check index usage, and disable monitoring with practical SQL examples.

1. Enable Index Monitoring

Suppose we have an index named IDX_EMP_ID on the EMPLOYEE table.

Execute the following statement to enable index usage monitoring:

SQL
ALTER INDEX IDX_EMP_ID MONITORING USAGE;

This statement enables usage monitoring for the specified index.

2. Execute SQL Queries

After enabling monitoring, execute SQL queries that may use the index.

For example:

SQL
SELECT *
FROM EMPLOYEE
WHERE EMP_ID = 101;

If Oracle's optimizer chooses the index for this query, its usage will be recorded.

Note: Oracle records index usage based on optimizer decisions. Simply enabling monitoring does not guarantee that every query will use the index.

3. Check Index Usage

Execute the following query to check monitored index usage:

SQL
SELECT *
FROM V$OBJECT_USAGE;

You can also use the following query to check a specific index:

SQL
SELECT
    INDEX_NAME,
    TABLE_NAME,
    MONITORING,
    USED,
    START_MONITORING,
    END_MONITORING
FROM V$OBJECT_USAGE
WHERE INDEX_NAME = 'IDX_EMP_ID';

Sample Output

INDEX_NAME TABLE_NAME MONITORING USED
IDX_EMP_ID EMPLOYEE YES YES

Understanding the Output

  • MONITORING = YES: Index usage monitoring is enabled.
  • USED = YES: The index was used at least once during the monitoring period.
  • USED = NO: The index was not recorded as used during the monitoring period.

4. Disable Index Monitoring

After completing the monitoring activity, you can disable index usage monitoring using the following statement:

SQL
ALTER INDEX IDX_EMP_ID NOMONITORING USAGE;

This disables usage monitoring for the specified index.

5. Complete Example

Below is the complete example to enable monitoring, execute a query, check index usage, and disable monitoring.

SQL
-- Step 1: Enable monitoring
ALTER INDEX IDX_EMP_ID MONITORING USAGE;

-- Step 2: Execute query
SELECT *
FROM EMPLOYEE
WHERE EMP_ID = 101;

-- Step 3: Check index usage
SELECT
    INDEX_NAME,
    TABLE_NAME,
    USED
FROM V$OBJECT_USAGE
WHERE INDEX_NAME = 'IDX_EMP_ID';

-- Step 4: Disable monitoring
ALTER INDEX IDX_EMP_ID NOMONITORING USAGE;

6. Important Oracle Index Monitoring Interview Questions

  1. Which statement enables index usage monitoring?
    ALTER INDEX index_name MONITORING USAGE;
  2. Which view displays index usage information?
    V$OBJECT_USAGE
  3. What does USED = YES mean?
    The index was used at least once during the monitoring period.
  4. How do you disable index monitoring?
    ALTER INDEX index_name NOMONITORING USAGE;
  5. Does index monitoring show how many times an index was used?
    No. It indicates whether the index was used, not the total number of executions.
  6. Can we drop an index if USED = NO?
    No. An index showing USED = NO does not automatically mean it is safe to drop.
Important: Before dropping an unused index, analyze application workloads, SQL execution patterns, constraints, and other database dependencies.

7. Additional Method to Check Index Usage

Oracle also provides execution-plan tools to identify whether a particular SQL statement used an index.

For example, you can use DBMS_XPLAN.DISPLAY_CURSOR to display the execution plan of a SQL statement.

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

Check the execution plan for operations such as:

  • INDEX RANGE SCAN
  • INDEX UNIQUE SCAN
  • INDEX FULL SCAN
  • TABLE ACCESS BY INDEX ROWID
Interview Tip: V$OBJECT_USAGE helps identify whether a monitored index has been used, whereas DBMS_XPLAN.DISPLAY_CURSOR helps analyze the execution plan of a particular SQL statement.

Conclusion

Oracle index usage monitoring is useful for identifying indexes that may not be used by SQL queries.

Using ALTER INDEX ... MONITORING USAGE and V$OBJECT_USAGE, DBAs can monitor index usage and make better index management decisions.

However, always analyze the workload and database dependencies before deciding to drop an unused index.


Related Topics:

  • Oracle Index Types
  • Oracle Execution Plan
  • DBMS_XPLAN.DISPLAY_CURSOR
  • Oracle Index Rebuild
  • Oracle Unused Indexes

How to Rename an Index in Oracle

In Oracle Database, we can rename an existing index using the ALTER INDEX ... RENAME TO statement.

Renaming an index is useful when we want to change an existing index name to a more meaningful name without dropping and recreating the index.

The index continues to work as before, and the underlying table is not affected.

1. Syntax to Rename an Index in Oracle

Use the following SQL statement to rename an existing index:

ALTER INDEX old_index_name
RENAME TO new_index_name;

Here:

  • ALTER INDEX is used to modify an existing index.
  • old_index_name represents the current index name.
  • RENAME TO specifies the new index name.
  • new_index_name represents the updated index name.

2. Example: Rename an Index in Oracle

Suppose we have an EMPLOYEE table with an EMP_ID column and we want to create an index on that column.

Step 1: Create an Index

First, create an index on the EMP_ID column.

CREATE INDEX IDX_EMP_ID
ON EMPLOYEE(EMP_ID);

Now, an index named IDX_EMP_ID is created on the EMPLOYEE table.

Step 2: Rename the Existing Index

Suppose we want to rename the index from IDX_EMP_ID to IDX_EMPLOYEE_ID.

Execute the following command:

ALTER INDEX IDX_EMP_ID
RENAME TO IDX_EMPLOYEE_ID;
Result: The index name is successfully changed from IDX_EMP_ID to IDX_EMPLOYEE_ID.

3. Verify the Renamed Index

After renaming the index, execute the following query to verify the new index name.

SELECT INDEX_NAME,
       TABLE_NAME
FROM USER_INDEXES
WHERE TABLE_NAME = 'EMPLOYEE';

Expected Output:

INDEX_NAME TABLE_NAME
IDX_EMPLOYEE_ID EMPLOYEE

The output confirms that the index has been renamed successfully.

4. Important Points About Renaming an Index

  • Renaming an index does not affect the underlying table.
  • The index continues to work as before.
  • You do not need to drop and recreate the index.
  • The index data and indexed columns remain unchanged.
  • You must have the necessary privileges to alter the index.
  • The new index name must not conflict with an existing object name in the same schema.
Important Note: Renaming an index changes only its name. It does not change the index structure, indexed columns, or tablespace.

5. Important Oracle Interview Questions

Q1. How do you rename an existing index in Oracle?

Use the ALTER INDEX RENAME TO statement.

ALTER INDEX IDX_EMP_ID
RENAME TO IDX_EMPLOYEE_ID;
Q2. Does renaming an index affect the underlying table?

No. Renaming an index does not affect the underlying table.

Q3. Do we need to recreate an index after renaming it?

No. We can rename an existing index directly without dropping and recreating it.

Q4. Does renaming an index change its indexed columns?

No. The indexed columns remain unchanged. Only the index name is modified.

Q5. Which data dictionary view can be used to verify an index name?

We can use USER_INDEXES to check index names and their associated tables in the current schema.

SELECT INDEX_NAME, TABLE_NAME
FROM USER_INDEXES;

6. Rename an Index – Interview Answer

In Oracle Database, we can rename an existing index using the ALTER INDEX ... RENAME TO statement.

Syntax:

ALTER INDEX old_name
RENAME TO new_name;

Example:

ALTER INDEX IDX_EMP_ID
RENAME TO IDX_EMPLOYEE_ID;
Easy Way to Remember:

ALTER INDEX is used to modify an existing index.

RENAME TO is used to change the index name.

Conclusion: Oracle provides a simple way to rename an existing index using the ALTER INDEX RENAME TO command. We do not need to drop and recreate the index, and its existing functionality remains unchanged.


Related Topics: Oracle Index, ALTER INDEX, Rename Index in Oracle, Oracle SQL Commands, Oracle DBA Interview Questions.

How to Rebuild an Index Online in Oracle

In Oracle Database, we use the ALTER INDEX ... REBUILD ONLINE command to rebuild an existing index while allowing users to perform INSERT, UPDATE and DELETE operations on the underlying table during most of the rebuild process.

It is mainly used in production databases where applications cannot be stopped for index maintenance.

1. Example: Rebuild an Index Online

Suppose we have an EMPLOYEE table with the following records:

EMP_ID EMP_NAME SALARY
101Ravi30000
102Kumar40000
103Priya35000
104Arun45000

Step 1: Create an Index

First, create an index on the SALARY column.

CREATE INDEX idx_emp_salary
ON employee(salary);

Step 2: Rebuild the Index Online

Execute the following command to rebuild the existing index online.

ALTER INDEX idx_emp_salary
REBUILD ONLINE;
Result: The index is rebuilt successfully, subject to available space, privileges and Oracle feature restrictions.

Here:

  • ALTER INDEX modifies an existing index.
  • idx_emp_salary is the index name.
  • REBUILD recreates the index structure.
  • ONLINE allows concurrent DML operations during most of the rebuild.

Step 3: Verify the Index Status

Execute the following query to verify the index status:

SELECT index_name,
       table_name,
       status
FROM user_indexes
WHERE index_name = 'IDX_EMP_SALARY';

Expected Output:

INDEX_NAME TABLE_NAME STATUS
IDX_EMP_SALARY EMPLOYEE VALID

2. How Does Online Index Rebuild Work?

Oracle generally performs the following steps during an online index rebuild:

  1. Starts rebuilding the existing index.
  2. Creates a new index structure.
  3. Allows concurrent DML operations during most of the rebuild.
  4. Synchronizes changes made during rebuilding.
  5. Switches to the newly rebuilt index.
Important: Online rebuild is not completely lock-free. Oracle requires brief locks during certain phases, especially during final synchronization and metadata changes.

3. Rebuild Online with Tablespace

We can also rebuild an index in a different tablespace.

ALTER INDEX idx_emp_salary
REBUILD ONLINE
TABLESPACE users;

This rebuilds the index online and places the rebuilt index in the USERS tablespace.

4. Rebuild a Partitioned Index Online

Suppose we have a Local Partitioned Index named IDX_SALES_AMOUNT.

To rebuild one index partition online, execute:

ALTER INDEX idx_sales_amount
REBUILD PARTITION sales_2024
ONLINE;

Here, sales_2024 represents the local index partition name.

5. Difference Between Normal Rebuild and Online Rebuild

Normal Rebuild Online Rebuild
REBUILD REBUILD ONLINE
More restrictive for concurrent DML Allows concurrent DML during most of the operation
May require application coordination Minimizes application interruption
Generally simpler Requires additional resources
Suitable for maintenance windows Useful for production databases

6. Advantages and Disadvantages of Online Index Rebuild

Advantages

  • Allows concurrent INSERT, UPDATE and DELETE operations during most of the rebuild.
  • Minimizes application interruption.
  • Useful for production databases.
  • Helps perform index maintenance without a long period of DML unavailability.

Disadvantages

  • Requires additional temporary space.
  • Consumes CPU and I/O resources.
  • Can increase database workload.
  • Requires brief locking during synchronization.
  • Some index types have restrictions.

7. Important Oracle Interview Questions

Q1. How do you rebuild an index online in Oracle?
ALTER INDEX idx_emp_salary
REBUILD ONLINE;
Q2. What is the main advantage of Online Index Rebuild?
It allows concurrent DML operations on the underlying table during most of the rebuild process.
Q3. Does Online Index Rebuild lock the table?
It requires brief locks during certain phases, but does not prevent all DML operations throughout the rebuild.
Q4. Does Online Rebuild require additional space?
Yes. Oracle needs additional space to construct the new index structure.
Q5. Can we rebuild a Local Partitioned Index online?
Yes, supported local index partitions can be rebuilt online.

8. Online Index Rebuild – Interview Answer

An Online Index Rebuild in Oracle is the process of rebuilding an existing index while allowing concurrent INSERT, UPDATE and DELETE operations on the underlying table during most of the operation.

Syntax:

ALTER INDEX index_name REBUILD ONLINE;

Example:

ALTER INDEX idx_emp_salary REBUILD ONLINE;
Easy Way to Remember:
  • REBUILD → Recreates an existing index.
  • REBUILD ONLINE → Recreates an index while allowing concurrent DML during most of the operation.

Important Interview Point: Online Index Rebuild is mainly used in production databases to perform index maintenance with minimal application interruption.


Conclusion: The ALTER INDEX ... REBUILD ONLINE command is useful for maintaining Oracle indexes while minimizing application downtime. It is especially helpful in production environments where continuous database availability is important.

Related Topics: Oracle Index, Oracle Index Rebuild, ALTER INDEX, Oracle DBA Interview Questions, Oracle Performance Tuning.

How to Rebuild an Index in Oracle

In Oracle Database, we use the ALTER INDEX ... REBUILD statement to rebuild an existing index.

Rebuilding an index means Oracle creates a new index structure using the existing index definition and table data.

It can be useful when:

  • An index becomes unusable.
  • We need to perform index maintenance.
  • An index needs to be rebuilt after certain partition maintenance operations.
  • We want to change index storage attributes.
Simple Definition:
Index Rebuild is an Oracle operation that recreates an existing index structure using the underlying table data.

1. Example: Rebuild a Normal Index

Suppose we have an EMPLOYEE table.

First, create an index:

CREATE INDEX idx_emp_salary
ON employee(salary);

Now rebuild the index:

ALTER INDEX idx_emp_salary REBUILD;
Output: Index rebuilt.

Explanation:

  • ALTER INDEX → Specifies the index to modify.
  • idx_emp_salary → Index name.
  • REBUILD → Rebuilds the existing index.

2. Rebuild an Index Online

Oracle also supports online index rebuilding.

ALTER INDEX idx_emp_salary
REBUILD ONLINE;

What Is the Advantage of ONLINE?

  • Allows concurrent DML operations during most of the rebuild.
  • Reduces application downtime.
  • Useful for production databases.
Note: Online rebuild has restrictions depending on the index type and Oracle version. It also requires additional resources and space.

3. Rebuild an Index with Tablespace

Suppose we want to rebuild an index in a different tablespace.

ALTER INDEX idx_emp_salary
REBUILD TABLESPACE users;

Here, the rebuilt index is placed in the USERS tablespace.

Important:
The target tablespace must have sufficient space and the user must have the required privileges and quota.

4. Rebuild a Partitioned Index

Suppose we have a partitioned index named idx_sales_amount.

Rebuild a Particular Index Partition

ALTER INDEX idx_sales_amount
REBUILD PARTITION sales_2024;

To rebuild a Global Partitioned Index partition, the same syntax can be used with its actual index partition name.

ALTER INDEX idx_sales_global
REBUILD PARTITION idx_p1;
Important: For a Local Partitioned Index, specify the local index partition name, not necessarily the corresponding table partition name.

5. How to Check Index Status?

Before rebuilding, check the index status.

Check Normal Index Status

SELECT index_name,
       table_name,
       status
FROM user_indexes
WHERE table_name = 'EMPLOYEE';

Example Output:

INDEX_NAME TABLE_NAME STATUS
IDX_EMP_SALARY EMPLOYEE VALID

Check Partitioned Index Status

SELECT index_name,
       partition_name,
       status
FROM user_ind_partitions
WHERE index_name = 'IDX_SALES_AMOUNT';

This query displays the status of individual index partitions.

6. Difference Between REBUILD and COALESCE

Oracle provides two operations for index maintenance.

REBUILD COALESCE
Creates a new index structure. Merges adjacent index leaf blocks where possible.
Requires additional space during rebuilding. Generally requires less additional space.
Can move the index to another tablespace. Does not move the index to another tablespace.
Useful for unusable indexes and certain maintenance needs. Useful for reclaiming fragmented leaf-block space.
Can be performed ONLINE in supported cases. Generally an online operation.

Example of COALESCE

ALTER INDEX idx_emp_salary COALESCE;
Remember:
REBUILD creates a new index structure, whereas COALESCE merges adjacent index leaf blocks where possible.

7. Important Oracle Interview Questions

Q1. Which command is used to rebuild an index in Oracle?

ALTER INDEX index_name REBUILD;

This command rebuilds an existing index.

Q2. Can we rebuild an index online?

Yes. Oracle supports online index rebuilding for supported index types.

ALTER INDEX idx_emp_salary
REBUILD ONLINE;

Q3. Why do we rebuild an index?

To recreate an index structure, recover an unusable index and perform index maintenance when appropriate.

Q4. Can we rebuild a partitioned index?

Yes. We can rebuild individual index partitions using the ALTER INDEX REBUILD PARTITION statement.

Q5. What is the difference between REBUILD and COALESCE?

REBUILD creates a new index structure, whereas COALESCE merges adjacent index leaf blocks where possible.

Q6. Does rebuilding an index delete table data?

No. Rebuilding an index does not delete the underlying table data. Oracle recreates the index using the existing table data.

8. Interview Answer – How to Rebuild an Index in Oracle?

To rebuild an index in Oracle, we use the ALTER INDEX ... REBUILD command.

Example:

ALTER INDEX idx_emp_salary REBUILD;

To rebuild an index online:

ALTER INDEX idx_emp_salary REBUILD ONLINE;

Easy way to remember:

  • CREATE INDEX → Creates a new index.
  • ALTER INDEX REBUILD → Recreates an existing index.
  • ALTER INDEX COALESCE → Merges index leaf blocks where possible.
  • DROP INDEX → Removes an index.

Important interview point: Rebuilding an index is not something we should do regularly without checking its condition and performance impact. Oracle automatically maintains indexes during normal DML operations, and rebuilding should be based on a specific maintenance or performance requirement.

Conclusion

Index Rebuilding is an important Oracle Database maintenance operation. It helps recreate index structures, recover unusable indexes and perform certain index maintenance activities.

Oracle provides different options such as REBUILD, REBUILD ONLINE and COALESCE to manage indexes based on database requirements.

Before rebuilding an index in a production database, always check its status, space requirements and performance impact.

Key Points to Remember:
  • ALTER INDEX REBUILD is used to rebuild an existing index.
  • REBUILD ONLINE allows concurrent DML during most of the operation.
  • We can rebuild an index in a different tablespace.
  • Individual partitions of partitioned indexes can be rebuilt.
  • COALESCE merges adjacent index leaf blocks where possible.
  • Rebuilding an index does not delete table data.
  • Index rebuilding should be based on actual maintenance requirements.

What is a Foreign Key Index in Oracle

A Foreign Key Index in Oracle Database is an index created on a Foreign Key column to improve query performance and help Oracle efficiently locate related records in the child table.

In simple words, a Foreign Key connects two tables. Creating an index on the Foreign Key column can make JOIN operations and searches faster.

Important: Oracle does not automatically create an index on a Foreign Key column. We need to create it manually when required.

1. Example of a Foreign Key Index

Suppose we have two tables:

DEPARTMENT Table (Parent Table)

DEPT_ID DEPT_NAME
10 HR
20 IT
30 Finance

EMPLOYEE Table (Child Table)

EMP_ID EMP_NAME DEPT_ID
101 Ravi 10
102 Kumar 10
103 Priya 20
104 Arun 20
105 Raj 30

Here:

  • DEPT_ID in the DEPARTMENT table is the Primary Key.
  • DEPT_ID in the EMPLOYEE table is the Foreign Key.
  • The Foreign Key connects the EMPLOYEE table with the DEPARTMENT table.

2. How to Create a Foreign Key?

Step 1: Create the Parent Table

CREATE TABLE department
(
    dept_id NUMBER PRIMARY KEY,
    dept_name VARCHAR2(50)
);

Step 2: Create the Child Table

CREATE TABLE employee
(
    emp_id NUMBER PRIMARY KEY,
    emp_name VARCHAR2(50),
    dept_id NUMBER,

    CONSTRAINT fk_emp_dept
    FOREIGN KEY (dept_id)
    REFERENCES department(dept_id)
);

Here, FK_EMP_DEPT is the Foreign Key constraint.

Output: Tables created successfully.

3. How to Create a Foreign Key Index?

Now create an index on the Foreign Key column DEPT_ID in the EMPLOYEE table.

CREATE INDEX idx_emp_dept
ON employee(dept_id);

Explanation:

  • idx_emp_dept → Index name.
  • employee → Child table.
  • dept_id → Foreign Key column.
Output: Index created.

Now Oracle can use this index when searching employees based on department ID.

4. Query Using a Foreign Key Index

Consider the following query:

SELECT *
FROM employee
WHERE dept_id = 10;

Output:

EMP_ID EMP_NAME DEPT_ID
101 Ravi 10
102 Kumar 10

Oracle can use the Foreign Key Index to locate employees belonging to department 10.

Another Example Using JOIN

SELECT e.emp_name,
       d.dept_name
FROM employee e
JOIN department d
ON e.dept_id = d.dept_id;

The index may help Oracle execute the JOIN more efficiently, depending on the execution plan and available access paths.

5. Why Do We Use Foreign Key Indexes?

There are several important reasons for creating an index on a Foreign Key column.

1. Faster JOIN Operations

Indexes can help Oracle find matching child records when joining parent and child tables.

2. Faster SELECT Queries

Queries filtering by Foreign Key columns can benefit from the index.

3. Avoid Unnecessary Full Table Scans

For selective queries, Oracle may use the index instead of scanning the entire child table.

4. Improve Parent Table DELETE and UPDATE Operations

This is particularly important in Oracle.

When a parent Primary Key is deleted or updated, Oracle must check whether child records reference that key.

An index on the Foreign Key can help Oracle find matching child records efficiently.

6. What Happens If We Don't Create a Foreign Key Index?

Suppose we execute the following DELETE statement:

DELETE FROM department
WHERE dept_id = 10;

If the department has matching employee records, Oracle checks the Foreign Key relationship.

Without an index on EMPLOYEE.DEPT_ID, Oracle may need to perform a Full Table Scan on the child table to find matching records.

With an index, Oracle can potentially locate the matching child records more efficiently.

Important Oracle Interview Point:
An unindexed Foreign Key can also cause locking and concurrency problems when parent rows are deleted or updated, particularly when concurrent transactions modify the child table.

7. How to Check Foreign Key Indexes in Oracle?

Step 1: Find Foreign Key Constraints

Use the USER_CONSTRAINTS data dictionary view.

SELECT constraint_name,
       table_name,
       constraint_type
FROM user_constraints
WHERE table_name = 'EMPLOYEE'
AND constraint_type = 'R';

Here, R represents a Foreign Key constraint.

Step 2: Check Indexes on the EMPLOYEE Table

Use the USER_IND_COLUMNS view.

SELECT index_name,
       table_name,
       column_name
FROM user_ind_columns
WHERE table_name = 'EMPLOYEE';

This query displays the index names and their indexed columns.

Step 3: Check Index Details

SELECT index_name,
       table_name,
       uniqueness
FROM user_indexes
WHERE table_name = 'EMPLOYEE';

Here, the UNIQUENESS column indicates whether an index is UNIQUE or NONUNIQUE.

8. Difference Between Primary Key Index and Foreign Key Index

Primary Key Index Foreign Key Index
Created on Primary Key columns. Created on Foreign Key columns.
Oracle normally creates it automatically. Oracle does not automatically create it.
Enforces uniqueness when used for the Primary Key. Does not enforce uniqueness.
Primary Key values cannot be NULL. Foreign Key values can be NULL.
Usually a Unique Index. Usually a Non-Unique Index.

9. Important Oracle Interview Questions

Q1. Does Oracle automatically create an index on a Foreign Key?

No. Oracle does not automatically create an index on a Foreign Key column.

Q2. Is a Foreign Key Index Unique or Non-Unique?

Usually, it is a Non-Unique Index because multiple child records can reference the same parent record.

Q3. Why should we create an index on a Foreign Key?

To improve JOIN performance, child-record searches and parent DELETE or UPDATE operations.

Q4. Can we create a Foreign Key without an index?

Yes. A Foreign Key constraint does not require an index.

Q5. What happens when we delete a parent record without a Foreign Key Index?

Oracle may need to scan the child table to check whether matching child records exist. This can also cause locking and concurrency issues.

10. Interview Answer – What is a Foreign Key Index in Oracle?

A Foreign Key Index in Oracle is an index created manually on a Foreign Key column in a child table. It improves data retrieval, JOIN operations and parent DELETE or UPDATE operations.

Oracle does not automatically create an index on a Foreign Key column.

Example:

CREATE INDEX idx_emp_dept
ON employee(dept_id);

Easy way to remember:

  • Primary Key Index → Usually created automatically by Oracle.
  • Foreign Key Index → Created manually when required.
  • Primary Key Index → Usually Unique.
  • Foreign Key Index → Usually Non-Unique.

Important interview point: Creating an index on a Foreign Key is particularly useful in OLTP databases because it can reduce unnecessary child-table scans and prevent certain locking and concurrency problems during parent-table operations.

Conclusion

A Foreign Key Index is an important concept in Oracle Database. It helps improve query performance, JOIN operations and parent-table DELETE or UPDATE operations.

Unlike Primary Key indexes, Oracle does not automatically create an index on Foreign Key columns. Database developers and DBAs should create Foreign Key indexes based on application requirements and workload.

Key Points to Remember:
  • A Foreign Key connects a parent table and a child table.
  • Oracle does not automatically create a Foreign Key Index.
  • A Foreign Key Index is generally Non-Unique.
  • It can improve JOIN and SELECT query performance.
  • It can help avoid unnecessary child-table scans.
  • It is important for parent DELETE and UPDATE operations.
  • Use USER_CONSTRAINTS and USER_IND_COLUMNS to check Foreign Keys and indexes.