在休眠状态下快速获得结果的方法?
我目前在我的项目中设置了休眠。它适用于大多数事情。然而今天我需要一个查询从表中返回几十万行。它大约占表中总行数的 2/3。问题是查询大约需要 7 分钟。使用直接 JDBC 并执行我假设的相同查询,需要 << 20秒。因此,我认为我正在做一些完全错误的事情。我将在下面列出一些代码。
DetachedCriteria criteria =DetachedCriteria.forlass(MyObject.class);
criteria.add(Restrictions.eq("booleanFlag", false));
List<MyObject> list = getHibernateTemplate().findByCriteria(criteria);
关于为什么它会很慢和/或我可以做些什么来改变它有什么想法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能已经回答了自己的问题,直接使用 JDBC。
Hibernate 充其量是为每一行创建某个
Object
的实例,或更糟糕的是,创建多个Object< /code> 每行的实例。 Hibernate 有一些非常退化的代码生成和实例化行为,这些行为很难控制,尤其是对于大型数据集,如果启用了任何缓存选项,情况会更糟。
Hibernate 不适合大型结果集,并且将数十万行作为对象处理也不是非常注重性能。
原始 JDBC 就是行列的原始类型。数据量减少了几个数量级。
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, multipleObject
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.
如果您需要提取数十万条记录,我不确定使用 hibernate 是否正确。查询执行时间可能不到 20 秒,但获取时间会很长并且消耗大量内存。获得所有这些记录后,如何输出它们?它的数据量远远超过您可以向用户显示的数据量。对于进行数据仓库类型的数据处理,Hibernate 并不是一个真正好的解决方案。
I'm not sure hibernate is the right thing to use if you need to pull hundreds of thousands of records. The query execute time might be under 20 seconds but the fetch time will be huge and consume a lot of memory. After you get all those records, how do you output them? It's far more data than you could display to a user. Hibernate isn't really a good solution for doing data wharehouse style data crunching.
可能您在 MyObject 类中对其他类有多个引用,并且在您的映射中设置了预加载或类似的内容。使用您编写的代码很难找到问题,因为它没问题。
也许使用 Hibernate Profiler 会更好 - http://hibernateprofiler.com/ 。它将向您显示映射、配置和查询的所有问题。
Probably you have several references to other classes in your MyObject class and in your mapping you set eager loading or something like that. It's very hard to find the issue using the code you wrote because it's OK.
Probably it will be better for you to use Hibernate Profiler - http://hibernateprofiler.com/ . It will show you all the problems with your mappings, configurations and queries.