我们需要在 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;
}
}
答案 0 :(得分:1)
值应为布尔值。
spring.jpa.properties.hibernate.jdbc.order_inserts=true
spring.jpa.properties.hibernate.jdbc.order_updates=true