Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MyBatis RowBounds vs Oracle pagination query using rownum and nested subquery

I want to know which of the below performs better in terms of time to execute query with 100k+ records

1) Oracle's Pagination

SELECT *
FROM  (
   SELECT id, col1, col2, rownum rn
   FROM (
      SELECT /*+ first_rows(50) */ id, col1, col2
      FROM   table1
      ORDER  BY id DESC
   )
   WHERE   rownum <= 50
)
WHERE  rn >= 20;

2) Pagination using MyBatis RowBounds.

MyBatis RowBounds uses normal JDBC and after firing the select it skips the first 20 records and then fetches the next 30 (pagesize).

Also, will the MyBatis approach become slower as the page number increases as more rows needs to be skipped?

like image 203
samarjit samanta Avatar asked Nov 03 '22 21:11

samarjit samanta


1 Answers

In general, the JDBC driver would have to fetch the first 20 rows across the network so that will generally be less efficient than writing a pagination query. That penalty will grow larger as you fetch more pages.

Both approaches will, in general, get slower as you fetch subsequent pages. But that is normally not an issue-- if you are paging results, that implies that users aren't going to actually fetch more than a couple pages of results before they give up.

like image 158
Justin Cave Avatar answered Nov 09 '22 13:11

Justin Cave