Pergunta

I am facing to problem since I have millions of domain objects in one table. When I try to get all objects by using Domain.findAllBy() after couple of minutes I get OutOfMemoryError.. I would like to know if there is an efficient way to load all of them without getting this error again ?

Should I page the result and only load the necessaries ?

Please tell me if I am doing it wrong as well..

Thank you for your help and happy new year ;)

Foi útil?

Solução

The real solution is not to try to retrieve all domain objects in one go into memory. No matter how much memory you buy, you can't guarantee that the domain objects won't grow more quickly than your RAM.

Next, even if you could store all the objects in memory, it would take a non-trivial amount of time to retrieve them all. Any operations you want to undertake on the objects - modifying attributes, calling methods - would take even longer.

I can't imagine a scenario where a human user would want to see millions of business objects on a web page - even paging through them all doesn't make sense.

So, if you are retrieving the objects to modify them, do so in the database. If you are retrieving them to run a method on the business objects, use paging, or consider if you can implement that method as a database call. If you're retrieving them for display, you'll need to allow the user to filter their request, and provide pagination.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top