45
Hibernate performance tuning by Igor Dmitriev

Hibernate performance tuning - JUGjug.ua/wp-content/uploads/2016/04/Hibernate-Performance-tuning.pdf · Hibernate has to find out if foreign key exists to create proxy. You can simply

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Hibernate performance tuning - JUGjug.ua/wp-content/uploads/2016/04/Hibernate-Performance-tuning.pdf · Hibernate has to find out if foreign key exists to create proxy. You can simply

Hibernate performance tuning by Igor Dmitriev

Page 2: Hibernate performance tuning - JUGjug.ua/wp-content/uploads/2016/04/Hibernate-Performance-tuning.pdf · Hibernate has to find out if foreign key exists to create proxy. You can simply
Page 3: Hibernate performance tuning - JUGjug.ua/wp-content/uploads/2016/04/Hibernate-Performance-tuning.pdf · Hibernate has to find out if foreign key exists to create proxy. You can simply

In the beginning

• The most difficult to configure an application

• Default settings.

Page 4: Hibernate performance tuning - JUGjug.ua/wp-content/uploads/2016/04/Hibernate-Performance-tuning.pdf · Hibernate has to find out if foreign key exists to create proxy. You can simply

• The huge amount of data stored in database has a great impact on performance.

• Customers have major concerns over slow performance of the application.

Page 5: Hibernate performance tuning - JUGjug.ua/wp-content/uploads/2016/04/Hibernate-Performance-tuning.pdf · Hibernate has to find out if foreign key exists to create proxy. You can simply

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

Page 6: Hibernate performance tuning - JUGjug.ua/wp-content/uploads/2016/04/Hibernate-Performance-tuning.pdf · Hibernate has to find out if foreign key exists to create proxy. You can simply

Most popular reasons

Page 7: Hibernate performance tuning - JUGjug.ua/wp-content/uploads/2016/04/Hibernate-Performance-tuning.pdf · Hibernate has to find out if foreign key exists to create proxy. You can simply

Slow running queries

Page 8: Hibernate performance tuning - JUGjug.ua/wp-content/uploads/2016/04/Hibernate-Performance-tuning.pdf · Hibernate has to find out if foreign key exists to create proxy. You can simply

There are lots of queries

Page 9: Hibernate performance tuning - JUGjug.ua/wp-content/uploads/2016/04/Hibernate-Performance-tuning.pdf · Hibernate has to find out if foreign key exists to create proxy. You can simply

Database locks

Page 10: Hibernate performance tuning - JUGjug.ua/wp-content/uploads/2016/04/Hibernate-Performance-tuning.pdf · Hibernate has to find out if foreign key exists to create proxy. You can simply

Settings

Hibernate : 5.0.0.Final DB : Postgresql

Java : 8

Page 11: Hibernate performance tuning - JUGjug.ua/wp-content/uploads/2016/04/Hibernate-Performance-tuning.pdf · Hibernate has to find out if foreign key exists to create proxy. You can simply
Page 12: Hibernate performance tuning - JUGjug.ua/wp-content/uploads/2016/04/Hibernate-Performance-tuning.pdf · Hibernate has to find out if foreign key exists to create proxy. You can simply
Page 13: Hibernate performance tuning - JUGjug.ua/wp-content/uploads/2016/04/Hibernate-Performance-tuning.pdf · Hibernate has to find out if foreign key exists to create proxy. You can simply

1. @OneToOne is not working?

Page 14: Hibernate performance tuning - JUGjug.ua/wp-content/uploads/2016/04/Hibernate-Performance-tuning.pdf · Hibernate has to find out if foreign key exists to create proxy. You can simply
Page 15: Hibernate performance tuning - JUGjug.ua/wp-content/uploads/2016/04/Hibernate-Performance-tuning.pdf · Hibernate has to find out if foreign key exists to create proxy. You can simply

There are several possible mappings for one-to-one associations

• Sharing Primary key

- required @PrimaryKeyJoinColumn and @OneToOne(fetch = FetchType.LAZY, optional = false)

Page 16: Hibernate performance tuning - JUGjug.ua/wp-content/uploads/2016/04/Hibernate-Performance-tuning.pdf · Hibernate has to find out if foreign key exists to create proxy. You can simply

• Lazy loading with bytecode instrumentation

• Using a Join Table with mapping table

• @OneToMany instead of @OneToOne trick

Page 17: Hibernate performance tuning - JUGjug.ua/wp-content/uploads/2016/04/Hibernate-Performance-tuning.pdf · Hibernate has to find out if foreign key exists to create proxy. You can simply

@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

Page 18: Hibernate performance tuning - JUGjug.ua/wp-content/uploads/2016/04/Hibernate-Performance-tuning.pdf · Hibernate has to find out if foreign key exists to create proxy. You can simply

• Do you really need a separate table?

- one table for one-to-one mapping

- use @Embeddable

Page 19: Hibernate performance tuning - JUGjug.ua/wp-content/uploads/2016/04/Hibernate-Performance-tuning.pdf · Hibernate has to find out if foreign key exists to create proxy. You can simply

2. EAGER is not working?

Page 20: Hibernate performance tuning - JUGjug.ua/wp-content/uploads/2016/04/Hibernate-Performance-tuning.pdf · Hibernate has to find out if foreign key exists to create proxy. You can simply
Page 21: Hibernate performance tuning - JUGjug.ua/wp-content/uploads/2016/04/Hibernate-Performance-tuning.pdf · Hibernate has to find out if foreign key exists to create proxy. You can simply

3. N+1

