There is a cost per statement, so if you can write your
function to do less statements, you're better off...
FOR all IN (select * from TABLE1)
LOOP
FOR some IN (select * from)
LOOP
INSERT INTO TABLE2 VALUES (all.id, some.id)
END LOOP
END LOOP
Replace the whole loop with a single INSERT statement:
INSERT INTO TABLE2 SELECT all.id, some.id FROM all, some WHERE...
But beware of the size of the list you are going to update.
We had a similar problem, we have to create a lot of tables dynamically and insert a lot of data in them. Firstly we create a stored procedure and loops over a list of months and years and create a single table for every month, but it collapse in a single stored procedure.
So, we create the stored procedure but we don't loops in there, instead we loops outside the stored procedure and it works.