weike_student_wrong_questions表大概有150万行数据,其中(tid, cid)做了索引,weike_exam_files不到100行,tid做了索引。下面一条语句执行时间大概是1.3秒。
SELECT weike_student_wrong_questions.tid, name, exam_time,
update_time, cid
FROM weike_student_wrong_questions
INNER JOIN weike_exam_files
ON weike_student_wrong_questions.tid = weike_exam_files.tid
GROUP BY weike_student_wrong_questions.tid, cid
ORDER BY exam_time DESC, cid;
把语句换成下面之后,查询效率大概提高了1000倍。
SELECT name, exam_time, update_time, e.tid, cid
FROM weike_exam_files e
INNER JOIN
(SELECT tid, cid
FROM weike_student_wrong_questions
GROUP BY tid, cid) tc
ON e.tid = tc.tid
ORDER BY exam_time DESC, cid;
评论