Can anybody recommend the software to analyze/optimize existent queries in the Java/Tomcat application. About 70% of queries are generated by Hibernate. The database is PostgreSQL but I would like to use general solution, not for a particular database
Asked
Active
Viewed 481 times
2
-
4Stop using Hibernate is the best way to optimize the SQL queries. – Jun 06 '13 at 14:42
-
If Hibernate could further optimize the SQL queries it generates, it would. If another product could do it, there's no good reason Hibernate wouldn't. – Christopher Schultz Jun 06 '13 at 15:07
1 Answers
3
There's no magic product for this, no "make my queries go faster".
What you can do is use a profiling tool like Hibernate Profiler to trace your database activity. Hibernate's built-in logging is also useful. See how to measure hibernate performance? for more detail.
These won't give you a recipe to make things go faster; you'll have to look at the hot-spots and:
- see if you can find ways to reduce the query rates through caching or smarter logic;
- reformulate HQL or Criteria queries more efficiently
- Rewrite bad HQL or Criteria queries in native SQL where necessary
- add indexes (where the update cost of adding the index doesn't exceed the benefit of having it for reads); etc.
You can also examine the PostgreSQL logs:
- Enable detailed logging (
log_statement = 'all'
,log_min_duration_statement = 0
) - Install and use pgbadger to get log reports
- Read the log reports
- Look at whether you can improve the query patterns in your app based on the "hot" queries and tables
It can also be useful to:
- install the
auto_explain
module and enable it - Examine the query plans of slow queries
- Identify the bits of your app they came from and see if you can reformulate the query more efficiently, cache where appropriate, or add indexes where the increased write cost doesn't exceed the benefits of the read speed-up.

Community
- 1
- 1

Craig Ringer
- 307,061
- 76
- 688
- 778
-
Thank you. Can you please look at http://stackoverflow.com/questions/16993107/error-in-reading-postgres-log-by-pgfouine-php. I could not make pgfouine working. Can pgbadger and auto_explain substitute pgfouine. Can Hibernate profile work with not hibernate queries? – Alex Jun 08 '13 at 20:26
-
@anarinsky Pgbadger is an updated and improved version of PgFouine. As for hibernate profile I really doubt it can work with queries not going via Hibernate. – Craig Ringer Jun 09 '13 at 01:42
-
1@anarinsky Also, why're you asking me but not replying to Daniel's question on the post you link to? We don't ask for more info just for fun. – Craig Ringer Jun 09 '13 at 01:43
-