Speeding up Hibernate object creation?

We use Hibernate as our ORM layer on top of the MySQL database. We have quite a few model objects, some of which are quite large (in terms of the number of fields, etc.). Some of our queries require that many (if not all) model objects be retrieved from the database in order to perform various calculations on them.

We have lazy loading enabled, but in some cases it takes a lot of time for Hibernate to fill objects. MySQL query execution time is very fast (on the order of a few milliseconds), but then Hibernate takes its sweet time to populate the objects.

Is there a way / template / optimization to speed up this process?

Thanks.

+4
source share
3 answers

One approach is not to fill an object, but some kind of object of the form.

Assuming CustomerView has an appropriate constructor, you can do

select new CustomerView(c.firstname, c.lastname, c.age) from Customer c 

Although I’m a little surprised that Hibernate is slowly populating objects, unless you cascade related objects and forget a few suitable sets.

+2
source

Maybe you should add a second level cache? This does not necessarily speed up the creation of the object, but can significantly reduce the frequency at which you need to do this.

http://docs.jboss.org/hibernate/core/3.3/reference/en/html/performance.html

+1
source

Since you are asking a question related to performance, you can collect more data about where the bottleneck is. You speak

Sleep mode takes its sweet time to fill objects.

How do you know hibernate what's the problem? In other words, is the Hibernate problem on its own, or may there be insufficient memory (or too much) to prevent the JVM from working efficiently?

In addition, you mentioned

We have quite a few model objects, some of which are quite large (in terms of the number of fields, etc.).

How many are "pretty big"? Dozens? Hundreds? Thousands? This is important because relational databases (like MySQL) start to work worse, as your table becomes β€œwider” (see This question: Is there a performance hit if there are too many columns in the table? ).

Performance is a lot about balancing limitations, but also about collecting a lot of data to see where the problem is, and then fix the problem. Then you will find the following bottleneck and fix it until your performance is good enough or you run out of implementation time.

+1
source

All Articles