Does Oracle 11g optimize For Loops over arrays?
If so how does it do it?
Example:
Does the following code treat each instance of lt_some_array(i) as a pointer to the record in the array at index i? Or, does it look it up every time?
FOR i IN lt_some_array.FIRST..lt_some_array.LAST
LOOP
field1 := lt_some_array(i).field1;
field2 := lt_some_array(i).field2;
field3 := lt_some_array(i).field3;
field4 := lt_some_array(i).field4;
field5 := lt_some_array(i).field5;
field6 := lt_some_array(i).field6;
do_something();
END LOOP;
Clarification:
Does the compiler treat it like the following (where lr_some_row is a pointer and not a copy):
FOR i IN lt_some_array.FIRST..lt_some_array.LAST
LOOP
lr_some_row := lt_some_array(i);
field1 := lr_some_row.field1;
field2 := lr_some_row.field2;
field3 := lr_some_row.field3;
field4 := lr_some_row.field4;
field5 := lr_some_row.field5;
field6 := lr_some_row.field6;
do_something();
END LOOP;
Yes, Oracle 11g performs that optimization for you.
That feature was added in 10g. See page 60 of the presentation “PL/SQL Performance – Debunking the Myths”. It includes an example very similar to your code. The manual optimization was almost twice as fast in 9iR2, but does not help at all in 10gR1.
Although I can’t tell you how Oracle does it. As far as I know, that level of detail about how Oracle works does not exist.