Successfully reported this slideshow.
Your SlideShare is downloading. ×

Hibernate performance tuning

Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Loading in …3
×

Check these out next

1 of 36 Ad

Hibernate performance tuning

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
.

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
.

Advertisement
Advertisement

More Related Content

Similar to Hibernate performance tuning (20)

Advertisement

More from Mikalai Alimenkou (20)

Advertisement

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

Editor's Notes

  • TODO: поставить правильную ссылку и перегенерить QR код
  • На демо показываем:

    как сконфигурировать и показывать статистику
    как контролировать количество запросов в тестах
  • На демо показываем:

    N + 1 проблему
    разные стратегии вычитки (join, subselect, batching)
    применение графов сущностей
    blind guess loading (несколько наперед чтобы экономить запросы)
    применение фетча для одного запроса !!! объяснить
  • На демо показываем:

    - вставка пачкой
    удаление пачкой
    обновление пачкой
  • На демо показываем:

    как настроить second level кэш !!! не показывыем
    как работает second level кэш
    как что кешируется (сущности, коллекции) !!! не показываем
    query cache и его взаимодействие с second level cache
  • На демо показываем:

    как отключается dirty checking
    пример работы через StatelessSession !!! не показывать
  • На демо показываем:

    примеры трансформации результов
    маппинг на DTO прямой
  • На демо показываем:

    - вставку и обновление сущности с включенными флагами
  • TODO: добавить визуализацию
  • TODO: добавить еще ресурсов

×