誰かがこのクエリを最適化するのを手伝ってもらえますか?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;
これはEXPLAINの結果です
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