改进使用分区表的 JPA update() 性能,提升性能

huangapple 未分类评论99阅读模式
英文:

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;

  ...
}

出于性能考虑,我决定按照datesport_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 = &quot;sport_car&quot;)
public class SportCar implements Serializable {

  @Id
  UUID id;

  Instant date;

  @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
  List&lt;Sponsor&gt; 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 = &quot;id&quot;, referencedColumnName = &quot;sport_car_id&quot;)
  List&lt;Sponsor&gt; 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?

huangapple
  • 本文由 发表于 2020年8月15日 01:38:34
  • 转载请务必保留本文链接:https://java.coder-hub.com/63417593.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定