Interaction of SELECT FOR UPDATE lock with cursor and batch DML

余生长醉 提交于 2020-01-06 04:52:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!