I have 1,000,000 records to enter into a database. I have an arrayList of all million records. I then run a for loop to break the arrayList into smaller lists of 1,000 items and do a save all. The first group of 1,000 inserts in under a second, then every batch of 1,000 takes longer and longer to run, within 100,000 records it takes over a minute to save. If I stop the program and pick up where it left off the first batch again inserts in under a second then the time per insert grows. Below is pseudocode
ArrayList<Student> students= getAllRecords();
int skip =1000;
for (int i = (int) (repository.count() + 1); i < students.size(); i += skip)
{
List<String> subStudents = students.subList(i, Math.min(i + skip, students.size()));
studentRepository.saveAll(subStudents);
}