Skip to Main Content
  • Questions
  • Select for update skip locked failing

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Shri.

Asked: July 19, 2018 - 6:09 pm UTC

Last updated: July 23, 2018 - 10:27 am UTC

Version: Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production

Viewed 1000+ times

You Asked

Hi,
I have a scenario where I have to get first 10 rows from filtered and ordered result set for update.
As we are on multi user environment we are trying to use SELECT FOR UPDATE SKIP LOCKED.

Let me put it in a simple example

Scenario-1

SESSION-1

select emp_id from emp
where rownum>=1
and  rownum <=10
for update skip locked 
order by emp_id;



SESSION-2

select emp_id from emp
where  rownum>=11 
and  rownum <=20
for update skip locked 
order by emp_id;


In above example I'm getting result from Session-1 but not from session-2(assume emp table has 1 million records).

---------------------------------------------------------------------------------
Scenario-2

SESSION-1

select emp_id from emp
where rownum>=1
and  rownum <=10
for update skip locked 
order by emp_id;



SESSION-2

select emp_id from emp
where  rownum>=1
and  rownum <=20
for update skip locked 
order by emp_id;


In above example I'm getting result from Session-1 and session-2 is also returning result from rownum 11 to 20.


I'm using Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit

Am I missing something or is this a bug?

Can you please help me to get top n records from each session with no common records between the sessions?


and Chris said...

This has nothing to do with "for update skip locked". The problem comes from:

where  rownum>=11


Remove the skip locked clause and you'll see still no rows:

select count(*) from scott.emp
where  rownum>=11
and    rownum <=20;

COUNT(*)   
         0 


Why?

Because rownum starts at 1. And increments after the processing the where clause. So:

where  rownum>=11


is false for every row. So you get nothing!

Read more about this at:

https://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:948366252775
https://blogs.oracle.com/oraclemagazine/on-rownum-and-limiting-results

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Clear about rownum but how can I achieve my goal

Shri, July 20, 2018 - 12:30 pm UTC

Thanks Chris! Now I’m clear about rownum.

How can I get top 10 rows from each session and whatever selected rows should not available for any other sessions.(Once selected, rows would be processed and updated in a high dml environment)

select emp_id from emp
where rownum <=10
for update skip locked
order by emp_id;
Chris Saxon
July 23, 2018 - 10:27 am UTC

Remember:

select emp_id from emp 
where rownum <=10 
order by emp_id; 


fetches any 10 rows. Then sorts those by emp_id.

What exactly are you trying to do? Why do you need "for update skip locked"?

More to Explore

SQL

The Oracle documentation contains a complete SQL reference.