1、只查询需要的数据字段
2、优化数据库往返次数
3、查询只读
Session session = entityManager.unwrap(Session.class);
session.setDefaultReadOnly(true);
List<Post> posts = entityManager.createQuery(
"select p from Post p", Post.class)
.setHint(QueryHints.HINT_READONLY, true)
.getResultList();
4、声明批处理
<property name="hibernate.order_inserts" value="true"/>
<property name="hibernate.order_updates" value="true"/>
5、使用映射关系
- The best way to map a @OneToMany relationship with JPA and Hibernate
- The best way to map a @OneToOne relationship with JPA and Hibernate
- The best way to use the @ManyToMany annotation with JPA and Hibernate
- The best way to map a many-to-many association with extra columns when using JPA and Hibernate
6、数据库级别的处理
- How to call Oracle stored procedures and functions with JPA and Hibernate
- How to call SQL Server stored procedures and functions with JPA and Hibernate
- How to call PostgreSQL functions (stored procedures) with JPA and Hibernate
- How to call MySQL stored procedures and functions with JPA and Hibernate
7、缓存处理
re details about how to use the Hibernate 2nd-level cache, check out these articles:
- How does Hibernate store second-level cache entries
- How does Hibernate READ_ONLY CacheConcurrencyStrategy work
- How does Hibernate NONSTRICT_READ_WRITE CacheConcurrencyStrategy work
- How does Hibernate READ_WRITE CacheConcurrencyStrategy work
- How does Hibernate TRANSACTIONAL CacheConcurrencyStrategy work
- How does Hibernate Collection Cache work
- How does Hibernate Query Cache work
- How to use the Hibernate Query Cache for DTO projections
- How to avoid the Hibernate Query Cache N+1 issue
- How to cache non-existing entity fetch results with JPA and Hibernate
8、查询计划缓存
配置Hibernate时另一个鲜为人知的主题是查询计划缓存。 需要解析所有实体查询(例如JPQL或Criteria API)以生成正确的SQL语句。 这个解析实体查询的过程需要时间,因此Hibernate提供了一个计划缓存来重用已经计算的计划。
如果您的应用程序生成许多查询,则正确配置查询计划缓存非常重要。 有关更多详细信息,请查看此文章。
参考地址:https://vladmihalcea.com/hibernate-performance-tuning-tips/