gpt4 book ai didi

java - spring boot/hibernate查询结果花费太多时间

转载 作者:行者123 更新时间:2023-11-30 05:28:32 25 4
gpt4 key购买 nike

我正在开发一个Springboot应用程序,我的要求如下:

我有 8 个 SQL 查询,必须并行运行并返回单个结果。我使用此 sql 请求的实体是:

@SqlResultSetMapping(name = "bud01Mapping", classes = {
@ConstructorResult(targetClass = Bud01Entity.class, columns = {
@ColumnResult(name = "MNTBUDGETMB", type = Double.class),

}
) })

@Entity
@Getter
@Setter
@AllArgsConstructor
public class Bud01Entity {

@Id
private Double mntBudgetMB;

}

为了避免我的帖子重复,请考虑:Bud01Entity、Bud02Entity ...直到 Bud08Entity。

我的查询在 hbm.xml 文件 Bud01Entity.hbm.xml 中实现,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings
xmlns="http://java.sun.com/xml/ns/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_2_0.xsd"
version="2.0">
<entity
class="budget.Bud01Entity"
name="Bud01Entity">

<named-native-query
name="Bud01Entity.findBud01"
result-set-mapping="bud01Mapping">
<query>
SELECT
SUM(CASE WHEN budget.sens='C' THEN -1 * budget.mnt_budget ELSE budget.mnt_budget END) AS mntBudgetProduction,
ent.code as codService
FROM si.t_sb_budget budget
JOIN si.t_sb_rubrique_budget rub ON rub.pk_id_rubrique_budget = budget.fk_id_rubrique_budget
JOIN si.t_ga_activite act ON act.pk_id_activite = budget.fk_id_activite
JOIN si.t_rd_service serv ON act.fk_id_service = serv.pk_id
JOIN si.t_rd_entite ent ON ent.pk_id = serv.pk_id
JOIN si.t_sb_type_budget typ_bud ON typ_bud.pk_id_type_budget=budget.fk_id_type_budget
WHERE budget.exercice = :codAnnee
AND ent.code = :codService
AND typ_bud.pk_id_type_budget = 6
GROUP BY
ent.code
</query>
</named-native-query>

</entity>
</entity-mappings>

对于第一个,为了避免在我的帖子中重复,请考虑:Bud01Entity.hbm.xml、Bud02Entity.hbm.xml ... Bud08Entity.hbm.xml。

我的服务实现如下:

@Async
@Slf4j
@Service
public class Bud01Service {

@Autowired
private Bud01Repository bud01Repository;

public CompletableFuture<List<Bud01Entity>> findBud(String codService, String codAnnee) {
log.info("Running findBud for service {} and annee {}", codService, codAnnee);
List<Bud01Entity> data = bud01Repository.findBud01(codService, codAnnee);
return CompletableFuture.completedFuture(data);
}
}

与其他相同,Bud02Service、...Bud08Service 的逻辑相同。

我的存储库界面如下:

@Repository
public interface Bud01Repository extends JpaRepository<Bud01Entity, Double> {
List<Bud01Entity> findBud01(@Param("codService") String codService, @Param("codAnnee") String codAnnee);
}

这 8 个结果中的每一个都代表 BudAllEntity 的属性:

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class BudAllEntity {

private Double mntBudgetProduction;
private Double mntBudgetMB;
private Double mntBudgetRegul;
private Double mntBudgetFraisGenAct;
private Double mntBudgetFraisGenSer;
private Double mntBudgetFraisGenReg;
private Double mntBudgetFraisGenSie;
private Double mntBudgetFraisFin;

}

还有我的聚合端点:

@Slf4j
@RestController
@RequestMapping("/AggBud")
public class BudAggEndpoint {

@Autowired
private Bud01Service bud01Service;

@Autowired
private Bud02Service bud02Service;

@Autowired
private Bud03Service bud03Service;

@Autowired
private Bud04Service bud04Service;

@Autowired
private Bud05Service bud05Service;

@Autowired
private Bud06Service bud06Service;

@Autowired
private Bud07Service bud07Service;

@Autowired
private Bud08Service bud08Service;

@GetMapping(value = "details", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public Page<BudAllEntity> findBudClient(@RequestParam(value = "codService") String codService,
@RequestParam(value = "codAnnee") String codAnnee,
@RequestParam(required = false) Pageable pageRequest) throws ExecutionException, InterruptedException {

CompletableFuture<List<Bud01Entity>> att1 = bud01Service.findBud(codService, codAnnee);
CompletableFuture<List<Bud02Entity>> att2 = bud02Service.findBud(codService, codAnnee);
CompletableFuture<List<Bud03Entity>> att3 = bud03Service.findBud(codService, codAnnee);
CompletableFuture<List<Bud04Entity>> att4 = bud04Service.findBud(codService, codAnnee);
CompletableFuture<List<Bud05Entity>> att5 = bud05Service.findBud(codService, codAnnee);
CompletableFuture<List<Bud06Entity>> att6 = bud06Service.findBud(codService, codAnnee);
CompletableFuture<List<Bud07Entity>> att7 = bud07Service.findBud(codService, codAnnee);
CompletableFuture<List<Bud08Entity>> att8 = bud08Service.findBud(codService, codAnnee);

CompletableFuture.allOf(att1, att2, att3, att4, att5, att6, att7, att8);

log.info("Retrieve Budgets");

List<BudAllEntity> finalResult = new ArrayList<BudAllEntity>();
BudAllEntity result = new BudAllEntity();
try{
result.setMntBudgetProduction(att1.get().get(0).getMntBudgetProduction());
result.setMntBudgetMB(att2.get().get(0).getMntBudgetMB());
result.setMntBudgetRegul(att3.get().get(0).getMntBudgetRegul());
result.setMntBudgetFraisGenAct(att4.get().get(0).getMntBudgetFraisGenAct());
result.setMntBudgetFraisGenReg(att5.get().get(0).getMntBudgetFraisGenSer());
result.setMntBudgetFraisGenSer(att6.get().get(0).getMntBudgetFraisGenReg());
result.setMntBudgetFraisGenSie(att7.get().get(0).getMntBudgetFraisGenSie());
result.setMntBudgetFraisFin(att8.get().get(0).getMntBudgetFraisFin());
} catch (Exception e){
log.error(e.getMessage(),e);
}


finalResult.add(result);
Pageable page = Optional.ofNullable(pageRequest).orElse(PageRequest.of(0, 20));
return new ResponsePage<>(finalResult, page, finalResult.size());
}
}

我在主类中添加了 @EnableAsync 注释。

有没有办法让这段代码更高效、更简单?

预先感谢您的帮助

最佳答案

您遵循的方法看起来不错,但有一些小错误需要注意

@Async Annotation它应该注释在方法而不是类上

  • 它只能应用于公共(public)方法
  • 自调用 - 从同一个类中调用异步方法 - 不起作用

The reasons are simple – the method needs to be public so that it can be proxied. And self-invocation doesn't work because it bypasses the proxy and calls the underlying method directly.

覆盖执行器:

By default, Spring uses a SimpleAsyncTaskExecutor to actually run these methods asynchronously.

覆盖默认执行器

@Configuration
@EnableAsync
public class SpringAsyncConfig {

@Bean(name = "threadPoolTaskExecutor")
public Executor threadPoolTaskExecutor() {
ThreadPoolTaskExecutor pool = new ThreadPoolTaskExecutor();
pool.setMaxPoolSize(10);
return pool;
}
}

然后执行器名称应作为 @Async 中的属性提供:

@Async("threadPoolTaskExecutor")
public void asyncMethodWithConfiguredExecutor() {
System.out.println("Execute method with configured executor - "
+ Thread.currentThread().getName());
}

注意:确保您创建的执行程序线程池具有与 CPU 资源匹配的 N 个线程 here

关于java - spring boot/hibernate查询结果花费太多时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58031491/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com