Наличие POST API, который вставляет данные в несколько таблиц.(MySQL)
Реализация использует JPA.
Ниже приведена последовательность выполняемых операций, любые предложения: как это оптимизировать.
SQL-запросы:
1) Select * from University where UID = 'UNI1';
2) If (University Not Exist) then Insert INTO University ...
3) Select * from College where UID = 'UNI1'
4) If (College Not Exist) then Insert INTO College ...
**In Loop (For Each Student)**
5) Delete * from CollegeStudent;
LOOP :
6) Select * from Student where StudentId = 'ST22'
7) If (Student Not Exist) then Insert INTO Student ...
8) Insert INTO CollegeStudent (Student, College);
LOOP ENDS;
Фрагмент кода:
@Transactional
public void persistStudentResults(String universityId, String collegeId, List<Student> studentList) {
University university= universityRepository.findByUniversityId(universityId);
if (university == null) {
university = createUniversityObject(universityId);
universityRepository.save(university );
}
College college = collegeRepository.getCollegeByCollegeId(university.getUniversityId(), collegeId);
if (college == null) {
college = createCollegeObject(university , collegeId);
collegeRepository.save(deviceDetails);
}
collegeStudentRepository.deleteByCollegeId(university.getUniversityId(), college.getCollegeId());
for (Student student: studentList) {
Student dbStudent = studentRepository.findByStudentId(student.getStudentId());
if (dbStudent == null) {
dbStudent = createStudentObject(student);
studentRepository.save(dbStudent);
}
CollegeStudent collegeStudent = createCollegeStudentObject(dbStudent, college);
collegeStudentRepository.save(collegeStudent);
}
}
Журналы гибернации:
className=org.hibernate.engine.internal.StatisticalLoggingSessionEventListener, methodName=end> StatisticalLoggingSessionEventListener - Session Metrics {
308714170 nanoseconds spent acquiring 1 JDBC connections;
0 nanoseconds spent releasing 0 JDBC connections;
524069 nanoseconds spent preparing 1 JDBC statements;
309001256 nanoseconds spent executing 1 JDBC statements;
0 nanoseconds spent executing 0 JDBC batches;
0 nanoseconds spent performing 0 L2C puts;
0 nanoseconds spent performing 0 L2C hits;
0 nanoseconds spent performing 0 L2C misses;
197852 nanoseconds spent executing 1 flushes (flushing a total of 1 entities and 0 collections);
0 nanoseconds spent executing 0 partial-flushes (flushing a total of 0 entities and 0 collections)
}
Кажется, дляпри каждом сохранении () создается новое соединение.
Время выполнения:
Количество учащихся: 5
БД MySQL: 243 мс
Aurora DB: 32 с
(если непосредственно вставлено в БД с помощью DBeaver: 1,5 с)
Количество учеников: 30
MySQL DB: 1 с
Aurora DB: 173 с
(если непосредственно вставлено в БД с использованием DBeaver: 9 с)