如何使用NOT EXISTS MySQL?CodeIgniter修复查询性能低下的问题

guz6ccqo  于 2022-12-07  发布在  Mysql
关注(0)|答案(1)|浏览(135)

我正在尝试优化/加快数据加载的性能,是否有办法解决此问题?
我有3个表:
tbl_tt_college_studentpersonalinfo-此表显示学生的姓名,它是查询中的连接
tbl_tt_college_preenrollment-此表显示预注册学员的待定状态
tbl_tt_college_enlistment-此表将检查学生的现有记录。
以下是我的查询:- 我遇到的问题是数据加载很慢,可能需要30-50秒。有没有办法改进这个问题,或者使它像连接查询一样?

public function get_enlists()
{
    $this->db->join('tbl_tt_college_studentpersonalinfo','tbl_tt_college_preenrollment.studentID = tbl_tt_college_studentpersonalinfo.studentID');
    $this->db->where('tbl_tt_college_preenrollment.departmentID', $this->input->post('course'));
    $this->db->where('tbl_tt_college_preenrollment.yearLevel', $this->input->post('yearLevel'));
    $this->db->where("NOT EXISTS (SELECT tbl_tt_college_enlistment.studentKeyID FROM tbl_tt_college_enlistment WHERE tbl_tt_college_enlistment.studentKeyID = tbl_tt_college_preenrollment.studentKeyID AND schoolYear = '" . $this->session->userdata('currentAcademicYear') . '-' . ($this->session->userdata('currentAcademicYear') + 1) . "' AND semester =     '" . $this->session->userdata('currentSemester') . "')");
    $query = $this->db->get('tbl_tt_college_preenrollment');
    return $query->result_array();
}

下面是引用的原始查询:

NOT EXISTS (
    SELECT studentKeyID FROM tbl_tt_college_enlistment 
    WHERE tbl_tt_college_studentpersonalinfo.studentKeyID = tbl_tt_college_enlistment.studentKeyID 
    AND schoolYear = '" . $this->session->userdata('currentAcademicYear') . '-' . ($this->session->userdata('currentAcademicYear') + 1) . "'                            
    AND semester =  '" . $this->session->userdata('currentSemester') . "'
)
zsbz8rwp

zsbz8rwp1#

看看tbl_tt_college_enlistment上的复合索引是否有帮助:

INDEX(schoolYear, semester, studentKeyID)

如果不充分,请提供SHOW CREATE TABLEEXPLAIN SELECT ...

相关问题