简体   繁体   English

如何使用Spring Boot并行执行SQL查询?

[英]How to execute SQL query in parallel using spring boot?

I have multiple charts in a page which will be updated with values from database. 我在页面中有多个图表,这些图表将使用数据库中的值进行更新。 I am making an ajax call when chart is initialized, now the request comes to controller class. 初始化图表时,我正在进行ajax调用,现在请求来自控制器类。 From controller class I am making multiple call to database using repository class object. 从控制器类,我正在使用存储库类对象多次调用数据库。 How can I make a single request to database with multiple queries and get array of response. 如何通过多个查询向数据库发出单个请求并获取响应数组。

For eg Here I have made a 3 different calls to get 3 different value for a chart: 例如,在这里,我进行了3次不同的调用,以为图表获得3种不同的值:

Controller Class and Repository class 控制器类和存储库类

How can I combine these request into single one. 如何将这些请求合并为一个请求。

There is a concept of Batching in JDBC where we can use addBatch and excuteBatch to do what I wanted, but I am not able to understand if I can achieve the same using Spring batch. JDBC中有一个批处理概念,我们可以使用addBatch和excuteBatch来完成我想要的操作,但是我无法理解是否可以使用Spring Batch实现相同的目的。

Statement.addBatch is not supposed to be used for SELECT but for batching INSERT s and UPDATE s Statement.addBatch不应该用于SELECT而是用于批处理INSERTUPDATE

What you need is a bit of a Custom query with UNION to get all the data you need in one sql. 您需要使用UNION进行一些自定义查询,以在一个sql中获取所需的所有数据。

      SELECT COUNT(n.lastUpdatedOn)
      FROM TableEntity n 
      WHERE n.lastUpdatedOn BETWEEN :start1 AND :end1 
      UNION 
      SELECT COUNT(n.lastUpdatedOn)
      FROM TableEntity n 
      WHERE n.lastUpdatedOn BETWEEN :start2 AND :end2 
      UNION 
      SELECT COUNT(n.lastUpdatedOn)
      FROM TableEntity n 
      WHERE n.lastUpdatedOn BETWEEN :start3 AND :end3

And your Repository code. 还有您的存储库代码。

@Query("SELECT COUNT(n.lastUpdatedOn) FROM TableEntity n WHERE n.lastUpdatedOn BETWEEN :start1 AND :end1 UNION SELECT COUNT(n.lastUpdatedOn) FROM TableEntity n WHERE n.lastUpdatedOn BETWEEN :start2 AND :end2 UNION SELECT COUNT(n.lastUpdatedOn) FROM TableEntity n WHERE n.lastUpdatedOn BETWEEN :start3 AND :end3")
List<Long> countModifiedTimeStamp(@Param("start1") Timestamp start1, @Param("end1") Timestamp end1, @Param("start2") Timestamp start2, @Param("end2") Timestamp end2, @Param("start3") Timestamp start3, @Param("end3") Timestamp end3);

And when you call 当你打电话时

List<Long> counts = this.repo.countModifiedTimeStamp(todayStartDay, today, last7days, today, longBack, last7days);

In the returned list you will have today at first element, last7days in second and longBack in third. 在返回的列表中, today将在第一个元素中显示today ,第二个元素是longBack ,第三个元素是last7days

Sounds like you want to implement these as async queries. 听起来您想将这些实现为异步查询。 This is covered in the Spring Data JPA documentation, I believe. 我相信,这在Spring Data JPA文档中有所介绍。 You can use any of these method formats in the current version: 您可以在当前版本中使用以下任何一种方法格式:

@Async
Future<User> findByFirstname(String firstname);               

@Async
CompletableFuture<User> findOneByFirstname(String firstname); 

@Async
ListenableFuture<User> findOneByLastname(String lastname);

https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-async https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-async

(You could also spin off separate Thread instances for each, but I wouldn't advise it) (您也可以为每个Thread分拆单独的Thread实例,但我不建议这样做)

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

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