Hibernate - 第一个查询总是加载更长时间

Hei*_*erg 5 java postgresql caching hibernate

这里有Hibernate新手.

据我所知,第一级缓存仅在会话打开时可用.会话关闭时,第一级中的所有缓存实体都被逐出/删除.它是否正确?

我有一个使用Hibernate框架在Java中开发的简单CRUD应用程序.每次我的应用程序启动,加载并执行其第一个查询操作时,执行时间通常比后续查询操作长.第一个查询通常需要17ms才能执行,成功通常需要1-2ms.

我的问题是,这真的是Hibernate在应用程序启动时的行为吗?从第一个查询操作加载的数据是否存储在某个缓存中?(绝对不是会话缓存,因为在执行我的第一个查询操作后,会话立即关闭)eager load是否会影响此行为?

我真的不知道从哪里开始,因为Hibernate文档没有涵盖这一点.如果我错了,请纠正我.

我很感激任何帮助,因为我真的不知道从哪里开始阅读这个.

编辑:有关更多信息,这里是第一个和第二个查询操作的hibernate统计信息:

第一:

100222 nanoseconds spent acquiring 1 JDBC connections;
0 nanoseconds spent releasing 0 JDBC connections;
23238430 nanoseconds spent preparing 3 JDBC statements;
8333256 nanoseconds spent executing 3 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;
40215588 nanoseconds spent executing 1 flushes (flushing a total of 3 entities and 3 collections);
135213 nanoseconds spent executing 1 partial-flushes (flushing a total of 0 entities and 0 collections)
Run Code Online (Sandbox Code Playgroud)

第二:

168597 nanoseconds spent acquiring 1 JDBC connections;
0 nanoseconds spent releasing 0 JDBC connections;
2332976 nanoseconds spent preparing 3 JDBC statements;
6427565 nanoseconds spent executing 3 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;
1095389 nanoseconds spent executing 1 flushes (flushing a total of 3 entities and 3 collections);
17600 nanoseconds spent executing 1 partial-flushes (flushing a total of 0 entities and 0 collections)
Run Code Online (Sandbox Code Playgroud)

相同的查询执行但执行时间长度不同.

Dra*_*vic 2

我的问题是,这真的是应用程序启动时 Hibernate 的行为吗?

例如,当您打开 Word 文档时,第一次所需的时间比关闭并再次打开所需的时间要长得多。所以,这实际上不是 Hibernate 特有的行为。

从第一个查询操作加载的数据是否存储在缓存中?

它被缓存“无处不在”。磁盘有其缓存级别。操作系统缓存东西。数据库肯定会缓存经常/最近访问的数据。甚至处理器也有自己的缓存。

但除此之外,Java 本质上也有其预热时间。当你第一次访问一个类时,它会从磁盘加载,通过 JIT 编译等。

我们这里讨论的是17ms;考虑到以上所有因素,这是一个非常好的热身时间。