查询是否将所有数据加载到内存中 [英] Do query loads all the data in memory
问题描述
无论何时执行查询,Hibernate/JPA都会加载内存中的所有数据吗?它是自己进行某种优化还是具有某种机制来批量获取查询数据?
Whenever we execute a query does Hibernate/JPA load all the data in memory? Does it do some kind of optimization by itself or does it have some kind of mechanism where it fetches the query data in batch?
因此,这里我所关心的是,如果我有一个包含数百万行的表,那么hibernate如何管理这些行,如果我不指定任何优化策略,是否会将所有行立即加载到内存中?
So here just my concern is, if I have a table with millions of rows, how hibernate manages those rows, will all the rows be loaded in memory at once if I don't specify any optimization strategy?
我正在使用Oracle作为数据库.
I am using Oracle as database.
推荐答案
如果使用org.hibernate.Query.list
,则Hibernate会将所有映射的对象加载到内存中.您可以通过使用org.hibernate.Query.scroll
来避免这种情况.完成操作后,您可以在会话中滚动ScrollableResults
并逐出实体.
If you use org.hibernate.Query.list
Hibernate will load all mapped objects into memory. You can avoid this by using org.hibernate.Query.scroll
instead. You can then scroll through the ScrollableResults
and evict entities from your session after you're finished with them.
这样做可以避免将所有实体都加载到内存中,并且可以避免在很大的会话中进行脏检查.
Doing this will avoid loading all the entities into memory and will avoid issues with dirty checking a very large session.
这篇关于查询是否将所有数据加载到内存中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!