简单的hibernate查询返回非常缓慢 [英] Simple hibernate query returning very slowly
问题描述
Query query = session.createQuery(from MyHibernateClass);
List< MyHibernateClass> result = query.list(); //执行时间为7000ms
我看到
select
myhibernat0_.myFirstColumn as myfirstcolumn92_,
myhibernat0_.mySecondColumn as mysecondcolumn92_,
myhibernat0_.mythirdcolumn as mythirdcolumn92_,
myhibernat0_.myFourthColumn as myfourthcolumn92_
from MyHibernateClass myhibernat0_
where(1 = 1);
当在MyHibernateClass数据库表中测量3500行的小数据集中的jvm中的java代码时,大约7000ms。
如果我在另一方面使用直接jdbc,如下所示:
Statement statement = session.connection()。createStatement();
ResultSet rs = statement.executeQuery(select * from MyHibernateClass); // 7ms
List< MyHibernateClass> result = convert(rs); //在20ms内执行
我看到进入数据库的同一个sql但现在Java代码在jvm中花费的时间是7ms。
MyHibernateClass是一个简单的带有getter和setter的java bean类,我没有使用特殊的resulttransformers在这个例子中可以看到。我只需要一个只读的类实例,它不需要连接到hibernate会话。
我宁愿使用hibernate版本但不能接受执行时间。
新增信息:
添加hibernate日志记录后,我看到
[2011-07-07 14:26:26,643] DEBUG [main] [logid:] -
org.hibernate.jdbc.AbstractBatcher.logOpenResults (AbstractBatcher.java:426) -
将打开ResultSet(打开ResultSets:0,全局:0)
后跟3500个以下日志语句
[2011-07-07 14:26:26,649] DEBUG [main] [logid:] -
org.hibernate.loader.Loader.getRow(Loader.java:1197) -
结果行:EntityKey [com.mycom.MyHibernateClass#1]
后跟3500个日志语句,如
[2011-07-07 14:27:06,789] DEBUG [main] [logid:] -
org.hibernate.engin e.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:130) -
解析[com.mycom.MyHibernateClass#1]的关联
[2011-07-07 14:27:06,792] DEBUG [main] [ logid:] -
org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:226) -
完成实体化[com.mycom.MyHibernateClass#1]
这是什么意思?
Hibernate在第一次执行时做了什么,我可以发现吗?
添加一个带有该类所有属性的构造函数的窍门,现在执行时间为70ms休眠查询。以前这个类只有一个没有参数的默认构造函数和一个带有实体id参数的构造函数。
I have the following hibernate query:
Query query = session.createQuery("from MyHibernateClass");
List<MyHibernateClass> result = query.list();// executes in 7000ms
When logging the sql being executed in MySQL I see
select
myhibernat0_.myFirstColumn as myfirstcolumn92_,
myhibernat0_.mySecondColumn as mysecondcolumn92_,
myhibernat0_.mythirdcolumn as mythirdcolumn92_,
myhibernat0_.myFourthColumn as myfourthcolumn92_
from MyHibernateClass myhibernat0_
where (1=1);
When measurering the java code in the jvm on a small dataset of 3500 rows in MyHibernateClass database table this takes about 7000ms.
If I on the otherhand uses direct jdbc as follows:
Statement statement = session.connection().createStatement();
ResultSet rs = statement.executeQuery("select * from MyHibernateClass");// 7ms
List<MyHibernateClass> result = convert(rs);// executes in 20ms
I see the same sql going into the database but now the time spend in the java code in the jvm is 7ms.
The MyHibernateClass is a simple java bean class with getters and setters, I use no special resulttransformers as can be seen in the example. I only need a read-only instance of the class, and it doesn't need to be attached to the hibernate session.
I would rather like to use the hibernate version but cannot accept the execution times.
Added information: After adding hibernate logging I see
[2011-07-07 14:26:26,643]DEBUG [main] [logid: ] -
org.hibernate.jdbc.AbstractBatcher.logOpenResults(AbstractBatcher.java:426) -
about to open ResultSet (open ResultSets: 0, globally: 0)
followed by 3500 of the following log statements
[2011-07-07 14:26:26,649]DEBUG [main] [logid: ] -
org.hibernate.loader.Loader.getRow(Loader.java:1197) -
result row: EntityKey[com.mycom.MyHibernateClass#1]
followed by 3500 log statements like
[2011-07-07 14:27:06,789]DEBUG [main] [logid: ] -
org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:130) -
resolving associations for [com.mycom.MyHibernateClass#1]
[2011-07-07 14:27:06,792]DEBUG [main] [logid: ] -
org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:226) -
done materializing entity [com.mycom.MyHibernateClass#1]
What does this mean?
What is Hibernate doing in the first implementation, how can I find out?
Adding a constructor with all attributes of the class did the trick, now the execution times are 70ms for the hibernate query. Previously the class only had a default constructor without arguments and a constructor with the entity id argument.
这篇关于简单的hibernate查询返回非常缓慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!