Hibernate巨型查询优化

时间:2012-05-09 11:35:20

标签: hibernate optimization

我刚刚负责一个应用程序,其目标是从包含10,000,000行的表中提取大量数据(最多100,000行)。不幸的是,提取是用Java + Hibernate编写的,性能相对较差。使用Java + Hibernate提取100,000行大约需要1分30秒。使用Talend进行相同的提取大约需要30秒(少3倍)。

以下是代码的示例:

Launcher.initStatelessSession();
Launcher.beginStatelessTransaction();

//Creation of the Criteria crit, no join, only a single table is read.
int fetchSize = 1000;
crit.setFetchSize(fetchSize);
crit.setCacheable(false);
crit.setReadOnly(true);

ScrollableResults result = crit.scroll(ScrollMode.FORWARD_ONLY);
// Most of the time is spent from HERE ...
while (result.next()) {
   // Some code but insignificant time compared to the result.next().
   // I replaced this code with continue; and the speed did not really change.
}
// ... to HERE

关于可以加速此查询的优化的任何想法?目前,没有计划放弃Hibernate用于其他目的。

1 个答案:

答案 0 :(得分:0)

我不知道talend是什么,但我怀疑它是某种数据库gui工具?

在这种情况下,可能的原因可能是hibernate使对象脱水,即检查检索到的对象是否尚未在会话中,创建实例并填充所有属性(可能与其他引用的实体)。

使用分析器更详细地了解实际情况

所有这些假设您实际执行相同的sql语句。正如评论中所提到的,根据你的标准和映射,hibernate可能会创建非常“有趣”的select语句。