快速获得冬眠结果的方法? [英] Fast way to get results in hibernate?

查看:84
本文介绍了快速获得冬眠结果的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在我的项目中设置了hibernate。它适用于大多数事情。但是今天我需要查询从表中返回几十万行。它是表中总行数的2/3。问题是查询需要7分钟。使用直接JDBC并执行我认为是相同的查询,它需要< 20秒。因此,我认为我正在做一些完全错误的事情。我将在下面列出一些代码。

  DetachedCriteria criteria = DetachedCriteria.forlass(MyObject.class); 
criteria.add(Restrictions.eq(booleanFlag,false));
列表< MyObject> list = getHibernateTemplate()。findByCriteria(criteria);

关于为什么它会变慢和/或我能做些什么来改变它?您可能已经回答了您自己的问题,请使用直接的JDBC。

$ b $

解决方案 b
Hibernate为每行创建一个 Object 的实例,或者更糟的是,多个 Object 每个行的实例。 Hibernate有一些真正退化的代码生成和实例化行为,难以控制,特别是对于大型数据集,如果启用了任何缓存选项,甚至会更糟。



Hibernate不适合大型结果集,并且处理成千上万行作为对象的行也不是很注重性能。



原始JDBC只是行列的原始类型。数据量少的订单。

I currently have hibernate set up in my project. It works well for most things. However today I needed to have a query return a couple hundred thousand rows from a table. It was ~2/3s of the total rows in the table. The problem is the query is taking ~7 minutes. Using straight JDBC and executing what I assumed was an identical query, it takes < 20 seconds. Because of this I assume I am doing something completely wrong. I'll list some code below.

DetachedCriteria criteria  =DetachedCriteria.forlass(MyObject.class);
criteria.add(Restrictions.eq("booleanFlag", false));
List<MyObject> list = getHibernateTemplate().findByCriteria(criteria);

Any ideas on why it would be slow and/or what I could do to change it?

解决方案

You have probably answered your own question already, use straight JDBC.

Hibernate is creating at best an instance of some Object for every row, or worse, multiple Object instances for each row. Hibernate has some really degenerate code generation and instantiation behavior that can be difficult to control, especially with large data sets, and even worse if you have any of the caching options enabled.

Hibernate is not suited for large results sets, and processing hundreds of thousands of rows as objects isn't very performance oriented either.

Raw JDBC is just that raw types for rows columns. Orders of magnitudes of less data.

这篇关于快速获得冬眠结果的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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