英文:
Improve JPA update() performance improvement with partitioned tables
问题
使用Hibernate,我有一个带有UUID
类型的Id
字段、Instant
类型的date
字段以及其他一些字段(包括具有一对多关系的集合)的JPA实体。
为了说明:
@Entity
@Table(name = "sport_car")
public class SportCar implements Serializable {
@Id
UUID id;
Instant date;
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
List<Sponsor> sponsors;
...
}
出于性能考虑,我决定按照date
对sport_car
表进行分区,每天一个表,因为每天都会添加很多SportCar
。
为了改进SportCarJpaRepository.update()
,我考虑为SportCar
设置一个包括date
在内的复合主键/ID,但问题在于sponsors
属性。
性能改进应该是明显的,因为大多数更新是针对最新的日期实例进行的。
为了建立一对多的引用,我需要在映射类Sponsor
的表中添加一个新的列,因为JPA假定您需要sport_card
表中的所有id列来创建唯一引用,实际上这并不是必要的,因为id
字段是唯一的。
为了说明:
@Entity
@IdClass(SportCarId.class)
public class SportCar implements Serializable {
@Id
UUID id;
@Id
Instant date;
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "id", referencedColumnName = "sport_car_id")
List<Sponsor> sponsors;
...
}
public class SportCarId implements Serializable {
UUID id;
Instant date;
}
这不起作用,因为SportCar.date
字段在SportCar.sponsors
的@OneToMany
关系中没有被引用。
我的问题是,Hibernate/JPA是否提供了一种优化update()
以利用分区的方法?
英文:
With hibernate, I have a JPA entity with an Id
field of UUID
type, a date
field of Instant
type, and some other fields including collection with One to Many relations.
To illustrate
@Entity
@Table(name = "sport_car")
public class SportCar implements Serializable {
@Id
UUID id;
Instant date;
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
List<Sponsor> sponsors;
...
}
For performance, I decided to partition the sport_car
table by date
, one for each day, since a lot of SportCar
are added each day.
To improve the SportCarJpaRepository.update()
I was thinking on setting a composite key/id to SportCar
including date
, but the problem is on the sponsors
attribute.
The performance improvement should be noticeable since most updates are done on latest day instances.
To make the One to Many reference I need to add a new column in the table where class Sponsor
is mapped, because JPA assumes that you need all the id columns in the sport_card
table to make the unique reference, when is not really necessary, because the id
field is unique.
To illustrate
@Entity
@IdClass(SportCarId.class)
public class SportCar implements Serializable {
@Id
UUID id;
@Id
Instant date;
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "id", referencedColumnName = "sport_car_id")
List<Sponsor> sponsors;
...
}
public class SportCarId implements Serializable {
UUID id;
Instant date;
}
This won't work because the SportCar.date
field is not referenced in the @OneToMany
relations on the SportCar.sponsors
.
My question is, does Hibernate/JPA provides a way to optimize the update()
taking advantage of the partitioning?
专注分享java语言的经验与见解,让所有开发者获益!
评论