소스 검색

sql优化

wangliang 2 년 전
부모
커밋
54ef42f95f
1개의 변경된 파일229개의 추가작업 그리고 255개의 파일을 삭제
  1. 229 255
      themis-business/src/main/resources/mapper/TOeExamRecordMapper.xml

+ 229 - 255
themis-business/src/main/resources/mapper/TOeExamRecordMapper.xml

@@ -11,20 +11,20 @@
                         t.exam_student_id  examStudentId,
                         t.id               examRecordId,
         INSERT(rpad(substring(s.identity, 1, 2), char_length(s.identity), '*'),char_length(s.identity)-1,char_length(s.identity),substring(s.identity, char_length(s.identity) - 1 , char_length(s.identity) - 2)) as identity,
-		s.room_code roomCode,
-		s.room_name roomName,
-		s.name name,
-		s.course_name courseName,
-		s.course_code courseCode,
-		IFNULL(t.paper_download,1) paperDownload,
-		t.status statusCode,
-	    TRUNCATE(t.answer_progress,2) progress,
-		IFNULL(t.client_current_ip,'无') clientCurrentIp,
-		IFNULL(t.warning_count,0) as warningCount,
-		IFNULL(t.breach_status,1) as breachStatus,
-		IFNULL(t.client_websocket_status,'OFF_LINE') as clientWebsocketStatus,
-		t.client_last_sync_time as updateTime,
-		t.monitor_video_source as monitorVideoSource,
+        s.room_code roomCode,
+        s.room_name roomName,
+        s.name name,
+        s.course_name courseName,
+        s.course_code courseCode,
+        IFNULL(t.paper_download,1) paperDownload,
+        t.status statusCode,
+        TRUNCATE(t.answer_progress,2) progress,
+        IFNULL(t.client_current_ip,'无') clientCurrentIp,
+        IFNULL(t.warning_count,0) as warningCount,
+        IFNULL(t.breach_status,1) as breachStatus,
+        IFNULL(t.client_websocket_status,'OFF_LINE') as clientWebsocketStatus,
+        t.client_last_sync_time as updateTime,
+        t.monitor_video_source as monitorVideoSource,
         t.monitor_record as monitorRecord
     </sql>
 
@@ -737,7 +737,18 @@
     <sql id="invigilatePageListHistorySql">
         select (@i := @i + 1) as seq,t.* from(
         select
-        temp.*,
+        distinct s.exam_id as examId,
+        tee.name as examName,
+        s.exam_activity_id as examActivityId,
+        teea.code as examActivityCode,
+        s.id as examStudentId,
+        s.identity,
+        s.room_code as roomCode,
+        s.room_name as roomName,
+        s.name,
+        s.course_name as courseName,
+        s.course_code as courseCode,
+        tes.mobile_number as mobileNumber,
         toer.id as examRecordId,
         IFNULL(toer.paper_download,1) as paperDownload,
         toer.status as statusCode,
@@ -754,29 +765,14 @@
         <if test="examId != null and examId != ''">
             and tiiwi.exam_id = #{examId}
         </if>
-            and tiiwi.exam_record_id = toer.id and tiiwi.`type` = 'FACE_COUNT_ERROR' and tiiwi.`level` = 'D8') as multipleFaceCount
+        and tiiwi.exam_record_id = toer.id and tiiwi.`type` = 'FACE_COUNT_ERROR' and tiiwi.`level` = 'D8') as multipleFaceCount
         ,(select count(1) from t_ie_invigilate_exception_info tiiei where 1 = 1
         <if test="examId != null and examId != ''">
             and tiiei.exam_id = #{examId}
         </if>
-            and tiiei.exam_record_id = toer.id) as
+        and tiiei.exam_record_id = toer.id) as
         exceptionCount
         from
