我有一个查询时间太长的问题(对于这个简单的查询超过两秒钟)。
乍一看,这似乎是一个索引问题,所有连接字段都已编入索引,但我找不到我可能需要索引的其他内容以加快速度。一旦我将需要的字段添加到查询中,它就会变得更慢。
SELECT `jobs`.`job_id` AS `job_id` FROM tabledef_Jobs AS jobs
LEFT JOIN tabledef_JobCatLink AS jobcats ON jobs.job_id = jobcats.job_id
LEFT JOIN tabledef_Applications AS apps ON jobs.job_id = apps.job_id
LEFT JOIN tabledef_Companies AS company ON jobs.company_id = company.company_id
GROUP BY `jobs`.`job_id`
ORDER BY `jobs`.`date_posted` ASC
LIMIT 0 , 50
表行数 (~):tabledef_Jobs (108k)、tabledef_JobCatLink (109k)、tabledef_Companies (100)、tabledef_Applications (50k)
在这里你可以看到描述。“使用临时”似乎是降低查询速度的原因:
表索引截图:
任何帮助将不胜感激
编辑答案
感谢@Steve(标记答案),最终改进了查询。最终,最终查询从 ~22s 减少到 ~0.3s:
SELECT `jobs`.`job_id` AS `job_id` FROM
(
SELECT * FROM tabledef_Jobs as jobs ORDER BY `jobs`.`date_posted` ASC LIMIT 0 , 50
) AS jobs
LEFT JOIN tabledef_JobCatLink AS jobcats ON jobs.job_id = jobcats.job_id
LEFT JOIN tabledef_Applications AS apps ON jobs.job_id = apps.job_id
LEFT JOIN tabledef_Companies AS company ON jobs.company_id = company.company_id
GROUP BY `jobs`.`job_id`
ORDER BY `jobs`.`date_posted` ASC
LIMIT 0 , 50