Hi,
We are in need to implement SQL stored procedure to archive application data rows from "active" table to "archive" table after some period of days. The rows in active table can be somewhat large (they have CLOB fields, data size from 30 - 100 k.) and there usually is about 300 000 - 500 000 new rows per day in active table.
Because of the number of rows and data size, the transactions are long and fill the transaction logs quickly, these haven't been able to be moved in single huge transaction to archive table.
For this we have made quite simple SQL procedure, which moves the rows by making insert & deletes for 5000 rows in loop and commits, and then repeats until all rows are "moved" to archive table.
This SQL procedure has been running fine so far in development environment, but is this design infact faulty of how it selects which rows to delete after inserts
(check the sample code) ? The insert and delete makes same query to same table, but is the data that the query returns always in same order between insert and delete executions
?! (are the deleted rows always, in all situations the same that were inserted ?)
The more I think the more this current design feels faulty

Do you have some recommendations or tips what better options there would be for implementing this "move procedure" ? (using temp table ?)
Using ORDER BY for selects would ensure the order, but I wonder how big performance hit it might cause on bigger data volumes ... ?
Insert + delete part of the SQL procedure:
--------------------------------------------
-- prmNumberOfDays is input DATE parameter to procedure
SET archiveDate = (CURRENT_DATE - prmNumberOfDays DAYS);
SET countOfRowsToMove = (select count(*) from APP_ACTIVE where DATE(CREATED_TIMESTAMP) < archiveDate)
WHILE (countOfRowsToMove > 0) DO
-- Insert 5000 rows
insert into APP_ARCHIVE(APP_TIMESTAMP, WORK_ID, APP_DATA, CREATED_TIMESTAMP) (select APP_TIMESTAMP, WORK_ID, APP_DATA, CREATED_TIMESTAMP from APP_ACTIVE where DATE(CREATED_TIMESTAMP) < archiveDate FETCH FIRST 5000 ROWS ONLY);
-- Delete 5000 rows (are these always same rows than were in insert step ?)
delete from APP_ACTIVE where ID_APP_ACTIVE in (select ID_APP_ACTIVE from APP_ACTIVE where DATE(CREATED_TIMESTAMP) < archiveDate FETCH FIRST 5000 ROWS ONLY);
-- Commit change
COMMIT;
set countOfRowsToMove = countOfRowsToMove - 5000;
END WHILE;
--------------------------------------------
Thanks for your help.