Hi Team.
I discovered that when a particular query is executed by hibernate, it takes much longer than if I take the generated sql and run it in a sql client.
for example, after enabled hibernate statics, I can see in the log
However, if I take the generated sql and run it, it only takes max of 1.5 seconds.
Can anybody points me to what are the possible cause of this difference?
My understanding is, the time for executing the JDBC statement is entirely up to the DB right?
If the SQL is the same, how come it’s so slow when it’s executed by hibernate?
Usually, Hibernate operations not only execute the query, but also fetch all the results and materialize objects from that. If you “just” execute the statement, you are not doing the same work that Hibernate does.
Also, do you use parameters with Hibernate but literals in your SQL? That makes a big difference for many DBs. It also makes a difference if you ran the query before, because the DB caches data.
What bothers me is I think this might be because of something wrong with the database view we are trying to hit. But if I take the hibernate generated sql and run it in other sql clients, the response time is less than 1.5 seconds. The code is simple, no joins or anything, you can find the example below. I also tested the pageable request with different sizes, 1,10,100, no matter the size, when executed by hibernate, it takes much longer.
Which makes me wonder, is it possible that the database(in this case, oracle), can deprioritize jdbc connection from hibernate? Otherwise, I don’t have any other explaination