-        (
-        select
-        distinct s.exam_id as examId,
-        tee.name as examName,
-        s.exam_activity_id as examActivityId,
-        teea.code as examActivityCode,
-        s.id as examStudentId,
-        s.identity,
-        s.room_code as roomCode,
-        s.room_name as roomName,
-        s.name,
-        s.course_name as courseName,
-        s.course_code as courseCode,
-        tes.mobile_number as mobileNumber
-        from
         t_e_exam_student s
         left join t_e_exam tee on
         tee.id = s.exam_id
@@ -784,29 +780,16 @@
         teea.id = s.exam_activity_id
         left join t_e_student tes on
         s.student_id = tes.id
-        WHERE
+        left join t_oe_exam_record toer on toer.exam_student_id = s.id
+        WHERE 1 = 1
         <choose>
             <when test="status != null and status != '' and status == 'UN_FINISH'">
-                NOT EXISTS(
+                and toer.id is null
             </when>
             <otherwise>
-                EXISTS(
+                and toer.id is not null
             </otherwise>
         </choose>
-        select
-        toer.exam_student_id
-        from
-        t_oe_exam_record toer
-        where 1 = 1
-        <if test="examId != null and examId != ''">
-            and toer.exam_id = #{examId}
-        </if>
-        and s.id = toer.exam_student_id
-        <!--<if test="status != null and status != '' and status == 'UN_FINISH'">
-            and (toer.STATUS = 'FINISHED' or toer.STATUS = 'PERSISTED')
-            and toer.first_start_time is not null
-        </if>-->
-        )
         <if test="examId != null and examId != ''">
             and s.exam_id = #{examId}
         </if>
@@ -836,67 +819,59 @@
         <if test="courseCode != null and courseCode != ''">
             and s.course_code = #{courseCode}
         </if>
-        ) temp
-        left join t_oe_exam_record toer on
-        toer.exam_student_id = temp.examStudentId
-        <where> 1 = 1
-            <if test="examId != null and examId != ''">
-                and toer.exam_id = #{examId}
-            </if>
-            <choose>
-                <when test="status == null">
-                    and toer.status <![CDATA[ <> ]]> 'JUNK'
-                </when>
-                <when test="status != null and status != '' and status != 'UN_FINISH'">
-                    <choose>
-                        <when test="status == 'EXAMING'">
-                            and (toer.status = 'ANSWERING' or toer.status = 'RESUME_PREPARE' or toer.status = 'BREAK_OFF')
-                        </when>
-                        <otherwise>
-                            and toer.status = #{status}
-                        </otherwise>
-                    </choose>
-                </when>
-            </choose>
-<!--            <if test="status != null and status != '' and status != 'UN_FINISH'">-->
-<!--                <choose>-->
-<!--                    <when test="status == 'EXAMING'">-->
-<!--                        and (toer.status = 'ANSWERING' or toer.status = 'RESUME_PREPARE' or toer.status = 'BREAK_OFF')-->
-<!--                    </when>-->
-<!--                    <otherwise>-->
-<!--                        and toer.status = #{status}-->
-<!--                    </otherwise>-->
-<!--                </choose>-->
-<!--            </if>-->
-            <!--<if test="status != null and status != '' and status == 'UN_FINISH'">
-                and (toer.first_start_time is null or toer.first_prepare_time is null)
-            </if>-->
-            <if test="maxWarningCount != null and maxWarningCount != '' or maxWarningCount == 0">
-                and toer.warning_count &lt;= #{maxWarningCount}
-            </if>
-            <if test="minWarningCount != null and minWarningCount != '' or minWarningCount == 0">
-                and toer.warning_count &gt;= #{minWarningCount}
-            </if>
-            <if test="breachStatus != null and breachStatus != '' or breachStatus == 0">
-                <if test="breachStatus == 0">
-                    and toer.breach_status = #{breachStatus}
-                </if>
-                <if test="breachStatus == 1">
-                    and (toer.breach_status = #{breachStatus} or toer.breach_status is null)
-                </if>
-            </if>
-            <if test="finishType != null and finishType != ''">
+        <choose>
+            <when test="status == null">
+                and toer.status <![CDATA[ <> ]]> 'JUNK'
+            </when>
+            <when test="status != null and status != '' and status != 'UN_FINISH'">
                 <choose>
-                    <when test="finishType == 'ALL'">
-                        and (toer.finish_type = 'MANUAL' or toer.finish_type = 'AUTO' or toer.finish_type = 'BREACH' or
-                        toer.finish_type = 'INTERRUPT')
+                    <when test="status == 'EXAMING'">
+                        and (toer.status = 'ANSWERING' or toer.status = 'RESUME_PREPARE' or toer.status = 'BREAK_OFF')
                     </when>
                     <otherwise>
-                        and toer.finish_type = #{finishType}
+                        and toer.status = #{status}
                     </otherwise>
                 </choose>
+            </when>
+        </choose>
+        <!--            <if test="status != null and status != '' and status != 'UN_FINISH'">-->
+        <!--                <choose>-->
+        <!--                    <when test="status == 'EXAMING'">-->
+        <!--                        and (toer.status = 'ANSWERING' or toer.status = 'RESUME_PREPARE' or toer.status = 'BREAK_OFF')-->
+        <!--                    </when>-->
+        <!--                    <otherwise>-->
+        <!--                        and toer.status = #{status}-->
+        <!--                    </otherwise>-->
+        <!--                </choose>-->
+        <!--            </if>-->
+        <!--<if test="status != null and status != '' and status == 'UN_FINISH'">
+            and (toer.first_start_time is null or toer.first_prepare_time is null)
+        </if>-->
+        <if test="maxWarningCount != null and maxWarningCount != '' or maxWarningCount == 0">
+            and toer.warning_count &lt;= #{maxWarningCount}
+        </if>
+        <if test="minWarningCount != null and minWarningCount != '' or minWarningCount == 0">
+            and toer.warning_count &gt;= #{minWarningCount}
+        </if>
+        <if test="breachStatus != null and breachStatus != '' or breachStatus == 0">
+            <if test="breachStatus == 0">
+                and toer.breach_status = #{breachStatus}
             </if>
-        </where>
+            <if test="breachStatus == 1">
+                and (toer.breach_status = #{breachStatus} or toer.breach_status is null)
+            </if>
+        </if>
+        <if test="finishType != null and finishType != ''">
+            <choose>
+                <when test="finishType == 'ALL'">
+                    and (toer.finish_type = 'MANUAL' or toer.finish_type = 'AUTO' or toer.finish_type = 'BREACH' or
+                    toer.finish_type = 'INTERRUPT')
+                </when>
+                <otherwise>
+                    and toer.finish_type = #{finishType}
+                </otherwise>
+            </choose>
+        </if>
         ) t,
         (SELECT @i := 0) as i
         <where>1 = 1
@@ -913,7 +888,6 @@
                 and t.exceptionCount &lt;= #{maxExceptionCount}
             </if>
         </where>
-        order by t.roomCode
     </sql>
 
     <select id="invigilatePageListHistoryExport"
@@ -1468,7 +1442,7 @@
         <if test="courseCode != null and courseCode != ''">
             and tees.course_code = #{courseCode}
         </if>
-            and toer.exam_student_id = tees.id
+        and toer.exam_student_id = tees.id
         )
         <if test="monitorStatus != null and monitorStatus != '' and monitorStatus == 'FINISHED'">
             and toer.first_start_time is not null
@@ -1721,8 +1695,8 @@
         select * from t_oe_exam_record toer
         where
             toer.exam_id = #{examId}
-            and toer.exam_activity_id = #{examActivityId}
-            and (toer.status = 'FIRST_PREPARE'
+          and toer.exam_activity_id = #{examActivityId}
+          and (toer.status = 'FIRST_PREPARE'
             or toer.status = 'ANSWERING'
             or toer.status = 'BREAK_OFF'
             or toer.status = 'RESUME_PREPARE')
@@ -1738,106 +1712,106 @@
                 and toer.exam_activity_id = #{examActivityId}
             </if>
             and (toer.status = 'FIRST_PREPARE'
-                or toer.status = 'ANSWERING'
-                or toer.status = 'BREAK_OFF'
-                or toer.status = 'RESUME_PREPARE')
+            or toer.status = 'ANSWERING'
+            or toer.status = 'BREAK_OFF'
+            or toer.status = 'RESUME_PREPARE')
         </where>
     </select>
 
     <select id="findExamRecordNeedMarkAll" resultType="com.qmth.themis.business.entity.TOeExamRecord">
         select
-            t1.*
+        t1.*
+        from
+        (
+        select
+        t.exam_student_id
+        from
+        t_oe_exam_record t
+        where
+        exists(
+        select
+        tees.id
         from
-            (
-                select
-                    t.exam_student_id
-                from
-                    t_oe_exam_record t
-                where
-                    exists(
-                            select
-                                tees.id
-                            from
-                                t_e_exam_student tees
-                                    join t_oe_exam_record toer on
-                                            toer.exam_id = tees.exam_id
-                                        and toer.exam_activity_id = tees.exam_activity_id
-                                        and toer.exam_student_id = tees.id
-                              <where>
-                                  <if test="examId != null and examId != ''">
-                                      and tees.exam_id = #{examId}
-                                  </if>
-                              and tees.enable = 1
-                              and tees.already_exam_count is not null
-                              and tees.already_exam_count <![CDATA[ > ]]> 0
-                              <if test="examPaperIdSet != null and examPaperIdSet != '' and examPaperIdSet.size > 0">
-                                  and toer.paper_id in
-                                  <foreach collection="examPaperIdSet" item="paperId" index="index" open="(" close=")" separator=",">
-                                      #{paperId}
-                                  </foreach>
-                              </if>
-                              and (toer.status = 'FINISHED' or toer.status = 'PERSISTED')
-                              and toer.first_start_time is not null
-                              and toer.finish_time is not null
-                              <if test="examStudentBreachPush != null and (examStudentBreachPush == false or examStudentBreachPush == 0)">
-                                and (toer.breach_status is null or toer.breach_status = 1)
-                              </if>
-                              and t.exam_student_id = tees.id
-                        </where>)
-                group by
-                    t.exam_student_id
-                <if test="many != null and many != '' or many == 0">
-                    <choose>
-                        <when test="many == true">
-                            HAVING count(t.exam_student_id) <![CDATA[ > ]]> 1
-                        </when>
-                        <otherwise>
-                            HAVING count(t.exam_student_id) = 1
-                        </otherwise>
-                    </choose>
-                </if>
-                ) t
-                join t_oe_exam_record t1 on
-                t1.exam_student_id = t.exam_student_id
-                where (t1.status = 'FINISHED' or t1.status = 'PERSISTED')
-                and t1.first_start_time is not null
-                and t1.finish_time is not null
-                <if test="examStudentBreachPush != null and (examStudentBreachPush == false or examStudentBreachPush == 0)">
-                    and (t1.breach_status is null or t1.breach_status = 1)
-                </if>
-                <!--<if test="many != null and many != '' or many == 0">
-                    <choose>
-                        <when test="many == true">
-                            order by t1.id limit 510
-                        </when>
-                        <otherwise>
-                            order by t1.id limit 500
-                        </otherwise>
-                    </choose>
-                </if>-->
-                order by t1.id
+        t_e_exam_student tees
+        join t_oe_exam_record toer on
+        toer.exam_id = tees.exam_id
+        and toer.exam_activity_id = tees.exam_activity_id
+        and toer.exam_student_id = tees.id
+        <where>
+            <if test="examId != null and examId != ''">
+                and tees.exam_id = #{examId}
+            </if>
+            and tees.enable = 1
+            and tees.already_exam_count is not null
+            and tees.already_exam_count <![CDATA[ > ]]> 0
+            <if test="examPaperIdSet != null and examPaperIdSet != '' and examPaperIdSet.size > 0">
+                and toer.paper_id in
+                <foreach collection="examPaperIdSet" item="paperId" index="index" open="(" close=")" separator=",">
+                    #{paperId}
+                </foreach>
+            </if>
+            and (toer.status = 'FINISHED' or toer.status = 'PERSISTED')
+            and toer.first_start_time is not null
+            and toer.finish_time is not null
+            <if test="examStudentBreachPush != null and (examStudentBreachPush == false or examStudentBreachPush == 0)">
+                and (toer.breach_status is null or toer.breach_status = 1)
+            </if>
+            and t.exam_student_id = tees.id
+        </where>)
+        group by
+        t.exam_student_id
+        <if test="many != null and many != '' or many == 0">
+            <choose>
+                <when test="many == true">
+                    HAVING count(t.exam_student_id) <![CDATA[ > ]]> 1
+                </when>
+                <otherwise>
+                    HAVING count(t.exam_student_id) = 1
+                </otherwise>
+            </choose>
+        </if>
+        ) t
+        join t_oe_exam_record t1 on
+        t1.exam_student_id = t.exam_student_id
+        where (t1.status = 'FINISHED' or t1.status = 'PERSISTED')
+        and t1.first_start_time is not null
+        and t1.finish_time is not null
+        <if test="examStudentBreachPush != null and (examStudentBreachPush == false or examStudentBreachPush == 0)">
+            and (t1.breach_status is null or t1.breach_status = 1)
+        </if>
+        <!--<if test="many != null and many != '' or many == 0">
+            <choose>
+                <when test="many == true">
+                    order by t1.id limit 510
+                </when>
+                <otherwise>
+                    order by t1.id limit 500
+                </otherwise>
+            </choose>
+        </if>-->
+        order by t1.id
     </select>
 
     <select id="persistedAnswerBatch" resultType="com.qmth.themis.business.entity.TOeExamRecord">
-            select toer.* from t_oe_exam_record toer
-                left join t_oe_exam_answer toea on toea.exam_record_id = toer.id
-            <where>
-                <if test="examId != null and examId != ''">
-                    and toer.exam_id = #{examId}
-                </if>
-                <if test="examActivityId != null and examActivityId != ''">
-                    and toer.exam_activity_id = #{examActivityId}
-                </if>
-                <if test="status != null and status != ''">
-                    and toer.status = #{status}
-                </if>
-              and toer.objective_score > 0
-              and toea.id is null
-              and toer.first_start_time is not null
-              and toer.finish_time is not null
-            </where>
-            group by toer.id
-            having count(toea.id) = 0
+        select toer.* from t_oe_exam_record toer
+        left join t_oe_exam_answer toea on toea.exam_record_id = toer.id
+        <where>
+            <if test="examId != null and examId != ''">
+                and toer.exam_id = #{examId}
+            </if>
+            <if test="examActivityId != null and examActivityId != ''">
+                and toer.exam_activity_id = #{examActivityId}
+            </if>
+            <if test="status != null and status != ''">
+                and toer.status = #{status}
+            </if>
+            and toer.objective_score > 0
+            and toea.id is null
+            and toer.first_start_time is not null
+            and toer.finish_time is not null
+        </where>
+        group by toer.id
+        having count(toea.id) = 0
     </select>
 
     <select id="cleanExamFirstPrepareCache" resultType="java.util.Map">
@@ -1846,30 +1820,30 @@
         from
         (
         select
-            tes.id as studentId,
-            toer.id as examRecordId,
-            DATE_FORMAT(FROM_UNIXTIME(toer.first_prepare_time  / 1000), '%Y-%m-%d %H:%i:%s') as 'firstPrepareTime',
-            DATE_FORMAT(FROM_UNIXTIME(toer.finish_time / 1000), '%Y-%m-%d %H:%i:%s') as 'finishTime',
-            DATE_FORMAT(FROM_UNIXTIME(toer.first_start_time / 1000), '%Y-%m-%d %H:%i:%s') as 'firstStartTime',
-            DATE_FORMAT(FROM_UNIXTIME(toer.start_time / 1000), '%Y-%m-%d %H:%i:%s') as 'startTime',
-            DATE_FORMAT(FROM_UNIXTIME(toer.end_time / 1000), '%Y-%m-%d %H:%i:%s') as 'endTime'
+        tes.id as studentId,
+        toer.id as examRecordId,
+        DATE_FORMAT(FROM_UNIXTIME(toer.first_prepare_time  / 1000), '%Y-%m-%d %H:%i:%s') as 'firstPrepareTime',
+        DATE_FORMAT(FROM_UNIXTIME(toer.finish_time / 1000), '%Y-%m-%d %H:%i:%s') as 'finishTime',
+        DATE_FORMAT(FROM_UNIXTIME(toer.first_start_time / 1000), '%Y-%m-%d %H:%i:%s') as 'firstStartTime',
+        DATE_FORMAT(FROM_UNIXTIME(toer.start_time / 1000), '%Y-%m-%d %H:%i:%s') as 'startTime',
+        DATE_FORMAT(FROM_UNIXTIME(toer.end_time / 1000), '%Y-%m-%d %H:%i:%s') as 'endTime'
         from
         t_oe_exam_record toer
         join t_e_exam_student tees on tees.id = toer.exam_student_id
         join t_e_student tes on tes.id = tees.student_id
         <where>
-        toer.status = 'FIRST_PREPARE'
-        <if test="examIds != null and examIds != ''">
-            and toer.exam_id in
-            <foreach collection="examIds" item="examId" index="index" open="(" close=")" separator=",">
-                #{examId}
-            </foreach>
-        </if>
-        and toer.first_prepare_time is not null
-        and toer.first_start_time is null
-        and toer.finish_time is null
-        and (toer.objective_score is null
-        or toer.objective_score = 0)
+            toer.status = 'FIRST_PREPARE'
+            <if test="examIds != null and examIds != ''">
+                and toer.exam_id in
+                <foreach collection="examIds" item="examId" index="index" open="(" close=")" separator=",">
+                    #{examId}
+                </foreach>
+            </if>
+            and toer.first_prepare_time is not null
+            and toer.first_start_time is null
+            and toer.finish_time is null
+            and (toer.objective_score is null
+            or toer.objective_score = 0)
         </where>
         group by
         toer.id
@@ -1926,42 +1900,42 @@
             sum(examCount) as examCount
         from
             (select
-            tbo.name,
-            tbo.code,
-            count(toer.id) as onlineCount,
-            0 as examCount
-        from
-            t_b_org tbo
-                join t_e_exam tee on
-                tbo.id = tee.org_id
-                join t_oe_exam_record toer on
-                tee.id = toer.exam_id
-        WHERE
-            toer.client_websocket_status = 'ON_LINE'
-        group by
-            tbo.name,
-            tbo.code
-        union all
-        select
-            tbo.name,
-            tbo.code,
-            0 as onlineCount,
-            count(toer.id) as examCount
-        from
-            t_b_org tbo
-                join t_e_exam tee on
-                tbo.id = tee.org_id
-                join t_oe_exam_record toer on
-                tee.id = toer.exam_id
-        WHERE
-            (toer.STATUS = 'ANSWERING'
-                or toer.STATUS = 'RESUME_PREPARE')
-          and toer.client_websocket_status = 'ON_LINE'
-        group by
-            tbo.name,
-            tbo.code) t group by
-                            t.name,
-                            t.code
+                 tbo.name,
+                 tbo.code,
+                 count(toer.id) as onlineCount,
+                 0 as examCount
+             from
+                 t_b_org tbo
+                     join t_e_exam tee on
+                     tbo.id = tee.org_id
+                     join t_oe_exam_record toer on
+                     tee.id = toer.exam_id
+             WHERE
+                 toer.client_websocket_status = 'ON_LINE'
+             group by
+                 tbo.name,
+                 tbo.code
+             union all
+             select
+                 tbo.name,
+                 tbo.code,
+                 0 as onlineCount,
+                 count(toer.id) as examCount
+             from
+                 t_b_org tbo
+                     join t_e_exam tee on
+                     tbo.id = tee.org_id
+                     join t_oe_exam_record toer on
+                     tee.id = toer.exam_id
+             WHERE
+                 (toer.STATUS = 'ANSWERING'
+                     or toer.STATUS = 'RESUME_PREPARE')
+               and toer.client_websocket_status = 'ON_LINE'
+             group by
+                 tbo.name,
+                 tbo.code) t group by
+                                 t.name,
+                                 t.code
     </select>
 
     <select id="mapDataCount" resultType="com.qmth.themis.business.bean.admin.MapDataCountBean">
@@ -1977,7 +1951,7 @@
                 toer.exam_id = tee.id
         WHERE
             toer.client_websocket_status = 'ON_LINE'
-            and tir.country != '0'
+          and tir.country != '0'
 	        and tir.province != '0'
         group by
             tir.country,