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

标签 java hibernate postgresql caching

这里是 Hibernate 新手。

据我了解,一级缓存仅在 session 打开时可用。当 session 关闭时,第一级中的所有缓存实体都将被逐出/删除。这是正确的吗?

我有一个使用 Hibernate 框架用 Java 开发的简单 CRUD 应用程序。每次我的应用程序启动、加载和执行它的第一个查询操作时,执行时间通常比后续的查询操作长。第一个查询通常需要 17 毫秒来执行,后续是 1-2 毫秒。

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

我真的不知道从哪里开始,因为 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)

第二个:

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)

Same query execution but different execution time length.

最佳答案

My question is this, is this really the behavior of Hibernate upon the start of the application?

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

Is the data loaded from the first query operation stored in a cache somewhere?

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

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

我们在这里谈论的是 17 毫秒;考虑到以上所有因素,这是一个很好的热身时间。

关于java - Hibernate - 第一个查询总是加载更长的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33474957/

相关文章:

java - 使用 Hibernate 批量插入或更新?

macos - Postgresql 数据库在 OS X 上的位置?

postgresql - 表创建的日期/时间格式

java - OpenCV 选择灰色颜色范围

java - GregorianCalendar 构造函数是否检查无效输入?

java - 无法与Hadoop一起使用CompositeInputFormat,抛出异常Expression为null

sql - Postgres 更新规则返回受影响的行数

java - 在 Java 中将 XML DOM 打印到字符串时将前缀传播到子元素

java - Hibernate(最佳)最常见的结构

java - org.hibernate.NonUniqueObjectException。到底是什么原因呢?