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
.
9. How to measure
• Hibernate statistics with
• Benchmarks with JMH:
– http://openjdk.java.net/projects/code-tools/jmh/
10. 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
14. 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
17. 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
23. 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
24.
25. Don’t use entities everywhere
• ResultTransformer and Transformers
• Fields map instead of object
• @SqlResultSetMapping
• Direct mapping on DTO
26.
27. Don’t update all columns
• Dynamic insert, update
• Update only changed columns
• Be careful with number of PreparedStatement
28.
29. 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
30. 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
TODO: поставить правильную ссылку и перегенерить QR код
На демо показываем:
как сконфигурировать и показывать статистику
как контролировать количество запросов в тестах
На демо показываем:
N + 1 проблему
разные стратегии вычитки (join, subselect, batching)
применение графов сущностей
blind guess loading (несколько наперед чтобы экономить запросы)
применение фетча для одного запроса !!! объяснить
На демо показываем:
- вставка пачкой
удаление пачкой
обновление пачкой
На демо показываем:
как настроить second level кэш !!! не показывыем
как работает second level кэш
как что кешируется (сущности, коллекции) !!! не показываем
query cache и его взаимодействие с second level cache
На демо показываем:
как отключается dirty checking
пример работы через StatelessSession !!! не показывать
На демо показываем:
примеры трансформации результов
маппинг на DTO прямой
На демо показываем:
- вставку и обновление сущности с включенными флагами