SlideShare a Scribd company logo
Hibernate performance tuning
by Igor Dmitriev
Hibernate performance tuning
In the beginning
• The most difficult to configure an application
• Default settings.
• The huge amount of data stored in database has a
great impact on performance.
• Customers have major concerns over slow
performance of the application.
What should I do?
To grap statistics
• Aspect for Service method execution time
• P6Spy for SQL logging
• High load testing
• Be careful :
- Testing in stable state
- Do a few iterations
- DB SQL cache
- Second or Query cache
Most popular reasons
Slow running queries
There are lots of queries
Database locks
Settings
Hibernate : 5.0.0.Final
DB : Postgresql
Java : 8
Hibernate performance tuning
Hibernate performance tuning
1. @OneToOne is not working?
Hibernate performance tuning
There are several possible mappings
for one-to-one associations
• Sharing Primary key
- required @PrimaryKeyJoinColumn and
@OneToOne(fetch = FetchType.LAZY, optional = false)
• Lazy loading with bytecode instrumentation
• Using a Join Table with mapping table
• @OneToMany instead of @OneToOne trick
@OneToOne
• It’s important where FK is.
Hibernate has to find out if foreign key exists to create
proxy. You can simply make the association
unidirectional, keep the link where foreign key resides
• Do you really need a separate table?
- one table for one-to-one mapping
- use @Embeddable
2. EAGER is not working?
Hibernate performance tuning
3. N+1
Hibernate performance tuning
There are a lot of different approaches
to resolve N+1
• Prefetching collections with subselects
@Fetch(FetchMode.SUBSELECT)
- just for collections
- Hibernate remembers the original query used
to load the elements.
• Prefetching data in batches
@BatchSize(size=10)
- blind-guess optimization
• Prefetching data in batches
@BatchSize(size=10)
- blind-guess optimization
- Batch size = 50
[50, 25, 12, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
• HQL, Criteria or SQL fetch join
• Hibernate Fetch Profile
• EntityGraph
- JPA 2.1
4. Cartesian product
Hibernate performance tuning
• “Every time you think have to map a collection
you have to ask yourself why. A mapped
persistent collection is a feature, not a
requirement.”
Christian Bauer Jan 26, 2006
EclipseLink
• @JoinFetch
• query.setHint(“eclipselink.join-fetch”,”client.accounts”)
• @BatchFetch(type=BatchFetchType.EXISTS) (JOIN,IN)
• query.setHint(“eclipselink.batch.type”,”EXISTS”)
• query.setHint(“eclipselink.batch”,”client.accounts”)
Keys
Batch processing
Update or insert huge amount of entity objects
• flush + clear
• ScrollableResults
• StatelessSession
- does not have a persistence context cache and does not
interact with second and query cache, no dirty checking
- all retrieved entities are in detached state
- it bypasses any enabled Interceptor or event listeners or
event callback methods
- no batching
- It’s a lower-level abstraction that is much closer to JDBC
Hibernate performance tuning
Read-only
• HQL constructor Expressions, Criteria
Projections, ResultSetTransformer for read-
only data
• @Immutable
• @Transactional(readOnly=true)
Caching data
• Second-level cache (Reference data is an
excellent candidate for shared caching)
• Query cache
- You must ask yourself how often you’re going
to execute the same query repeatedly, with the
same arguments.
- Use the query cache in conjunction with the
second-level cache !!!
- Query.setCacheable(true)
Bytecode enhancement
• @Basic(fetch=FetchType.LAZY)
- Blob
- @Formula
- Dirty checking enhancement
Hibernate 5.0+ version
Hibernate performance tuning
The most useful recommendation
• Control your queries
Useful references
• Hibernate documentation
• JPA 2.1 specification
• Java Persistence with Hibernate 2nd edition 2016
Questions?

More Related Content

What's hot (20)

PPTX
Practical examples of using extended events
Dean Richards
 
PPTX
SQL Server R Services: What Every SQL Professional Should Know
Bob Ward
 
PDF
DataStax | DataStax Tools for Developers (Alex Popescu) | Cassandra Summit 2016
DataStax
 
PPTX
Oracle: Let My People Go! (Shu Zhang, Ilya Sokolov, Symantec) | Cassandra Sum...
DataStax
 
PPTX
Inside SQL Server In-Memory OLTP
Bob Ward
 
PPTX
Enhancements that will make your sql database roar sp1 edition sql bits 2017
Bob Ward
 
KEY
All your data belong to us - The Active Objects Plugin
Samuel Le Berrigaud
 
KEY
Hibernate performance tuning
Sander Mak (@Sander_Mak)
 
PDF
Caching In The Cloud
Alex Miller
 
PPTX
OUG Scotland 2014 - NoSQL and MySQL - The best of both worlds
Andrew Morgan
 
PDF
Groovy concurrency
Alex Miller
 
PPTX
SQL Server In-Memory OLTP: What Every SQL Professional Should Know
Bob Ward
 
PPTX
Ssis Best Practices Israel Bi U Ser Group Itay Braun
sqlserver.co.il
 
PPT
Persistence hibernate
Krishnakanth Goud
 
PDF
Presto Testing Tools: Benchto & Tempto (Presto Boston Meetup 10062015)
Matt Fuller
 
KEY
Using ActiveObjects in Atlassian Plugins
Atlassian
 
PDF
Ashnik EnterpriseDB PostgreSQL - A real alternative to Oracle
Ashnikbiz
 
PDF
Gradle - Build System
Jeevesh Pandey
 
PPTX
Liquibase for java developers
Illia Seleznov
 
PDF
How We Test MongoDB: Evergreen
MongoDB
 
Practical examples of using extended events
Dean Richards
 
SQL Server R Services: What Every SQL Professional Should Know
Bob Ward
 
DataStax | DataStax Tools for Developers (Alex Popescu) | Cassandra Summit 2016
DataStax
 
Oracle: Let My People Go! (Shu Zhang, Ilya Sokolov, Symantec) | Cassandra Sum...
DataStax
 
Inside SQL Server In-Memory OLTP
Bob Ward
 
Enhancements that will make your sql database roar sp1 edition sql bits 2017
Bob Ward
 
All your data belong to us - The Active Objects Plugin
Samuel Le Berrigaud
 
Hibernate performance tuning
Sander Mak (@Sander_Mak)
 
Caching In The Cloud
Alex Miller
 
OUG Scotland 2014 - NoSQL and MySQL - The best of both worlds
Andrew Morgan
 
Groovy concurrency
Alex Miller
 
SQL Server In-Memory OLTP: What Every SQL Professional Should Know
Bob Ward
 
Ssis Best Practices Israel Bi U Ser Group Itay Braun
sqlserver.co.il
 
Persistence hibernate
Krishnakanth Goud
 
Presto Testing Tools: Benchto & Tempto (Presto Boston Meetup 10062015)
Matt Fuller
 
Using ActiveObjects in Atlassian Plugins
Atlassian
 
Ashnik EnterpriseDB PostgreSQL - A real alternative to Oracle
Ashnikbiz
 
Gradle - Build System
Jeevesh Pandey
 
Liquibase for java developers
Illia Seleznov
 
How We Test MongoDB: Evergreen
MongoDB
 

Similar to Hibernate performance tuning (20)

PDF
Hibernate ORM: Tips, Tricks, and Performance Techniques
Brett Meyer
 
PPTX
Hibernate Performance Tuning @JUG Thüringen
Thorben Janssen
 
PDF
High Performance Hibernate JavaZone 2016
Vlad Mihalcea
 
PDF
Effiziente Datenpersistierung mit JPA 2.1 und Hibernate
Thorben Janssen
 
PPT
Hibernate jj
Joe Jacob
 
PPT
Accelerated data access
gordonyorke
 
PPTX
Effiziente persistierung
Thorben Janssen
 
PPTX
Tips about hibernate with spring data jpa
Thiago Dos Santos Hora
 
PPT
Jpa & hibernate
Jiří Kiml
 
DOC
Advanced Hibernate Notes
Kaniska Mandal
 
PDF
Advance Features of Hibernate
Mindfire Solutions
 
PDF
Tech Talk - JPA and Query Optimization - publish
Gleydson Lima
 
PDF
Hibernate caching
Alex Verdyan
 
PPT
Performance Tuning with JPA 2.1 and Hibernate (Geecon Prague 2015)
Thorben Janssen
 
PPTX
Hibernate performance tuning
Mikalai Alimenkou
 
PPTX
Hibernate in XPages
Toby Samples
 
PDF
Building Fast and Scalable Persistence Layers with Spring Data JPA
VMware Tanzu
 
PPSX
Hibernate - Part 2
Hitesh-Java
 
Hibernate ORM: Tips, Tricks, and Performance Techniques
Brett Meyer
 
Hibernate Performance Tuning @JUG Thüringen
Thorben Janssen
 
High Performance Hibernate JavaZone 2016
Vlad Mihalcea
 
Effiziente Datenpersistierung mit JPA 2.1 und Hibernate
Thorben Janssen
 
Hibernate jj
Joe Jacob
 
Accelerated data access
gordonyorke
 
Effiziente persistierung
Thorben Janssen
 
Tips about hibernate with spring data jpa
Thiago Dos Santos Hora
 
Jpa & hibernate
Jiří Kiml
 
Advanced Hibernate Notes
Kaniska Mandal
 
Advance Features of Hibernate
Mindfire Solutions
 
Tech Talk - JPA and Query Optimization - publish
Gleydson Lima
 
Hibernate caching
Alex Verdyan
 
Performance Tuning with JPA 2.1 and Hibernate (Geecon Prague 2015)
Thorben Janssen
 
Hibernate performance tuning
Mikalai Alimenkou
 
Hibernate in XPages
Toby Samples
 
Building Fast and Scalable Persistence Layers with Spring Data JPA
VMware Tanzu
 
Hibernate - Part 2
Hitesh-Java
 
Ad

Recently uploaded (20)

PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
Blockchain Transactions Explained For Everyone
CIFDAQ
 
PDF
July Patch Tuesday
Ivanti
 
PDF
Python basic programing language for automation
DanialHabibi2
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PPTX
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
PPTX
MSP360 Backup Scheduling and Retention Best Practices.pptx
MSP360
 
PDF
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PDF
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
Blockchain Transactions Explained For Everyone
CIFDAQ
 
July Patch Tuesday
Ivanti
 
Python basic programing language for automation
DanialHabibi2
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
MSP360 Backup Scheduling and Retention Best Practices.pptx
MSP360
 
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
Ad

Hibernate performance tuning