Skip to content

Instantly share code, notes, and snippets.

@cedricvidal
Created November 15, 2018 09:57
Show Gist options
  • Save cedricvidal/92fbe29331acfdb3c44f75be2df13326 to your computer and use it in GitHub Desktop.
Save cedricvidal/92fbe29331acfdb3c44f75be2df13326 to your computer and use it in GitHub Desktop.
Optimized Oracle pagination

Where :

  • FIRST_ROWS(N) tells the optimizer, "Hey, I'm interested in getting the first rows, and I'll get N of them as fast as possible."
  • :MAX_ROW_TO_FETCH is set to the last row of the result set to fetch—if you wanted rows 50 to 60 of the result set, you would set this to 60.
  • :MIN_ROW_TO_FETCH is set to the first row of the result set to fetch, so to get rows 50 to 60, you would set this to 50.

Source https://blogs.oracle.com/oraclemagazine/on-rownum-and-limiting-results

select *
from ( select /*+ FIRST_ROWS(n) */
a.*, ROWNUM rnum
from ( your_query_goes_here,
with order by ) a
where ROWNUM <=
:MAX_ROW_TO_FETCH )
where rnum >= :MIN_ROW_TO_FETCH;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment