简体   繁体   中英

fetch bulk collect with limit question (Oracle)

Good day! Environment : Oracle 12.2 EE 64-bit.

Suppose I have a code:

Declare
 Rc sys_refcursor;
Type mt is varchar2(25);
Type_mt_t is table of mt;
Mem_tbl mt_t;
begin
Open rc for
Select a.a from tbl a
Where a.path =’1’ 
 And not exists
(select 1 from tbl b
 Where b.path=’-1’
 And b.a = a.a);

Loop 
 Fetch rc bulk collect into mem_tbl limit 500;
 Exit when …
End loop;
End;

The question is – does Open rc clause prefetch all data at once , preliminary, or ref cursor re-scans every time it's called inside “fetch loop”? I'm about read consistency and possible (undesirable of course) cursor mutation during fetch bulk collect limit loop.

Any help would be very appreciated.

TIA, Andrew.

No. The moment you open a cursor in Oracle, the results are "set in stone" as of that moment in time. That does not mean we actually go an read all of the data - we only need to read it as you fetch.

Of course, that raises the question - what happens when we fetch and chance upon some data that someone else has changed since the cursor was opened? (because we don't block anyone whilst we read).

We use internal "undo" information to reverse out those changes (in memory) so that we can give the caller (you) a version of the data as it was at the time your query started.

You'll never see those changes that were committed after your query started - you always get a clean, consistent set of rows.

If you want a longer, more detailed coverage of this, I spend around 20mins on it in this podcast

https://open.spotify.com/episode/3J8FbfQrKFyVAr7F1Ja0gN

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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