In Oracle, indexes can decrease INSERT performance because whenever we insert a new record into a table, Oracle must also maintain the associated indexes.
In simple words:
- Without an index, Oracle inserts data into the table.
- With an index, Oracle inserts data into the table and updates the corresponding index structures.
- More indexes generally mean more maintenance work during INSERT operations.
Let's understand this concept with practical Oracle SQL examples.
Table of Contents
- INSERT Performance Without an Index
- INSERT Performance With an Index
- What Happens If a Table Has Multiple Indexes?
- Why Do Indexes Affect INSERT Performance?
- Normal INSERT vs Bulk INSERT
- How Can We Improve INSERT Performance?
- INSERT vs UPDATE vs DELETE
- Important Oracle Interview Questions
- Important Concept to Remember
1. INSERT Performance Without an Index
First, create an employee table.
Create employee table:
CREATE TABLE employees (
emp_id NUMBER,
emp_name VARCHAR2(100),
salary NUMBER
);
Now insert a record:
INSERT INTO employees
VALUES (101, 'Rahul', 50000);
COMMIT;
Oracle inserts the record into the table.
There are no indexes to maintain apart from any automatically created constraints or other structures, if present.
2. INSERT Performance With an Index
Now create an index on the EMP_ID column.
Create index:
CREATE INDEX idx_emp_id
ON employees(emp_id);
Insert another record:
INSERT INTO employees
VALUES (102, 'Raj', 60000);
COMMIT;
What Happens Internally?
Oracle performs two major operations:
- Inserts the record into the employee table.
- Updates the index IDX_EMP_ID with the new employee ID and corresponding row location (ROWID).
INSERT Operation in Oracle
Stores employee data
Updates index entry and ROWID
Therefore, INSERT operations require additional work when indexes exist.
3. What Happens If a Table Has Multiple Indexes?
Suppose your employee table has five indexes:
CREATE INDEX idx_emp_id
ON employees(emp_id);
CREATE INDEX idx_emp_name
ON employees(emp_name);
CREATE INDEX idx_salary
ON employees(salary);
CREATE INDEX idx_department
ON employees(department);
CREATE INDEX idx_location
ON employees(location);
Now execute an INSERT statement:
INSERT INTO employees
VALUES (103, 'Kiran', 70000, 'IT', 'Hyderabad');
Oracle needs to:
- Insert one row into the table.
- Update IDX_EMP_ID.
- Update IDX_EMP_NAME.
- Update IDX_SALARY.
- Update IDX_DEPARTMENT.
- Update IDX_LOCATION.
4. Why Do Indexes Affect INSERT Performance?
There are several reasons why indexes can affect INSERT performance.
| Reason | Explanation |
|---|---|
| Index Maintenance | Oracle updates every affected index. |
| Redo Generation | Index changes generate redo records. |
| Undo Generation | Oracle maintains undo for transaction consistency. |
| Block Splitting | B-tree index blocks may split when space is insufficient. |
| I/O Operations | Additional index block reads and writes may be required. |
| Contention | Concurrent inserts can compete for index blocks. |
What Is an Index Block Split?
Suppose an index leaf block is full.
When Oracle inserts a new index entry that belongs in that block, it may need to split the block and redistribute entries.
This additional work can affect INSERT performance.
5. Practical Example: Normal INSERT vs Bulk INSERT
Suppose you need to insert 10 lakh employee records.
Method 1: Normal INSERT Using PL/SQL Loop
BEGIN
FOR i IN 1..1000000 LOOP
INSERT INTO employees
VALUES (
i,
'EMP_' || i,
50000,
'IT',
'HYD'
);
END LOOP;
COMMIT;
END;
/
Method 2: Bulk INSERT Using INSERT INTO SELECT
INSERT INTO employees
SELECT level,
'EMP_' || level,
50000,
'IT',
'HYD'
FROM dual
CONNECT BY level <= 1000000;
COMMIT;
The second approach can reduce SQL-to-PL/SQL context switching and improve bulk-loading efficiency.
6. How Can We Improve INSERT Performance?
Here are some approaches used in real-time Oracle projects.
1. Avoid Unnecessary Indexes
Create indexes only when they are needed for query performance or constraints.
2. Use Bulk Loading Techniques
For large data loads, consider direct-path INSERT.
INSERT /*+ APPEND */ INTO employees
SELECT *
FROM employees_staging;
Direct-path INSERT can improve bulk-loading performance by using a different data-loading path.
3. Use Appropriate COMMIT Frequency
Avoid committing after every individual row in a bulk-loading process.
Use suitable batch sizes based on transaction size, undo requirements, and recovery requirements.
4. Consider Partitioning
For very large tables, partitioning can help manage data loads and index maintenance, especially when using partition-specific operations.
5. Evaluate Index Design
Avoid creating multiple indexes with overlapping or unnecessary columns.
Review actual SQL workload before creating additional indexes.
7. INSERT vs UPDATE vs DELETE: Index Impact
Indexes can affect different DML operations in different ways.
| Operation | Index Impact |
|---|---|
| INSERT | Adds entries to affected indexes. |
| UPDATE | Updates index entries if indexed column values change; row movement can also matter. |
| DELETE | Removes corresponding entries from indexes. |
8. Important Oracle Interview Questions
Q1. Do indexes improve INSERT performance?
Generally, no. Indexes usually add overhead to INSERT operations because Oracle must maintain the index structures.
Q2. Does Oracle update indexes automatically during INSERT?
Yes. Oracle automatically maintains associated indexes when rows are inserted.
Q3. If a table has 10 indexes, how many indexes need maintenance during INSERT?
All applicable indexes must be maintained, subject to index type and special cases.
Q4. Can we disable an index before bulk INSERT?
You can make an index unusable in certain bulk-loading scenarios, but this requires careful planning.
Rebuild it afterward and consider constraints, concurrent access, and index dependencies.
Q5. Which is faster for INSERT: a table with no indexes or a table with multiple indexes?
Generally, a table with no indexes has faster INSERT performance because there are fewer index structures to maintain.
9. Important Concept to Remember
Indexes have both advantages and disadvantages.
| SQL Operation | Index Impact |
|---|---|
| SELECT | Can improve query performance. |
| INSERT | Usually adds index maintenance overhead. |
| UPDATE | May require index maintenance. |
| DELETE | Requires index entry maintenance. |
Interview-Ready Answer
"Indexes improve SELECT performance, but they can decrease INSERT performance. Whenever we insert a record into an Oracle table, Oracle must update the table and all applicable indexes. If a table has many indexes, the database performs additional index maintenance, redo generation, and I/O operations. Therefore, we should create indexes based on actual query requirements rather than creating unnecessary indexes."
Happy Learning! Keep Practicing Oracle SQL and DBA Concepts.
No comments:
Post a Comment