我正在尝试将大量数据(大约100k)插入MYSQL数据库。见下文:
EntityManager em = emf.createEntityManager();
EntityTransaction transaction = em.getTransaction();
long startTime = System.nanoTime();
transaction.begin();
for(int y = 0; y < 100000; y++) {
RealVector real = new RealVector(10000);
for(int i = 0; i < 10000; i++) {
real.getCoordinates().add((float) i);
}
em.persist(real);
}
transaction.commit();
该课程看起来像这样:
@TableGenerator(name = "vector_gen", table = "id_gen", pkColumnName = "gen_name", valueColumnName = "gen_val", pkColumnValue = "REAL_VECTOR", allocationSize = 100)
@Id
@GeneratedValue(strategy = GenerationType.TABLE, generator = "vector_gen")
private int id;
private int dimension;
@ElementCollection
private List<Float> coordinates;
public RealVector() {
}
public RealVector(int dimension) {
this.dimension = dimension;
this.coordinates = new ArrayList<Float>();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getDimension() {
return dimension;
}
public void setDimension(int dimension) {
this.dimension = dimension;
}
public List<Float> getCoordinates() {
return coordinates;
}
public void setCoordinates(List<Float> coordinates) {
this.coordinates = coordinates;
}
总而言之,我试图将100k类型的RealVector对象插入到数据库中。每个RealVector对象都有一个包含10k个对象的数组。但这需要数小时才能完成并且需要大量内存。我是MYSQL和JPA的新手。有什么方法可以改进它,使其更快,如果可能的话,消耗更少的内存?