Spring Data JPA
  1. Spring Data JPA
  2. DATAJPA-124

findAll(Pageable pageable) should not issue a second query when SELECT COUNT() returns 0

    Details

    • Type: Improvement Improvement
    • Status: Closed
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: 1.0.1
    • Fix Version/s: 1.1 RC1
    • Component/s: Core
    • Labels:
      None

      Description

      findAll() first queries the number of records in a table (to return the total number in a Page) and then queries requested page. If the first query returns 0 (empty table), the second query is pointless as it will always return an empty list. Please see patch: https://github.com/nurkiewicz/spring-data-jpa/commit/dd0a213ecd298d4886659213cf8d78de8ad16919 and pull-request: https://github.com/SpringSource/spring-data-jpa/pull/12.

      Actually we might consider even more aggressive optimizations, e.g. if user requests second page with size 10 (records 11-20) but there are only 9 records in the database, querying the database is unnecessary as well.

        Activity

        Hide
        Oliver Gierke added a comment -

        Applied the proposed fix with the more aggressive way. We now don't trigger the actual query if there's less elements in the database than the offset of the given Pageable points to. Applied this to pagination execution for query methods as well.

        Show
        Oliver Gierke added a comment - Applied the proposed fix with the more aggressive way. We now don't trigger the actual query if there's less elements in the database than the offset of the given Pageable points to. Applied this to pagination execution for query methods as well.

          People

          • Assignee:
            Oliver Gierke
            Reporter:
            Tomasz Nurkiewicz
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: