我可以为 spring-data-jpa 启用任何日志以查看为什么它在 Tomcat 中速度慢吗?

Is there any logs I can enable for spring-data-jpa to see why it is slow in Tomcat?

我有一个在 DBeaver 中运行大约 6 毫秒的查询,我使用通用日志提取正在传递的查询,该查询与我的 Tomcat 服务器连接到的数据库相同

select min(organizati0_.Downloaded) as col_0_0_ from oss_collection_history organizati0_ 
where organizati0_.AccountingSystem='SomeSystem' 
and (organizati0_.Org in ('ID1' , 'ID2', ..., 'ID10' ))

在 JUnit 测试中 运行 Spring Data JPA 针对数据库的 Dockerized 副本,查询也运行得非常快 60 毫秒,但在 Tomcat 上它需要 8- 10 秒。

所以不太确定发生了什么。

我这样做只是为了分析使用相同参数的函数调用。

var start = System.currentTimeMillis();
consolidatedDownloadSvc.getAvailableYearsOfDownloadedForAccountingSystem(...);
System.out.println(System.currentTimeMillis() - start);

即使我 set global log_queries_not_using_indexes = 'ON'; 它也不会出现在 slowlog 上,所以这会向我表明查询已编入索引。

所以我想知道是否有某种日志可以显示我在 Spring-Data 和 Hibernate 中可能遗漏的任何额外内容。

除了@Panagiotis 的建议之外,启用以下统计数据也有助于了解休眠在哪个阶段花费更多时间。

spring.jpa.properties.hibernate.generate_statistics=true
logging.level.org.hibernate.stat=DEBUG

您会在日志中看到如下内容

2021-12-29 10:54:52.408  INFO 31972 - – [           main] i.StatisticalLoggingSessionEventListener : Session Metrics {
    5070899 nanoseconds spent acquiring 1 JDBC connections;
    0 nanoseconds spent releasing 0 JDBC connections;
    4359903 nanoseconds spent preparing 20 JDBC statements;
    76117198 nanoseconds spent executing 20 JDBC statements;
    0 nanoseconds spent executing 0 JDBC batches;
    132026893 nanoseconds spent performing 40 L2C puts;
    0 nanoseconds spent performing 0 L2C hits;
    838900 nanoseconds spent performing 19 L2C misses;
    0 nanoseconds spent executing 0 flushes (flushing a total of 0 entities and 0 collections);
    16900 nanoseconds spent executing 1 partial-flushes (flushing a total of 0 entities and 0 collections)
}