考虑 2 个 MySQL 查询:
SELECT ue.userid,e.courseid
FROM (SELECT id,courseid FROM mdl_enrol WHERE status = 0 AND courseid IN (46)) e
INNER JOIN (SELECT enrolid,userid FROM mdl_user_enrolments ) ue ON ue.enrolid = e.id
INNER JOIN (SELECT userid FROM mdl_userdata) ud ON ue.userid = ud.userid
--
SELECT ue.userid,e.courseid
FROM mdl_enrol e
INNER JOIN mdl_user_enrolments ue ON ue.enrolid = e.id
INNER JOIN mdl_userdata ud ON ue.userid = ud.userid
WHERE e.status = 0 AND e.courseid IN (46)
底部查询比顶部查询快得多,但为什么呢?我已经读过,为了提高性能,您应该只选择您需要的列。此外,对我来说,顶级查询应该表现得更好,因为在每个 JOIN 中,您都在减少要加入的数据量。显然,我对数据库如何工作的理解是错误的,但如果有人能澄清这一点,将不胜感激。EXPLAIN 还确认底部查询要快得多。
非常感谢。