Browse Source

自动服务遍历数据sql慢

huang yuanyuan 5 năm trước cách đây
mục cha
commit
87cdbb2135

+ 1 - 1
examcloud-core-oe-student-dao/src/main/java/cn/com/qmth/examcloud/core/oe/student/dao/ExamRecordDataRepo.java

@@ -18,7 +18,7 @@ import cn.com.qmth.examcloud.core.oe.student.dao.entity.ExamRecordDataEntity;
  */
 @Repository
 public interface ExamRecordDataRepo extends JpaRepository<ExamRecordDataEntity, Long>, JpaSpecificationExecutor<ExamRecordDataEntity> {
-    @Query(value = "select *  from ec_oes_exam_record_data where (batch_num is null or batch_num!=?1) and id>?2 and (sync_status is null or sync_status='UNSYNC') and (exam_record_status is null or exam_record_status!='EXAM_ERROR')  order by id limit ?3",nativeQuery = true)
+    @Query(value = "select *  from ec_oes_exam_record_data where (batch_num is null or batch_num!=?1) and id>?2  order by id limit ?3",nativeQuery = true)
     List<ExamRecordDataEntity> getLimitExamRecordDataList(Long batchNum, Long startId, Integer size);
     
     @Modifying

+ 1 - 0
examcloud-core-oe-student-dao/src/main/java/cn/com/qmth/examcloud/core/oe/student/dao/entity/ExamRecordDataEntity.java

@@ -33,6 +33,7 @@ import cn.com.qmth.examcloud.web.jpa.JpaEntity;
         @Index(name = "IDX_E_O_E_R_D_002", columnList = "studentId"),
         @Index(name = "IDX_E_O_E_R_D_003", columnList = "examId"),
         @Index(name = "IDX_E_O_E_R_D_004", columnList = "courseId"),
+        @Index(name = "IDX_E_O_E_R_D_005", columnList = "batchNum"),
 })
 @DynamicInsert
 public class ExamRecordDataEntity extends JpaEntity {

+ 19 - 2
examcloud-core-oe-student-service/src/main/java/cn/com/qmth/examcloud/core/oe/student/service/impl/ExamRecordDataServiceImpl.java

@@ -2,6 +2,7 @@ package cn.com.qmth.examcloud.core.oe.student.service.impl;
 
 import java.math.BigDecimal;
 import java.text.DecimalFormat;
+import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
 import java.util.stream.Collectors;
@@ -184,8 +185,24 @@ public class ExamRecordDataServiceImpl implements ExamRecordDataService {
         if (size > 200) {
             throw new StatusException("1004", "size 最大为200");
         }
-        List<ExamRecordDataEntity> list = examRecordDataRepo.getLimitExamRecordDataList(batchNum, startId, size);
-        List<Long> ids = list.stream().map(et -> et.getId()).collect(Collectors.toList());
+        List<Long> ids=new ArrayList<Long>();
+        for(;;) {
+	        List<ExamRecordDataEntity> list = examRecordDataRepo.getLimitExamRecordDataList(batchNum, startId, size);
+	        if(list!=null&&list.size()!=0) {
+	        	for(ExamRecordDataEntity e:list) {
+	        		if((e.getSyncStatus()==null||e.getSyncStatus().equals(SyncStatus.UNSYNC))&&!ExamRecordStatus.EXAM_ERROR.equals(e.getExamRecordStatus())) {
+	        			ids.add(e.getId());
+	        		}
+	        	}
+	        	if(ids.size()==0) {
+	        		startId=list.get(list.size()-1).getId();
+	        	}else {
+	        		break;
+	        	}
+	        }else {
+	        	break;
+	        }
+        }
         return ids;
     }