简体   繁体   中英

PL/SQL FETCH NEXT or rownum

Not much to elaborate, what should be used? they looks to perform the same task.

rownum?

select * from IDENTIFIKATOR
  where rownum <= 10;

or fetch next?

select * from IDENTIFIKATOR
  FETCH NEXT 10 ROWS ONLY;

Upon further expection rownum, might on the surface look to be faster

select * from (select * from IDENTIFIKATOR order by IDENTIFIKATORID )
  where rownum <= 10;  

has a cardinality of 10 and a cost of 10 (plain execution time of 0.15-0.2s (asc vs desc))

select * from IDENTIFIKATOR 
order by IDENTIFIKATORID 
FETCH NEXT 10 ROWS ONLY;

has a cardinality of 10 and a cost of 158869 (plain execution time of 1.9s-2s(asc vs desc))

If you are using Oracle 12c+, then I would suggest that you get used to the FETCH clause. It is ANSI-standard syntax and available in other databases. Plus, it is more flexible. For instance, you can do:

select i.*
from IDENTIFIKATOR i
order by col2 desc
fetch first 10 rows only;

And, it supports the OFFSET modifier.

Doing this using rownum requires a subquery.

On the other hand, if your code needs to work with earlier versions of Oracle, then rownum is basically your only choice.

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