When dealing with oracle initial loads or with massif updates/deletes that generate undo (also known as rollback) and redo overheads, then the most frequent error in this context is the so famous ORA-01555: Rollback segment snapshot too old.
What also makes this error very frequent in many production applications is the data volume difference between test and production environments of those applications. Due to this data volume difference, the ORA-01555 error takes place most frequently in production were we are too late to change the logic and were it is so difficult to intervene.
This is why one of the obvious but very important solution to avoid this error is to have a real test environment with a leat the same data volume as it will be the case in production. Thus if the ORA-01555 error will have to happen, it will be pointed out much earlier in the development process making changes more flexible.
In On-Line Transaction Processing (OLTP) systems I have been faced with the ORA-01555 error in many cases. One of the main actions I realised I have to do in order to avoid it is to do not commit accross fetch (this is what many developers came to a long time before me).
Commit across fecth represents a commit inside a loop as shown here below:
FOR r_cur IN c_cur LOOP -- Traitement is done here update/delete .... COMMIT; -- commit across fetch increases the odds of ora-01555 END LOOP;
Thus in order to drastically decrease the odds of ORA-01555, commit outside the loop as shown below:
FOR r_cur IN c_cur LOOP -- Traitement is done here update/delete .... END LOOP; COMMIT; -- Commit is done here outside the loop
But commiting only once for the whole process will certainly contribute to avoid ORA-01555 error but may also increases the odds of its twin sister ORA-01562 : failed to extend rollback segment number string.
So that the probability of the occurence of both errors will decrease, the above plsql code are updated as follows:
DECLARE v_record_number NUMBER := 0; v_commit_val NUMBER := 500; -- commit when 500 records have been processed BEGIN LOOP -- main technical loop v_record_number := 0; FOR r_cur IN c_cur LOOP -- log the number of treated record v_record_number := v_record_number + 1; -- Traitement is done here update/delete .... -- Decide to commit IF v_record_number >= v_commit_val THEN EXIT; END IF; END LOOP; -- Cursor is closed automatically in the for loop COMMIT; -- commit outside the inner loop EXIT WHEN v_record_number less than v_commit_val ; END LOOP; -- inner loop COMMIT; END; -- main technical loop</strong>
In any way, one thing you must absolutely consider is to make your process restartable. If not, then any ORA-01555 or ORA-01562 error will let updates done in the FOR loop cursor in a totally unknown state and will be considered as fatal.