欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

hibernate generate_statistics

最编程 2024-03-27 22:09:13
...

这是hibernate提供的一个调优功能,在spring配置文件中加入下面的配置:

spring:
    properties:
      hibernate:
        format_sql: true
        generate_statistics: true

就可以打印每个阶段的执行时间

i.StatisticalLoggingSessionEventListener : Session Metrics {
     1747799 nanoseconds spent acquiring 1 JDBC connections;
     0 nanoseconds spent releasing 0 JDBC connections;
     16084554 nanoseconds spent preparing 7 JDBC statements;
     7851937 nanoseconds spent executing 7 JDBC statements;
     0 nanoseconds spent executing 0 JDBC batches;
     0 nanoseconds spent performing 0 L2C puts;
     0 nanoseconds spent performing 0 L2C hits;
     0 nanoseconds spent performing 0 L2C misses;
     0 nanoseconds spent executing 0 flushes (flushing a total of 0 entities and 0 collections);
     0 nanoseconds spent executing 0 partial-flushes (flushing a total of 0 entities and 0 collections)

从上面可以看出上面涉及到7条sql语句,获取jdbc连接耗时1747799纳秒,预准备阶段耗时16084554纳秒,执行阶段7851937纳秒

以及缓存命中情况,当发现sql执行过慢时,可以通过generate_statistics这个功能发现问题

PS:

上面不是一条sql的执行情况,而是一个任务执行涉及到的所有有效sql,比如下面这种

T findById(String id){
   sql1;
  sql2;
 sql3;
} 

上面的一次完整查询(一个Session)总共涉及到3条sql

推荐阅读