简体   繁体   English

用于在Oracle 12c上用ROWNUM锁定的更新跳过

[英]FOR UPDATE SKIP LOCKED with ROWNUM on ORACLE 12c

I searched all the forum but I didn't find any clue about it. 我搜索了所有论坛,但没有找到任何线索。 I have a staging table that multiple threads consume. 我有一个多线程消耗的临时表。 To avoid deadlock, I'm using something like this: 为了避免死锁,我使用了以下方法:

SELECT ID_MESSAGE
FROM TB_STAGE_IN S 
WHERE S.CD_STATUS = 0 
AND S.ID_JOB_SCHEDULE IS NULL 
AND ROWNUM <= 10000 
FOR UPDATE SKIP LOCKED; 

It works fine, but the threads don't reach the max of 10,000 rows. 它工作正常,但线程最多不能达到10,000行。 It's like: 就像是:

  • Thread 1: 5000 线程1:5000
  • Thread 2: 3000 线程2:3000
  • Thread 2: 2000 线程2:2000

I know that happens because the rownumber for them is the same, but the table has thousands and thousands of rows. 我知道发生这种情况是因为它们的行号相同,但是表有成千上万的行。 What I really need is the thread gets 10,000 rows unlocked on every step. 我真正需要的是线程在每一步都获得10,000行解锁。

I tried using FETCH FIRST 10000 ROWS ONLY, but I receive the message below: ORA-02014: cannot select FOR UPDATE from view with DISTINCT, GROUP BY, etc. 我尝试仅使用FETCH FIRST 10000 ROWS,但收到以下消息:ORA-02014:无法从DISTINCT,GROUP BY等视图中选择FOR UPDATE。

Could you all please help me? 大家能帮我吗?

Thanks for the kind. 谢谢你的好意

Ask Tom has a suggestion that goes like this Ask Tom有这样的建议

open C;  -- cursor C is select ... for update skip locked;

loop
  fetch C bulk collect into :my_array limit 100;
  append :my_array to :to_be_processed_array;
  exit when c%notfound or :to_be_processed_array.count >= 10000;
end loop;

-- process any rows in :to_be_processed_array

close C;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM