Skip to Main Content
  • Questions
  • Lock the child table by FOR Update Clause

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question.

Asked: May 07, 2013 - 1:04 pm UTC

Last updated: May 07, 2013 - 7:28 pm UTC

Version: 10.2.0.3

Viewed 1000+ times

You Asked

Hi Tom,

I am using the Oracle 10g and I have question related to "for Update" clause.
We have the data warehouse db, so no foreign key constraint between parent and child.
We process the data files every hour, the condition is If we find the row in parent table then we go and look into child tables and perform insertion (if no corresponding record is present) or updation (if one corresponding record is present) in the child table.

The problem is If I run the two process simultaneously for the same kind of data, and if no record is present in the child table then it create the duplicate in child table.

My question is if I use FOR Update clause while selecting the data in parent table will it lock the child table for any insertion or updation?

Ex- We have employee table for employee 1

In my data files I have the row for employee 1, so when I run the select query on employee table I found 1 row.

The I look the child table "Salary" as there is no record for emp_id =1 in this table I insert the record for this

Emp_id Salary
1 500

The problem is if both the process run at same time then I get duplicate rows in child table

Emp_id Salary
1 500
1 500

we do not want the duplicate row insertion. Can I lock the child table during first process run

and Tom said...

We have the data warehouse db, so no foreign key constraint between parent and child.


there probably should be...
http://asktom.oracle.com/Misc/stuck-in-rut.html

(and not just for materalized views, for optimization in general, you need constraints in your warehouse to make things go faster...)

and since you are slow by slow processing, it is almost as if you are a transactional system anyway, you need these constraints for data integrity.


rather than slowly processing things slow by slow as you are - trying to do it all yourself, why not use external tables and BULK SQL
http://www.youtube.com/watch?v=2jWq-VUeOGs


Just use SET BASED SQL to to merge all of the data in one statement, or if appropriate, use a create table as select (assuming some partitioning in play here) to create the new set of data and exchange partitions to put it into action.

a for update clause locks a row. a lock table command would lock a table. but if you keep up this slow by slow processing, this will make it even more painfully slow than it is.



Is this answer out of date? If it is, please let us know via a Comment

More to Explore

VLDB

If you are new to partitioning, check out Connor McDonald's introduction series here.

VLDB

Documentation set on VLDB and Partitioning.