I have 2 options to update a table based on a cursor of a select query.
Say I have this select query:
select id1 from table1
and my update query is :
update table2 set value=1 where table2.id2 = table1.id1
Now the 2 options are:
-
set a cursor the select query and fetch it in bulk and then fire the update query inside the for all statement.
-
write update query with the select subquery as:
update table2 set value=1 where table2.id2 in (select id1 from table1)
which one is better?
Does Oracle internally convert the select subquery in to a bulk collect or does it treat it as a normal cursor?
First on your question “Does Oracle internally convert the select subquery in to a bulk collect?”
No. The optimizer calculates a plan and selects the data from the subquery in some appropiate way. There’s no bulk collect involved.
To your question “which one is better?”. Well, it depends. If you can formulate a query which gets all of your
table1.id1in one run and table2 has lots of rows so the subselect is expensive, then I’d probably use bulk collect. But keep in mind that depending on the amount of data, you’ll need some PGA to accomplish this.But I may point to another – IMHO quite elegant – solution:
That’s usually faster than doing subqueries and faster than bulk collects: one run on table1 and one run on table2. (Add
whereclause to your needs)