Skip to content
Advertisement

Why are my Hibernate `select` queries super slow?

I am using Hibernate in my Java based REST API. I am using HQL mainly because it is easy. Also for this project, I am loading data from the main table, its associated tables, their associated tables and so on. The reason is then in the app, I have all the data I need in a one JSON string instead of running multiple methods.

Now, take a look at the following database structure.

enter image description here

Let’s take a look at the code. I am going to run getAll method on Stock. It will return me all the stock data + its product data + product’s vehicle_model data+ product’s spare_part data

I am just posting the data grab code.

Hibernate took 40+ seconds to return me the data! I also noticed the more data I have the more time it will take.

Below are my entities

Stock

VehicleModel

SparePart

This is just a part of the SQL output made by the Hibernate, you can see it tried to take data from each table by trying to execute SQL Queries individually.

If this is normal SQL this will use Joins. How can I fix this issue and get the data as fast as possible? More importantly any way to tell Hibernate to run the query as a single unit, and use joins or something?

Advertisement

Answer

Answer is to use joins.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement