Hibernate 4.1.6を使用していますが、リストの作成速度に問題があります。次のクエリを実行しています。
public void doQuery(final Baz baz){
final Query query = getSessionFactory().getCurrentSession().createQuery(
"select c.id, foo.someValue from Foo as foo "+
"join foo.a as a"+
"join foo.b as b "+
"join b.c as c "+
"where baz=:baz"
);
query.setParameter("baz", baz);
Long start=System.currentTimeMillis();
final List<Object[]> list = query.list();
Long end=System.currentTimeMillis();
System.out.println((end-start));
}
データベースに送信される実際のクエリを取得するために、休止状態のデバッグをオンに設定しました。そのクエリをデータベースで直接実行したところ、0.015ミリ秒で23,000行が返されました。だから、クエリは問題ではないと思います。上記の例は、そのリストを作成するのに約32秒かかることを示しています。それをスピードアップするためにできることはありますか?
更新:休止状態のデバッグクエリを使用してcreateSQLQuery()メソッドを使用しようとしましたが、createQuery()メソッドと同じくらい遅く実行されました。
更新:ステートレスセッションを使用しようとしましたが、実行速度が遅くなりました。
更新:いくつかの統計を出力しました(hibernate.generate_statisticsフラグをtrueに設定)が、何も心配する必要はありません:
Hibernate SessionFactory Statistics [
Number of connection requests[4]
Number of flushes done on the session (either by client code or by hibernate[3]
The number of completed transactions (failed and successful).[3]
The number of transactions completed without failure[3]
The number of sessions your code has opened.[4]
The number of sessions your code has closed.[3]
Total number of queries executed.[4]
Time of the slowest query executed.[28258]
the number of collections fetched from the DB.[6]
The number of collections loaded from the DB.[6]
The number of collections that were rebuilt[0]
The number of collections that were 'deleted' batch.[0]
The number of collections that were updated batch.[0]
The number of your objects deleted.[0]
The number of your objects fetched.[1]
The number of your objects actually loaded (fully populated).[204]
The number of your objects inserted.[1]
The number of your object updated.[0]
]
Hibernate SessionFactory Query Statistics [
total hits on cache by this query[0]
total misses on cache by this query[0]
total number of objects put into cache by this query execution[0]
Number of times this query has been invoked[1]
average time for invoking this query.[28258]
maximum time incurred by query execution[28258]
minimum time incurred by query execution[28258]
Number of rows returned over all invocations of this query[23303]
]
更新:ネイティブクエリのScrollableResultsからnext()を実行すると、同じ速度が低下します。ループでは何もしていないことに注意してください。
ScrollableResults results = query.scroll();
Long start=System.currentTimeMillis();
while (results.next()) {
//do nothing
}
Long end=System.currentTimeMillis();
System.out.println((end-start));