简体   繁体   English

有没有我可以为 spring-data-jpa 启用的日志,以查看为什么它在 Tomcat 中速度慢?

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

I have a query which runs in about 6ms in DBeaver and I used the general log to extract the query that is being passed in which is the same database that my Tomcat server is connected to我有一个在 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' ))

On a JUnit test running Spring Data JPA against a Dockerized copy of the database, the query also runs quite fast 60ms, but on Tomcat it takes upwards of 8-10 seconds.在针对数据库的 Dockerized 副本运行 Spring 数据 JPA 的 JUnit 测试中,查询也运行得非常快 60 毫秒,但在 Tomcat 上它需要 8-10 秒以上。

So not exactly sure what's going on.所以不确定发生了什么。

I just do this to profile the function call using the same parameters.我这样做只是为了使用相同的参数分析 function 调用。

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

It does not appear on slowlog even if I set global log_queries_not_using_indexes = 'ON';即使我set global log_queries_not_using_indexes = 'ON'; so that would indicate to me that the query is indexed.这样就可以向我表明查询已编入索引。

So I was wondering if there's some sort of log that will show me any extra things I may be missing in Spring-Data and Hibernate.所以我想知道是否有某种日志可以显示我在 Spring-Data 和 Hibernate 中可能丢失的任何额外内容。

You can use the following set of properties in your application.properties file.您可以在application.properties文件中使用以下属性集。 If you don't have a specific logger policy those will be pushed to your tomcat catalina.log file如果您没有特定的记录器策略,它们将被推送到您的 tomcat catalina.log文件

spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.use_sql_comments=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.type=trace

Apart from what @Panagiotis suggested it will also be helpful to enable below stats to understand in what phase is hibernate spending more time.除了@Panagiotis 建议的内容之外,启用以下统计信息也有助于了解 hibernate 在哪个阶段花费更多时间。

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

You would see something like below in your logs您会在日志中看到类似下面的内容

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)
}

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

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