Quote:
You cleared the db cache between each iteration??? I doubt that. Remember, the first iteration will populate the cache.
There are no multiple iterations here. The query is run just once. What I mean't to say is that I ran the test multiple times and before each run I would clear the DB cache to get realistic measures.
Quote:
For a system with no concurrency, performance of simple queries is irrelevant
I hear this in every thread where same one dares measure simple selects and inserts. In my application, and in many others, it is very relevant. When we need to process 250 - 400 million rows for multiple queries this adds up to hours ! Stored procedures is not an option for various reasons.
Perhaps you find it strange that such applications even consider Hibernate as an option :)
Quote:
Your test is broken, then. On my box, the overhead of this query is within 10-15%
Possibly - but I can't figure out where. My code is simple enough - as posted. The final measurement I have come up with is a difference of ~60% with JDBC being faster.
I also noticed that the execution of the statement on the database has a lot to do with the difference in the speed. Hibernate generates a PreparedStatement whereas in this case JDBC is issuing a regular statement. The execution time on DB difference is around 60 % - this accounts for a significant difference in the time measured.
So I did a test in JDBC itself between issuing regular Statements and PreparedStatements and the difference is ~50%. It seems to take a farim amount of 40 iterations for a Statement object to catch up with PreparedStatement i.e if a query is not likely to be used multiple times use a Statement object instead of PreparedStatement. I do not know if I can get Hibernate to use a StatementObject instead of PreparedStatement. I possibly can with a CreateSQLQuery but then I would lose the optimization Gavin suggested for session cache.
Quote:
P.S. Note that for this test case, Hibernate is pretty much guaranteed to actually beat direct JDBC, since you can use the query cache. Call Query.setCacheable(true), and set hibernate.cache.use_query_cache.
This didn't benefit me in this use case because I run the query just once.
Quote:
It is not equivalent, test results must be very different if you will add objects to list (depends on list size).
I made this change in the latest measurements. This had a 20% effect.