Java – the usual hibernate performance trap
We have just finished configuring our application She's starting to slow down
This is a legacy mapping Who works, do the job So is the relationship theme behind it
But some requests are slow
Therefore, we will thank you for any common wrong input about hibernate, which eventually leads to slow response
For example, desire instead of laziness can greatly change response time
Editor: as usual, reading manuals is often a good idea The entire chapter covers this topic:
http://docs.jboss.org/hibernate/core/3.3/reference/en/html/performance.html
Solution
One of the most common defects is the notorious n + 1 selections problem By default, hibernate will not load data you do not require This reduces memory consumption, but exposes problems that you can avoid by switching to the correct extraction strategy to retrieve all data that needs to load objects into the appropriate initialization state
But not too much, otherwise you will encounter the opposite problem, the Cartesian product problem: instead of executing many SQL statements, you may eventually create statements that retrieve too much data
This is the whole point of tuning: find an intermediate value between insufficient and too much data for each use case (or at least the use case that needs to be adjusted) of the application
My suggestion:
>First, activate SQL logging at the hibernate level > Run key use cases (20% use 80% of the time), and even all these are if you have such luxuries, identify suspicious queries, optimize the extraction plan, check whether the index is used properly, etc. > involve your DBA