首先,可以优化的地方是将子查询中的'exists'子查询改为'join'操作,这样可以避免重复查询。

修改后的 SQL 如下:

select pea.emp_code,
       pea.name emp_name,
       pee.suggested_level,
       round(count(1) / tmp.suggested_level_count * 100, 2) as proportion
from pfqa_examine_evaluation pee
       left join pfqa_evaluation_appraisee pea on pea.id = pee.appraisee_id
       inner join
     (select pea.emp_code, count(pee.suggested_level) as suggested_level_count
      from pfqa_examine_evaluation pee
             left join pfqa_evaluation_appraisee pea on pea.id = pee.appraisee_id
             left join pfqa_examine_weight pew on pew.id = pee.weight_id
             join pfqa_examiner_activ_evaluation activ on pee.examiner_id = activ.examiner_id and activ.status = 3
      where pee.activity_id = #{activityId}
      group by pea.emp_code) tmp on tmp.emp_code = pea.emp_code
where pee.activity_id = #{activityId}
group by pea.emp_code, pee.suggested_level, pea.name, tmp.suggested_level_count

此外,还可以考虑对相关表的索引进行优化,以提高查询效率。

SQL 优化技巧:将 EXISTS 子查询替换为 JOIN 操作

原文地址: https://www.cveoy.top/t/topic/fcsw 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录