简体   繁体   中英

How to fix slow query performance using NOT EXISTS MySQL? CodeIgniter

I am trying to optimize / speed up the performance the loading of my data, is there a way to fix this?

I have 3 tables involved:

tbl_tt_college_studentpersonalinfo - This table displays the name of students, it is join in my query

tbl_tt_college_preenrollment - This table displays the pending status of pre-enrolled students

tbl_tt_college_enlistment - This table will check the existing record the student has.

Here is my Query: - The problem that I am having is the load of data is slow, it may take 30-50 seconds. Is there a way to improve this or make it like a join query?

 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(); }

Here is the raw query for the reference:

 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'). "' )

See if this composite index on tbl_tt_college_enlistment helps:

INDEX(schoolYear, semester, studentKeyID)

If not adequate, please provide SHOW CREATE TABLE and EXPLAIN SELECT ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM