我使用以下查询COUNT记录并在MySQL中显示记录。虽然它显示正常并且工作正常,但速度非常慢。我认为这部分是由于HTG_ScheduleRequest表非常大。有没有更好的方法来安排此查询以加快速度?
SELECT HTG_TechProps.EmpNumber,
HTG_TechProps.EmpFirstName,
HTG_TechProps.EmpLastName,
HTG_TechProps.Veh_Number,
COUNT(HTG_ScheduleRequest.ID) AS current_job
FROM HTG_TechProps
LEFT JOIN HTG_ScheduleRequest
ON HTG_TechProps.EmpNumber = HTG_ScheduleRequest.SSR
AND (HTG_ScheduleRequest.ScheduleDateCurrent = CURDATE())
AND HTG_ScheduleRequest.JobStatus = '3'
GROUP BY HTG_TechProps.EmpNumber
ORDER BY HTG_TechProps.EmpNumber ASC
答案 0 :(得分:2)
假设EmpNumber
是唯一的,请尝试此版本(索引遵循查询):
SELECT tp.EmpNumber, tp.EmpFirstName, tp.EmpLastName, tp.Veh_Number,
(SELECT COUNT(*)
FROM HTG_ScheduleRequest sr
WHERE tp.EmpNumber = sr.SSR AND
sr.ScheduleDateCurrent = CURDATE() AND
sr.JobStatus = '3'
)
FROM HTG_TechProps tp
ORDER BY tp.EmpNumber ASC;
在运行查询之前,请创建以下索引:HTG_TechProps(EmpNumber)
和HTG_ScheduleRequest(SSR, JobStatus, ScheduleDateCurrent)
。