我们需要在mysql上快速插入500万条记录(对此实例无控制权。)>
下面尝试了所有,但没有任何效果:
rewritebatchedstatements=true
(通过url和props)useServerPrepStmts=true
和useServerPrepStmts=true
Story
有GenerationType.Auto
,所以还可以,但是Task
是EmbeddedId
,所以批处理应该起作用? (因为未指定任何世代)MySql5InnoDBDialect
和MySql5DBDialect
分批正在工作(基于统计信息),但rewritebatchedstatements
无法工作。
spring.jpa.properties.hibernate.jdbc.batch_size=500 spring.jpa.properties.hibernate.jdbc.fetch_size=500 spring.jpa.properties.hibernate.jdbc.order_inserts=500 spring.jpa.properties.hibernate.jdbc.order_updates=500 spring.jpa.properties.hibernate.batch_versioned_data=true spring.jpa.properties.hibernate.generate_statistics=true spring.datasource.hikari.data-source-properties.rewriteBatchedStatements=true #spring.datasource.hikari.data-source-properties.useServerPrepStmts=true spring.datasource.hikari.data-source-properties.useServerPrepStmts=false
示例实体:
class SprintService { @Transactional public void startSprint(Epic epic, List<Stories> stories) { epic.save() ..... stories.saveAll() // few hundreds of records Set<Task> tasks = new HashSet<>; for(Story story: stories) { tasks.addAll(story.getTasks()); tasks.addAll(story.getTasks()); tasks.addAll(story.getTasks()); //do something } tasks.saveAll() // few million records } } @Table(name = "STORY") @EqualsAndHashCode class Story { @Id @GeneratedValue(stratergy=GenerationType.Auto) @Column(name="STORY_ID") Long id; @Column(name="STORY_NAME") String name; @OneToMany(cascade=MERGE, mappedBy="story", fetch = FetchType.Lazy) Set<Task> task; } @Entity @Table(name = "TASK_XREF") class Task { @EmbeddedId TaskPKId taskPKId; @Column(name = "TASK_NAME") String name; @MapsId("storyId") @ManyToOne(fetch = FetchType.LAZY, optional = false) @JoinColumn(name = "STORY_ID") Story story; //getters, setters }
这是TaskPKID类:
@Embeddable
class TaskPKId implements Serializable {
long taskId;
long taskTypeId;
@Column(name="STORY_ID")
long storyId;
public long getTaskId() {
return taskId;
}
public void setTaskId(long taskId) {
this.taskId = taskId;
}
public void setTaskTypeId(long taskTypeId) {
this.taskTypeId = taskTypeId;
}
}
我们需要在mysql上快速插入500万条记录(对此实例无控制)。尝试了所有以下内容,但没有任何效果:尝试设置rewritebatchedstatements = true(通过url和props)...
值应为布尔值。