0

When using a ScrollableResults, does a call to last() move directly to the last record or is it stepping through each record until it reaches the last one? E.g. is it performant, or not?

This is for MySQL 8 via the MySQL Connector for Java. Goal here is to get a total record count w/out issuing a second COUNT() query and also not iterating every record, and certainly not bringing them all into memory.

// just skip to the last record, don't pull each from the DB (??)
scrollableResultSet.last();
totalCount = scrollableResultSet.getRowNumber() + 1;
Josh M.
  • 26,437
  • 24
  • 119
  • 200
  • 1
    It's a complicated question, depending on the driver and other details. If all the results are in memory, then it's fast but you're using a lot of memory. If you have a cursor backing the `ScrollableResults`, it could give you performance comparable to native database cursor scrolling. This is tricky business, and the [drivers don't make it easy](https://stackoverflow.com/q/2826319/2541560). – Kayaman Jan 07 '22 at 16:59
  • Sorry, this is for MySQL 8 via the MySQL Connector for Java. I know there is still more to it. Goal here is to get a total record count w/out issuing a second `COUNT()` query and also not iterating every record, and certainly not bringing them all into memory. – Josh M. Jan 07 '22 at 18:53
  • 1
    Ah, the classic "get the total count while also getting the data", why didn't you say so. There should be plenty of duplicates or near-duplicates for ideas around here. IIRC a *simultaneous* count query is one of the best approaches if possible. – Kayaman Jan 07 '22 at 19:10
  • [This is probably the duplicate](https://stackoverflow.com/questions/168084/is-there-a-more-efficient-way-of-making-pagination-in-hibernate-than-executing-s). – Kayaman Jan 07 '22 at 19:18

0 Answers0