Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Hibernate performance
tuning
Mikalai Alimenkou/Igor Dmitriev
http://xpinjection.com
@xpinjection
Disclaimer
This is only my
personal experience
You could follow code samples
https://github.com/xpinjection/hibernate-performance
What Hibernate really does?
Full execution chain
How to measure
• Hibernate statistics with
• Benchmarks with JMH:
– http://openjdk.java.net/projects/code-tools/jmh/
How to understand and trace
• Datasource proxy:
– https://github.com/ttddyy/datasource-proxy
– https://github.com/p6spy/p6...
Demo database schema
JDBC tuning
• Use good implementation of connection pool
• Batching, fetching
• Switch to native queries where performance...
N + 1, lazy loading issues
• Eager load
• Entity graphs
• Fetch modes
• Fetch profiles
• Query batching
Operations batching
• Flush Session state to avoid memory issues
• Use JDBC batching options
in order to mix statements fo...
Second level cache
Cache providers and strategies
Query cache
Mark lightweight requests
• Read-only flag for transactions
• Choose appropriate transaction isolation level
• Annotate en...
Don’t use entities everywhere
• ResultTransformer and Transformers
• Fields map instead of object
• @SqlResultSetMapping
•...
Don’t update all columns
• Dynamic insert, update
• Update only changed columns
• Be careful with number of PreparedStatem...
Slow ID generation
• Id generator mapping
• Don’t use GenerationType.AUTO, use
GenerationType.SEQUENCE instead
• Try to ge...
Other advices
• Use native queries if really helpful:
– “ORDER BY NULL” or “DELETE with JOIN” in
MySQL
– Effective data re...
Start thinking in CQRS way
When we really need to use ORM?
Resources
• https://vladmihalcea.com/presentations/
• https://vladmihalcea.com/tutorials/hibernate/
• http://jeeconf.com/a...
Books to read
@xpinjection
http://xpinjection.com
mikalai.alimenkou@xpinjection.com
ihor.dmitriiev@gmail.com
Hibernate performance tuning
Hibernate performance tuning
Hibernate performance tuning
Hibernate performance tuning
Hibernate performance tuning
Hibernate performance tuning
Hibernate performance tuning
Hibernate performance tuning
Hibernate performance tuning
Hibernate performance tuning
Hibernate performance tuning
Hibernate performance tuning
Upcoming SlideShare
Loading in …5
×

Hibernate performance tuning

4,463 views

Published on

Many Java developers use ORM in their projects but most of them don’t do deep dive into configuration, settings and tuning to achieve really good performance. What is worse most of them even don’t know what options do they have to improve performance. In this talk we will review them on practical samples and give concrete recommendations how to make your Hibernate work much better in real projects.

Code samples can be found here: https://github.com/xpinjection/hibernate-performance
.

Published in: Technology
  • Be the first to comment

Hibernate performance tuning

  1. 1. Hibernate performance tuning Mikalai Alimenkou/Igor Dmitriev http://xpinjection.com @xpinjection
  2. 2. Disclaimer This is only my personal experience
  3. 3. You could follow code samples https://github.com/xpinjection/hibernate-performance
  4. 4. What Hibernate really does?
  5. 5. Full execution chain
  6. 6. How to measure • Hibernate statistics with • Benchmarks with JMH: – http://openjdk.java.net/projects/code-tools/jmh/
  7. 7. How to understand and trace • Datasource proxy: – https://github.com/ttddyy/datasource-proxy – https://github.com/p6spy/p6spy • Queries counting interceptor: – https://github.com/vladmihalcea/db-util • Log and format SQL statements
  8. 8. Demo database schema
  9. 9. JDBC tuning • Use good implementation of connection pool • Batching, fetching • Switch to native queries where performance is critical • Do direct JDBC stuff with Session doWork
  10. 10. N + 1, lazy loading issues • Eager load • Entity graphs • Fetch modes • Fetch profiles • Query batching
  11. 11. Operations batching • Flush Session state to avoid memory issues • Use JDBC batching options in order to mix statements for different tables • Deletes are batched only for the same table
  12. 12. Second level cache
  13. 13. Cache providers and strategies
  14. 14. Query cache
  15. 15. Mark lightweight requests • Read-only flag for transactions • Choose appropriate transaction isolation level • Annotate entity as immutable if it is not updated anywhere • Use StatelessSession for some operations
  16. 16. Don’t use entities everywhere • ResultTransformer and Transformers • Fields map instead of object • @SqlResultSetMapping • Direct mapping on DTO
  17. 17. Don’t update all columns • Dynamic insert, update • Update only changed columns • Be careful with number of PreparedStatement
  18. 18. Slow ID generation • Id generator mapping • Don’t use GenerationType.AUTO, use GenerationType.SEQUENCE instead • Try to generate ID/PK in the code, not delegate to Hibernate
  19. 19. Other advices • Use native queries if really helpful: – “ORDER BY NULL” or “DELETE with JOIN” in MySQL – Effective data removal • Don’t use lists, arrays and ordering in Hibernate
  20. 20. Start thinking in CQRS way
  21. 21. When we really need to use ORM?
  22. 22. Resources • https://vladmihalcea.com/presentations/ • https://vladmihalcea.com/tutorials/hibernate/ • http://jeeconf.com/archive/jeeconf- 2012/materials/hibernate-performance/ • https://docs.jboss.org/hibernate/orm/5.0/manual/en- US/html_single/#performance • http://jeeconf.com/program/hibernate-how-the-magic- is-really-done/ • http://jeeconf.com/program/hibernate-performance- tuning/
  23. 23. Books to read
  24. 24. @xpinjection http://xpinjection.com mikalai.alimenkou@xpinjection.com ihor.dmitriiev@gmail.com

×