Kukmedis
Kukmedis

Reputation: 85

Why is first HQL query taking longer than other queries?

I am benchmarking JDBC and Hibernate (with JPA) performance and run the same queries several times in a row. I have noticed that the first query (Both with JDBC and Hibernate) runs longer than others.

I have disabled mysql cache, so i do believe it should not be mysql specific. Could it be? I also clear EntityManager between each (HQL) query.

Could someone point me why the first query takes longer than others?

Log: http://pastebin.com/fwNbqaMD

Thank you for your responses

Upvotes: 0

Views: 1114

Answers (2)

sgp15
sgp15

Reputation: 1280

Following can be few possible reasons.

  1. Connection pooling.
  2. Prepared statements.
  3. DB engine warm up / init.

In either cases the first iteration will have more to do than rest.

For example, for prepared statements query plan is built for first time and cached for reuse in subsequent executions.

Upvotes: 1

Sebastian Mach
Sebastian Mach

Reputation: 39109

I guess because of runtime optimizations on your program.

During each run, profiling information of your code is collected, and the JIT optimizer applies optimizations.

Upvotes: 1

Related Questions