I am looking to do a commit every 1000 rows until all records are deleted, we have over a million records to be deleted.
Originally:
private static final String DELETE_OLD_REPORTS_FROM_REPORTING =
- "DELETE FROM A_REPORTING\n" +
- "WHERE ID IN(" +
- "SELECT ID FROM A_REPORTING\n" +
- "WHERE STATUS = 'LOADED'\n" +
- "AND CREATE_DT < TO_DATE(:createdDate, 'dd-mon-yyyy'))";
I was thinking of doing something like this:
"BEGIN\n" +
"LOOP\n" +
"DELETE FROM A_REPORTING\n" +
"WHERE ID IN(" +
"SELECT ID FROM A_REPORTING\n" +
"WHERE STATUS = 'LOADED'\n" +
"AND CREATE_DT < TO_DATE(:createdDate, 'dd-mon-yyyy')\n+" +
"AND ROWNUM <= 10000);\n" +
"EXIT WHEN SQL%rowcount < 9999;\n" +
"COMMIT;\n" +
"END LOOP;\n"+
"COMMIT;\n" +
"END";
However, is there a better approach to doing this? The reason for this is because we were getting a ORA-01555 error:
ORA-01555: snapshot too old: rollback segment number %n with name "%segname" too small.
INand add them directly to the outer query (and thus get rid of theINaltogether). Maybe unrelated but may also seep up the query. Do you have an index onSTATUSandCREATE_DT? This might also speed things up. - sticky bitROWNUM. But anINwhich can be eliminated and maybe speed the query up (and maybe fast enough to eliminate the need for the "commit every n records" acrobatics). - sticky bit