Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to improve Hibernate performance in Java?

Tags:

java

hibernate

I have thousands of records which need to be retrieved on a single user click. Currently it is giving me the results very slowly, I have to wait for a long time. Is there a way to improve retrieving these results using hibernate?

I have a case where a method which has a select query will run every single minute. This is where Hibernate is giving me the slow result. I am using Hibernate with MySQL.

like image 687
Deval Sheth Avatar asked May 25 '12 10:05

Deval Sheth


People also ask

Why is Hibernate so slow?

Depending on the number of selected Order entities, Hibernate might need to execute a huge number of queries that significantly slow down your application. This issue is easy to find. Hibernate's session statistics and Retrace provide you with the number of queries that were executed within a given session.

Is Hibernate slower than JDBC?

Both Hibernate & JDBC facilitate accessing relational tables with Java code. Hibernate is a more efficient & object-oriented approach for accessing a database. However, it is a bit slower performance-wise in comparison to JDBC.

What is the problem with Hibernate?

This problem occurs when Hibernate performs 1 query to select n entities and then has to perform an additional query for each of them to initialize a lazily fetched association. Hibernate fetches lazy relationships transparently so that this kind of problem is hard to find in your code.


1 Answers

Common practice is enabling of 2nd level cache and query cache. Than your data will be readed from memmory not from db.

Good article about it here

Other things can be helpfull:

1 Indexing - in case there is where and ordering - you have to build indexes for fields you are searching/ordering - this can improve search speed up 10 times

2 Denormalisation - if you have a lot of joins some denormalisation (putting all in single table) can help. But this should be final solution when everything else fails.

like image 101
alexey28 Avatar answered Sep 20 '22 17:09

alexey28