问题
Currently my code skeleton looks like:
varchar rowidvariable[batchlimitcount][19];
stmt = "Select rowid from table_name where xx"
delstmt = "delete from table_name where rowid=:rowidvariable"
prepare delstatement using delstmt;
prepare cursor from stmt;
declare cursor from preparecursor;
open cursor;
while(1)
{
fetch cursor into rowidvariable;
somecondition {break};
exec sql for fetchedCount
execute delstatement using :rowidvariable;
commit;
}
It was pointed out to me that locking the table using SELECT FOR UPADATE
would be the way to go to ensure 100% that the rows are locked and ROWID
in any instance (however small the chance) doesn't get changed.
However as commit;
releases the lock and its extremely important that I do delete in batches as there are millions of records, there seems to be challenge in moving forward with the proposed solution.
Please advise if there are any better alternatives. Thanks in advance.
Reference to me earlier question : Link
Note the whole process is happening in oracle-pro-c.
回答1:
sounds like the issue is that you have to delete millions of rows and so you want to do it in baches
if so, this might work for you -- it will loop through and delete rows and commit so that you don't run out of undo and you don't have to worry about locking rows
begin
loop
delete from xx where yyy=zzz and rownum < 1000;
exit when sql%rowcount = 0;
commit;
end loop;
commit;
end;
/
来源:https://stackoverflow.com/questions/50608708/interaction-of-select-for-update-lock-with-cursor-and-batch-dml