Page 22: Hibernate performance tuning - JUGjug.ua/wp-content/uploads/2016/04/Hibernate-Performance-tuning.pdf · Hibernate has to find out if foreign key exists to create proxy. You can simply
Page 23: Hibernate performance tuning - JUGjug.ua/wp-content/uploads/2016/04/Hibernate-Performance-tuning.pdf · Hibernate has to find out if foreign key exists to create proxy. You can simply

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.

Page 24: Hibernate performance tuning - JUGjug.ua/wp-content/uploads/2016/04/Hibernate-Performance-tuning.pdf · Hibernate has to find out if foreign key exists to create proxy. You can simply

• Prefetching data in batches

@BatchSize(size=10)

- blind-guess optimization

Page 25: Hibernate performance tuning - JUGjug.ua/wp-content/uploads/2016/04/Hibernate-Performance-tuning.pdf · Hibernate has to find out if foreign key exists to create proxy. You can simply

• 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]

Page 26: Hibernate performance tuning - JUGjug.ua/wp-content/uploads/2016/04/Hibernate-Performance-tuning.pdf · Hibernate has to find out if foreign key exists to create proxy. You can simply

• HQL, Criteria or SQL fetch join

Page 27: Hibernate performance tuning - JUGjug.ua/wp-content/uploads/2016/04/Hibernate-Performance-tuning.pdf · Hibernate has to find out if foreign key exists to create proxy. You can simply

• Hibernate Fetch Profile

Page 28: Hibernate performance tuning - JUGjug.ua/wp-content/uploads/2016/04/Hibernate-Performance-tuning.pdf · Hibernate has to find out if foreign key exists to create proxy. You can simply

• EntityGraph

- JPA 2.1

Page 29: Hibernate performance tuning - JUGjug.ua/wp-content/uploads/2016/04/Hibernate-Performance-tuning.pdf · Hibernate has to find out if foreign key exists to create proxy. You can simply

4. Cartesian product

Page 30: Hibernate performance tuning - JUGjug.ua/wp-content/uploads/2016/04/Hibernate-Performance-tuning.pdf · Hibernate has to find out if foreign key exists to create proxy. You can simply
Page 31: Hibernate performance tuning - JUGjug.ua/wp-content/uploads/2016/04/Hibernate-Performance-tuning.pdf · Hibernate has to find out if foreign key exists to create proxy. You can simply

• “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

Page 32: Hibernate performance tuning - JUGjug.ua/wp-content/uploads/2016/04/Hibernate-Performance-tuning.pdf · Hibernate has to find out if foreign key exists to create proxy. You can simply

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”)

Page 33: Hibernate performance tuning - JUGjug.ua/wp-content/uploads/2016/04/Hibernate-Performance-tuning.pdf · Hibernate has to find out if foreign key exists to create proxy. You can simply

Keys

Page 34: Hibernate performance tuning - JUGjug.ua/wp-content/uploads/2016/04/Hibernate-Performance-tuning.pdf · Hibernate has to find out if foreign key exists to create proxy. You can simply

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

Page 35: Hibernate performance tuning - JUGjug.ua/wp-content/uploads/2016/04/Hibernate-Performance-tuning.pdf · Hibernate has to find out if foreign key exists to create proxy. You can simply
Page 36: Hibernate performance tuning - JUGjug.ua/wp-content/uploads/2016/04/Hibernate-Performance-tuning.pdf · Hibernate has to find out if foreign key exists to create proxy. You can simply

Read-only • HQL constructor Expressions, Criteria

Projections, ResultSetTransformer for read-only data

• @Immutable

• @Transactional(readOnly=true)

Page 37: Hibernate performance tuning - JUGjug.ua/wp-content/uploads/2016/04/Hibernate-Performance-tuning.pdf · Hibernate has to find out if foreign key exists to create proxy. You can simply

Caching data

• Second-level cache (Reference data is an excellent candidate for shared caching)

Page 38: Hibernate performance tuning - JUGjug.ua/wp-content/uploads/2016/04/Hibernate-Performance-tuning.pdf · Hibernate has to find out if foreign key exists to create proxy. You can simply

• 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)

Page 39: Hibernate performance tuning - JUGjug.ua/wp-content/uploads/2016/04/Hibernate-Performance-tuning.pdf · Hibernate has to find out if foreign key exists to create proxy. You can simply

Bytecode enhancement

• @Basic(fetch=FetchType.LAZY)

- Blob

- @Formula

- Dirty checking enhancement

Hibernate 5.0+ version

Page 40: Hibernate performance tuning - JUGjug.ua/wp-content/uploads/2016/04/Hibernate-Performance-tuning.pdf · Hibernate has to find out if foreign key exists to create proxy. You can simply
Page 41: Hibernate performance tuning - JUGjug.ua/wp-content/uploads/2016/04/Hibernate-Performance-tuning.pdf · Hibernate has to find out if foreign key exists to create proxy. You can simply

The most useful recommendation

• Control your queries

Page 42: Hibernate performance tuning - JUGjug.ua/wp-content/uploads/2016/04/Hibernate-Performance-tuning.pdf · Hibernate has to find out if foreign key exists to create proxy. You can simply

Useful references

• Hibernate documentation

Page 43: Hibernate performance tuning - JUGjug.ua/wp-content/uploads/2016/04/Hibernate-Performance-tuning.pdf · Hibernate has to find out if foreign key exists to create proxy. You can simply

• JPA 2.1 specification

Page 44: Hibernate performance tuning - JUGjug.ua/wp-content/uploads/2016/04/Hibernate-Performance-tuning.pdf · Hibernate has to find out if foreign key exists to create proxy. You can simply

• Java Persistence with Hibernate 2nd edition 2016

Page 45: Hibernate performance tuning - JUGjug.ua/wp-content/uploads/2016/04/Hibernate-Performance-tuning.pdf · Hibernate has to find out if foreign key exists to create proxy. You can simply

Questions?