簡體   English   中英

如何使用 NOT EXISTS MySQL 修復緩慢的查詢性能? CodeIgniter

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

我正在嘗試優化/加快數據加載的性能,有沒有辦法解決這個問題?

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

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 ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM