3
我刚刚掌握了一个应用程序,其目标是提取大量数据(从包含10,000,000行的表中最多100,000行)。不幸的是,提取是用Java + Hibernate编写的,性能相对较差。使用Java + Hibernate提取100,000行大约需要1分30秒。使用Talend进行同样的提取需要大约30秒(少3倍)。Hibernate巨型查询优化
这里的代码是什么样子的样本:
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来做其他事情。
了解查询是什么以及如何创建Criteria将有助于优化它。 –
两年前我的情况非常相似。主要问题是该域有很多懒惰的集合,导致非常非常多的选择。这个以及其他性能问题通过JProfiler分析非常简单:http://www.ej-technologies.com/products/jprofiler/overview.html。 – heikkim
如果您提供了有关Hibernate如何配置的细节,这将会很有帮助。 Criteria是如何定义的?如何定义持久化对象? 'while(result.next())...'块中的代码是什么?这些答案以及您可能提供的任何其他相关细节对于获得有用的答案都很有价值。 –