What is the impact of indexes on DELETE performance

In Oracle, indexes can decrease DELETE performance because whenever we delete a record from a table, Oracle must also maintain all applicable indexes by removing the corresponding index entries.

In simple words:

  • Without indexes, Oracle deletes the row from the table.
  • With indexes, Oracle deletes the row and removes its corresponding entries from the indexes.
  • More indexes generally mean additional maintenance work during DELETE operations.

Let's understand this concept with practical Oracle SQL examples.

1. DELETE Performance Without an Index

First, create an employee table.

Create employee table:

CREATE TABLE employees (
    emp_id NUMBER,
    emp_name VARCHAR2(100),
    salary NUMBER,
    department VARCHAR2(50)
);

Insert sample records:

INSERT INTO employees
VALUES (101, 'Rahul', 50000, 'IT');

INSERT INTO employees
VALUES (102, 'Raj', 60000, 'HR');

INSERT INTO employees
VALUES (103, 'Kiran', 70000, 'IT');

COMMIT;

Now delete one employee:

DELETE FROM employees
WHERE emp_id = 101;

COMMIT;

Oracle deletes the corresponding row from the table.

If there are no secondary indexes, there are no secondary index entries to maintain.

Important: Without secondary indexes, Oracle does not need to perform secondary index-entry maintenance during the DELETE operation.

2. DELETE Performance With an Index

Now create an index on the EMP_ID column.

Create index:

CREATE INDEX idx_emp_id
ON employees(emp_id);

Execute the DELETE statement:

DELETE FROM employees
WHERE emp_id = 102;

COMMIT;

What Happens Internally?

Oracle performs the following operations:

  1. Oracle locates the employee record.
  2. Oracle deletes the row from the table.
  3. Oracle removes or marks the corresponding entry in the IDX_EMP_ID index as deleted.

DELETE Operation in Oracle

DELETE FROM EMPLOYEES
↓
WHERE EMP_ID = 102
↓
Oracle Locates the Row
↓
Table

Deletes the employee row

Index

Marks corresponding index entry as deleted

Therefore, DELETE requires additional index maintenance.

Important: Oracle generally marks deleted index entries as deleted rather than immediately removing and reorganizing all index blocks. The space can be reused later.

3. What Happens If a Table Has Multiple Indexes?

Suppose your employee table has three 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);

Now execute:

DELETE FROM employees
WHERE emp_id = 103;

Oracle needs to:

  1. Locate the employee record, possibly using IDX_EMP_ID.
  2. Delete the record from the employee table.
  3. Maintain the corresponding entry in IDX_EMP_ID.
  4. Maintain the corresponding entry in IDX_EMP_NAME.
  5. Maintain the corresponding entry in IDX_SALARY.
Performance Impact: When a table has multiple indexes, Oracle must maintain all applicable indexes during DELETE operations. This can increase execution time, redo generation, undo generation, and I/O.

4. Why Do Indexes Affect DELETE Performance?

There are several reasons why indexes can affect DELETE performance.

Reason Explanation
Index Maintenance Oracle removes or marks corresponding index entries as deleted.
Redo Generation Index changes generate redo records.
Undo Generation Oracle maintains undo for transaction consistency.
Additional I/O Index blocks may need to be accessed and modified.
Locking and Contention Concurrent operations can compete for resources.
Remember: Deleted index entries are not necessarily removed from the physical index blocks immediately. Oracle manages index space for subsequent operations.

5. How Can We Improve DELETE Performance?

Here are some approaches used in real-time Oracle projects.

1. Avoid Unnecessary Indexes

Create indexes only when they provide a meaningful benefit to queries or constraints.

2. Use Appropriate WHERE Conditions

Delete only the required records.

DELETE FROM employees
WHERE emp_id = 101;

This deletes a specific employee instead of deleting unnecessary rows.

3. Use TRUNCATE When Deleting All Records

If you want to remove all records from a table, consider TRUNCATE.

TRUNCATE TABLE employees;

TRUNCATE is generally faster than deleting every row individually because it is a DDL operation that deallocates data segments according to its options.

Important:
  • TRUNCATE cannot be rolled back like DELETE.
  • DELETE triggers do not fire for TRUNCATE.
  • It requires appropriate privileges.
  • It may be restricted by foreign-key references.

4. Use Partition Maintenance for Large Tables

If a large table is partitioned by date, you can remove historical data using:

ALTER TABLE sales
DROP PARTITION p2024;

This can be much more efficient than deleting millions of individual rows, but associated indexes must also be considered.

6. Difference Between DELETE and TRUNCATE

This is a very important Oracle DBA and interview topic.

DELETE TRUNCATE
DML operation DDL operation
Can delete selected rows Removes all rows from the table or specified partition
Generates undo for deleted rows Does not generate row-by-row undo
DELETE triggers fire DELETE triggers do not fire
Can be rolled back before commit Cannot be rolled back
Maintains applicable indexes row by row Handles associated index structures as part of truncation

7. INSERT vs UPDATE vs DELETE: Index Impact

Let's understand how different DML operations affect indexes.

Operation Index Impact
INSERT Adds entries to applicable indexes.
UPDATE Indexed Column Updates affected index entries.
UPDATE Non-Indexed Column Generally no index-entry modification.
DELETE Removes or marks corresponding entries as deleted in applicable indexes.

8. Important Oracle Interview Questions

Q1. Do indexes affect DELETE performance?

Yes. Oracle must maintain the corresponding entries in all applicable indexes when a row is deleted.

Q2. If a table has five indexes, will all five indexes be maintained during DELETE?

Yes. All applicable indexes must be maintained because the deleted row's entries must be removed or marked as deleted in them.

Q3. Which is faster: DELETE or TRUNCATE?

TRUNCATE is generally faster when removing all rows because it avoids row-by-row deletion.

Q4. Does DELETE remove index entries?

Yes. Oracle marks the corresponding index entries as deleted and manages the associated index space.

Q5. Does DELETE make an index unusable?

No. Normal DELETE operations do not make an index unusable.

9. Important Interview Takeaway

Remember this simple concept:

SQL Operation Index Impact
SELECT Can improve performance.
INSERT Additional maintenance overhead.
UPDATE Indexed Column Additional maintenance overhead.
DELETE Additional maintenance overhead.

Interview-Ready Answer

"Indexes can decrease DELETE performance in Oracle because whenever we delete a record from a table, Oracle must also remove the corresponding entries from all applicable indexes. This involves additional index maintenance, redo generation, undo generation, and I/O operations. Therefore, tables with many indexes may experience slower DELETE operations."

10. Next Topic to Learn

Now that we understand how indexes affect INSERT, UPDATE, and DELETE operations, the next important concept is how indexes improve SELECT performance.

Learn how Oracle chooses between:

  • INDEX SCAN
  • FULL TABLE SCAN

Understanding these concepts is very important for Oracle SQL performance tuning and DBA interviews.

Happy Learning! Keep Practicing Oracle SQL and DBA Concepts.

No comments:

Post a Comment