使用 spring Jpa 提高休眠性能
Improve hibernate performance with spring Jpa
我有一个休眠问题,当我尝试将我的 object 保存在数据库中时,它看起来非常慢。
所以,关注的模型是这样的:
@Entity
@Table(name = "T_PARENT")
public class ParentEntity implements Serializable {
// Id
// Attributes
// Child relation
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name="xxxx")
private List<ChildEntity> childs;
// Getters & setters
}
@Entity
@Table(name = "T_CHILD")
public class ChildEntity implements Serializable {
// Id
// Attributes
// Child relation
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name="yyyyy")
private List<ChildChildEntity> childs;
// Getters & setters
}
@Entity
@Table(name = "T_CHILD_CHILD")
public class ChildChildEntity implements Serializable {
// Id
// Attributes
// Getters & setters
}
我使用 JpaRepository (spring) 来管理数据库中的数据。
我们在 "parent" 上调用 "save" 方法来持久化 "child" 和 "childChild"(只创建了 parent 的存储库)
数据很少,没有问题(例如:1 parent / 20 child 并且每个 child 有 200 个 child 孩子)
但是当我们有一个重要的卷时,休眠查询是无休止的......(例如:1 parent / 40 child 并且每个 child 有 600 childChild)
您是否知道如何通过仅使用一个存储库 (parent) 来提高性能,因为我不想拆分处理...
谢谢
您可以尝试使用批量插入。这样,Hibernate 就会向数据库发送成批的插入语句,而不是每个插入语句。
将 hibernate.jdbc.batch_size 设置为最大 30 的值。但要小心并测试它是否会提高您的性能。
阅读更多相关信息:
https://vladmihalcea.com/how-to-batch-insert-and-update-statements-with-hibernate/
我有一个休眠问题,当我尝试将我的 object 保存在数据库中时,它看起来非常慢。
所以,关注的模型是这样的:
@Entity
@Table(name = "T_PARENT")
public class ParentEntity implements Serializable {
// Id
// Attributes
// Child relation
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name="xxxx")
private List<ChildEntity> childs;
// Getters & setters
}
@Entity
@Table(name = "T_CHILD")
public class ChildEntity implements Serializable {
// Id
// Attributes
// Child relation
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name="yyyyy")
private List<ChildChildEntity> childs;
// Getters & setters
}
@Entity
@Table(name = "T_CHILD_CHILD")
public class ChildChildEntity implements Serializable {
// Id
// Attributes
// Getters & setters
}
我使用 JpaRepository (spring) 来管理数据库中的数据。 我们在 "parent" 上调用 "save" 方法来持久化 "child" 和 "childChild"(只创建了 parent 的存储库)
数据很少,没有问题(例如:1 parent / 20 child 并且每个 child 有 200 个 child 孩子) 但是当我们有一个重要的卷时,休眠查询是无休止的......(例如:1 parent / 40 child 并且每个 child 有 600 childChild)
您是否知道如何通过仅使用一个存储库 (parent) 来提高性能,因为我不想拆分处理...
谢谢
您可以尝试使用批量插入。这样,Hibernate 就会向数据库发送成批的插入语句,而不是每个插入语句。
将 hibernate.jdbc.batch_size 设置为最大 30 的值。但要小心并测试它是否会提高您的性能。
阅读更多相关信息: https://vladmihalcea.com/how-to-batch-insert-and-update-statements-with-hibernate/