Browse Source

sql优化

xiatian 3 years ago
parent
commit
cb195dc5d1

+ 12 - 3
examcloud-core-oe-admin-service/src/main/java/cn/com/qmth/examcloud/core/oe/admin/service/impl/ExamRecordServiceImpl.java

@@ -313,16 +313,25 @@ public class ExamRecordServiceImpl implements ExamRecordService {
         }
         sqlBuilder.append(" and record_data.is_illegality = 0");
         if (StringUtils.isBlank(next)){
-            sqlBuilder.append(" and record_data.id != ");
+        	sqlBuilder.append(" and record_data.id in (select id from ec_oe_exam_record_data where");
+            sqlBuilder.append(" id != ");
             sqlBuilder.append(examRecordDataId);
+            sqlBuilder.append(" and exam_id="+query.getExamId());
+            sqlBuilder.append(" ) ");
             sqlBuilder.append(" order by record_data.id desc");
         } else if ("1".equals(next)){
-            sqlBuilder.append(" and record_data.id < ");
+        	sqlBuilder.append(" and record_data.id in (select id from ec_oe_exam_record_data where");
+            sqlBuilder.append(" id < ");
             sqlBuilder.append(examRecordDataId);
+            sqlBuilder.append(" and exam_id="+query.getExamId());
+            sqlBuilder.append(" ) ");
             sqlBuilder.append(" order by record_data.id desc");
         } else if ("0".equals(next)){
-            sqlBuilder.append(" and record_data.id > ");
+        	sqlBuilder.append(" and record_data.id in (select id from ec_oe_exam_record_data where");
+            sqlBuilder.append(" id > ");
             sqlBuilder.append(examRecordDataId);
+            sqlBuilder.append(" and exam_id="+query.getExamId());
+            sqlBuilder.append(" ) ");
             sqlBuilder.append(" order by record_data.id asc");
         }
         sqlBuilder.append(" limit 1 ");

+ 7 - 7
examcloud-core-oe-admin-service/src/main/java/cn/com/qmth/examcloud/core/oe/admin/service/impl/ExamStudentServiceImpl.java

@@ -26,7 +26,6 @@ import java.util.stream.Collectors;
 import javax.persistence.EntityManager;
 import javax.persistence.Query;
 
-import cn.com.qmth.examcloud.examwork.api.request.GetExamListReq;
 import org.apache.commons.lang3.StringUtils;
 import org.hibernate.query.NativeQuery;
 import org.hibernate.transform.Transformers;
@@ -309,13 +308,14 @@ public class ExamStudentServiceImpl implements ExamStudentService {
         ExamSettingsCacheBean examBean = ExamCacheTransferHelper.getDefaultCachedExam(query.getExamId());
 
         List<ExamStudentEntity> examStudentList=new ArrayList<ExamStudentEntity>();
-        Long startId=0L;
+        int pageNo=1;
+        int pageSize=500;
         for(;;) {
-        	List<ExamStudentEntity> tem=getExamStudentInfoListByPage(query, examBean, startId);
+        	List<ExamStudentEntity> tem=getExamStudentInfoListByPage(query, examBean, pageNo,pageSize);
         	if(tem==null||tem.size()==0) {
         		break;
         	}else {
-        		startId=tem.get(tem.size()-1).getId();
+        		pageNo++;
         		examStudentList.addAll(tem);
         	}
         }
@@ -395,7 +395,7 @@ public class ExamStudentServiceImpl implements ExamStudentService {
 		};
 		tool.setDataForBatch(dataList, 100);
     }
-    private List<ExamStudentEntity> getExamStudentInfoListByPage(ExamStudentQuery query,ExamSettingsCacheBean examBean,Long startId) {
+    private List<ExamStudentEntity> getExamStudentInfoListByPage(ExamStudentQuery query,ExamSettingsCacheBean examBean,int pageNo,int pageSize) {
     	//查询条件
         StringBuffer sql = new StringBuffer();
         sql.append("select id,exam_student_id,exam_id,course_id,course_code,course_level");
@@ -409,8 +409,8 @@ public class ExamStudentServiceImpl implements ExamStudentService {
                 + ",info_collector,root_org_id,org_id,paper_type,used_num,extra_num"
                 + ",specialty_code,specialty_name,grade from ec_oe_exam_student t1 where 1=1 ");
         sql.append(selectExamStudentConfitionSql(query, examBean.getExamType()));
-        sql.append(" and id >"+startId);
-        sql.append(" order by id limit 500");
+        int currentNum = (pageNo - 1) * pageSize;
+        sql.append(" order by id limit "+currentNum+","+pageSize);
 
         List<ExamStudentEntity> examStudentList = jdbcTemplate.query(sql.toString(), new RowMapper<ExamStudentEntity>() {
             @Override