Upload
igor-dmitriev
View
66
Download
0
Embed Size (px)
Citation preview
• 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
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
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
- Batch size = 50
[50, 25, 12, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
• “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”)
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
Read-only • HQL constructor Expressions, Criteria
Projections, ResultSetTransformer for read-only data
• @Immutable
• @Transactional(readOnly=true)
• 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