简体   繁体   English

如何缓存 Spring JPA 分页计数数据?

[英]How to Cache Spring JPA Pagination Count Data?

I'm Creating a Simple Spring Boot application with JPA.我正在使用 JPA 创建一个简单的 Spring Boot 应用程序。 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.在我的存储库中,当我调用findAll(Pageable pageable)它会获取我想要的记录。

But takes lot of time to return me the subset of records.但是需要很多时间才能返回记录的子集。 I found it happening because of count query.我发现它是由于count查询而发生的。

Is there a way we can cache the page response after first call ?有没有办法在第一次调用后缓存page响应?

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.很容易理解为什么 count 比 select 花费的时间长,因为 select 通常有限制,但 select count 可能需要全表扫描。

Caching page is not supported in jpa, because the data is changing all the time. jpa不支持缓存page ,因为数据一直在变化。

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.如果你想简单地显示数据(你不需要支持其他过滤显示),你可以缓存count而不使用findAll(Pageable pageable) ,你需要查询数据(使用原生查询),组装一个Page对象和将其返回到前端。

Of course you need to update count when insert/delete .当然,您需要在insert/delete时更新count

Maybe you don't need to support displaying exact pages, or you can use other types of databases such as Elasticsearch ?也许您不需要支持显示精确页面,或者您可以使用其他类型的数据库,例如Elasticsearch

Futhermore: see : link may be help?此外:请参阅: 链接可能有帮助?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM