Question

Found this here:

In general, consider creating an index on a column in any of the following situations:

  • A referential integrity constraint exists on the indexed column or columns. The index is a means to avoid a full table lock that would otherwise be required if you update the parent table primary key, merge into the parent table, or delete from the parent table.

I don't understand why a full table lock would occurr in such situation. I would've thought that if I tried to delete/update the primary key in the parent table that a full table scan would be performed on the child table.

Where does the lock come from?

Was it helpful?

Solution

Have a look at this Tom Kyte blog entry. In it, he refers to the Oracle documentation, where this explanation is offered:

  • Prevents a full table lock on the child table. Instead, the database acquires a row lock on the index.
  • Removes the need for a full table scan of the child table. As an illustration, assume that a user removes the record for department 10 from the departments table. If employees.department_id is not indexed, then the database must scan employees to see if any employees exist in department 10.

In the first scenario, if the column is not indexed, the entire table must be locked because Oracle does not know which rows must be updated in the child table. With an index, Oracle can identify the rows in question and just lock them. Without the full table lock, it would be possible to modify the parent and have another session modify the child to something that violated the constraint.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top