I have this query that takes about 3.5 seconds to get 2 entries. However, in the reviews there are more than 100 thousand lines, 13 thousand users, 850 in courses, 2 in exams.
SELECT t.*, u.name, f.feedback FROM testmonials t INNER JOIN user u ON u.id = t.userid INNER JOIN courses co ON co.id = t.courseid LEFT JOIN exam ex ON ex.id = t.exam_id WHERE t.status = 4 AND t.verfication_required = 'Y' AND t.verfication_completed = 'N' ORDER BY t.submissiondate DESC
. Get the result :.
id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE co ALL PRIMARY NULL NULL NULL 850 Using temporary; Using filesort 1 SIMPLE t ref CID,nuk_tran_user CID 4 kms.co.id 8 Using where 1 SIMPLE u eq_ref PRIMARY PRIMARY 4 kms.t.userid 1 Using where 1 SIMPLE ex eq_ref PRIMARY PRIMARY 3 kms.t.eval_id 1
If I delete the join of the courses table, then the query will return the result pretty quickly. I canβt understand why this query should select all course lines, i.e. 850?
Any ideas what I'm doing wrong?
Edit: I have an index on courseid, userid in the review table, and these are the primary keys of their respective tables.
EDIT 2
I just deleted the course index from the review table (just to check), and it is interesting that the query result returned in 0.22 seconds !!! ?? Everything else, as above, just deleted only this index.
id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE t ALL nuk_tran_user NULL NULL NULL 130696 Using where; Using filesort 1 SIMPLE u eq_ref PRIMARY PRIMARY 4 kms.t.userid 1 Using where 1 SIMPLE co eq_ref PRIMARY PRIMARY 4 kms.t.courseid 1 1 SIMPLE ex eq_ref PRIMARY PRIMARY 3 kms.t.exam_id 1
EDIT 3
EDIT 3
CREATE TABLE IF NOT EXISTS `courses` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, `description` text NOT NULL, `duration` varchar(100) NOT NULL DEFAULT '', `objectives` text NOT NULL, `updated_at` datetime DEFAULT NULL, `updated_by` int(10) unsigned DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=851 ; Testimonials CREATE TABLE IF NOT EXISTS `testimonials` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `feedback` text NOT NULL, `userid` int(10) unsigned NOT NULL DEFAULT '0', `courseid` int(10) unsigned NOT NULL DEFAULT '0', `eventid` int(10) unsigned NOT NULL DEFAULT '0', `emr_date` datetime DEFAULT NULL, `exam_required` enum('Y','N') NOT NULL DEFAULT 'N', `exam_id` smallint(5) unsigned NOT NULL DEFAULT '0', `emr_completed` enum('Y','N') NOT NULL DEFAULT 'N', PRIMARY KEY (`id`), KEY `event` (`eventid`), KEY `nuk_tran_user` (`userid`), KEY `emr_date` (`emr_date`), KEY `courseid` (`courseid`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=134691 ;
.. this is the last result of an explanation request now ...
id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE t ALL nuk_tran_user,courseid NULL NULL NULL 130696 Using where; Using filesort 1 SIMPLE u eq_ref PRIMARY PRIMARY 4 kms.t.userid 1 Using where 1 SIMPLE co eq_ref PRIMARY PRIMARY 4 kms.t.courseid 1 1 SIMPLE ex eq_ref PRIMARY PRIMARY 3 kms.t.exam_id 1