2019独角兽企业重金招聘Python工程师标准>>>
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, cidFROM weike_exam_files eINNER JOIN(SELECT tid, cidFROM weike_student_wrong_questionsGROUP BY tid, cid) tcON e.tid = tc.tidORDER BY exam_time DESC, cid;