I have a two statements that are basically identical. Yet, one takes 55 seconds, the other 2 seconds. WHY?
Here's the query that works fine:
SELECT
employees.EmployeeID
FROM
employees
LEFT JOIN req_budget_centers ON employees.BudgetCenter = req_budget_centers.BudgetCenter
INNER JOIN req_bcjc ON employees.BudgetCenter = req_bcjc.BudgetCenter AND employees.JobCode = req_bcjc.JobCode AND employees.EmpContr = req_bcjc.EmpContr
INNER JOIN j_bcjc_req ON req_bcjc.bcjcID = j_bcjc_req.bcjcID
INNER JOIN req_requirements ON j_bcjc_req.requirementID = req_requirements.id
WHERE
req_requirements.BoardCategoryID = 4 /*PROBLEM VALUE*/
AND req_requirements.isActive = 1
AND employees.`Status` = 'A'
Here's the code for the query that takes 50+ seconds:
SELECT
employees.EmployeeID
FROM
employees
LEFT JOIN req_budget_centers ON employees.BudgetCenter = req_budget_centers.BudgetCenter
INNER JOIN req_bcjc ON employees.BudgetCenter = req_bcjc.BudgetCenter AND employees.JobCode = req_bcjc.JobCode AND employees.EmpContr = req_bcjc.EmpContr
INNER JOIN j_bcjc_req ON req_bcjc.bcjcID = j_bcjc_req.bcjcID
INNER JOIN req_requirements ON j_bcjc_req.requirementID = req_requirements.id
WHERE
req_requirements.BoardCategoryID = 8 /* WORKS FINE */
AND req_requirements.isActive = 1
AND employees.`Status` = 'A'
Here are the execution plans for those two queries:
There's nothing in the database or in the tables that's different for the value 8 vs 4... the number of overall records involved is the same... How do I hunt down the bottleneck here?
I would also add that leaving the condition ( = 8 or =4) out completely, but leaving all the joins in, speeds up the query considerably as well...