谁能帮我优化这个查询?需要 15 秒。如果我将 WHERE 子句移动到连接本身,结果仍然相同。
SELECT
`users`.`employeeID` as `username`,
`users`.`firstName` as `fname`,
`users`.`lastName` as `lname`
FROM `enrollment`
INNER JOIN `users` ON `enrollment`.`employeeID` = `users`.`employeeID`
WHERE `enrollment`.`number` = [int]
AND
`enrollment`.`term` = [int]
AND
(
`enrollment`.`status` = 'E'
OR
`enrollment`.`status` = 'M'
)
ORDER BY
`users`.`lastName` ASC,
`users`.`firstName` ASC;
只要这样:
SELECT
`users`.`employeeID` as `username`,
`users`.`firstName` as `fname`,
`users`.`lastName` as `lname`
FROM `enrollment`
INNER JOIN
`users` ON `enrollment`.`employeeID` = `users`.`employeeID`
AND
`enrollment`.`number` = [int]
AND
`enrollment`.`term` = [int]
AND
(
`enrollment`.`status` = 'E'
OR
`enrollment`.`status` = 'M'
)
ORDER BY
`users`.`lastName` ASC,
`users`.`firstName` ASC;
这是解释结果
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE users ALL PRIMARY NULL NULL NULL 52925 Using filesort
1 SIMPLE enrollment ref employeeID,number_term employeeID 9 ezlrn.users.employeeID 2 Using where