I am trying to insert big amounts of data (around 100k) into MYSQL database. See below:
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();
The class looks something like this:
@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;
}
So in summary I am trying to insert 100k objects of type RealVector into the db. Each RealVector object has an array of 10k objects. But this takes hours to complete and a lot of memory. I am new to MYSQL and JPA. Is there any way I can improve this to make it faster and if possible consume less memory?