简单的休眠查询返回非常缓慢 [英] Simple hibernate query returning very slowly

查看:31
本文介绍了简单的休眠查询返回非常缓慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下休眠查询:

Query query = session.createQuery("from MyHibernateClass");
List<MyHibernateClass> result = query.list();// executes in 7000ms

记录在 MySQL 中执行的 sql 时,我看到

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);

在 MyHibernateClass 数据库表中 3500 行的小数据集上测量 jvm 中的 java 代码时,这需要大约 7000 毫秒.

When measurering the java code in the jvm on a small dataset of 3500 rows in MyHibernateClass database table this takes about 7000ms.

如果我在另一方面使用直接 jdbc 如下:

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

我看到同样的 sql 进入数据库,但现在在 jvm 中的 java 代码花费的时间是 7ms.

I see the same sql going into the database but now the time spend in the java code in the jvm is 7ms.

MyHibernateClass 是一个带有 getter 和 setter 的简单 java bean 类,我没有使用特殊的 resulttransformers,如示例中所示.我只需要一个只读的类实例,不需要附加到休眠会话中.

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)

后面跟着 3500 条日志语句

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]

之后是 3500 条日志语句,例如

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]

这是什么意思?

Hibernate 在第一个实现中是做什么的,我怎样才能知道?

What is Hibernate doing in the first implementation, how can I find out?

推荐答案

添加具有类的所有属性的构造函数成功了,现在休眠查询的执行时间为 70 毫秒.以前该类只有一个没有参数的默认构造函数和一个带有实体 id 参数的构造函数.

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.

这篇关于简单的休眠查询返回非常缓慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