简体   繁体   中英

How to Cache Spring JPA Pagination Count Data?

I'm Creating a Simple Spring Boot application with JPA. I have millions of records to retrieve it from a database. in my repository when I call findAll(Pageable pageable) it gets the records I want.

But takes lot of time to return me the subset of records. I found it happening because of count query.

Is there a way we can cache the page response after first call ?

It's easy to see why count takes longer than select, because select usually has limit, but select count might require a full table scan.

Caching page is not supported in jpa, because the data is changing all the time.

If you want to simply display the data(you don't need to support other filtering displays), you can cache count and not use findAll(Pageable pageable) , you need to query datas(use native query) , assemble a Page object and return it to front end.

Of course you need to update count when insert/delete .

Maybe you don't need to support displaying exact pages, or you can use other types of databases such as Elasticsearch ?

Futhermore: see : link may be help?

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