在MySQL中减慢子查询

时间:2011-07-28 19:18:18

标签: mysql

我尝试使用CodeIgniter和Datatables.net生成报告。

现在我正在尝试关闭工作量(它是一个人力资源系统)。我曾经查询所有的工作,在PHP中做一个foreach,然后进行计算。

因为我想使用Datatables的所有功能(具体排序)我试图在mySQL中完成所有的计算。

问题是:第二个子查询非常非常慢。

SELECT 
jobs.jobs_id, clients.nome_fantasia, concat_ws(' ', user_profiles.first_name, user_profiles.last_name) as fullname, 
jobs.titulo_vaga, jobs.qtd_vagas, company.name as nome_company, jobs_status.name as      status_name, DATEDIFF(NOW(), jobs.data_abertura) as date_idade, 
(select count(job_cv.jobs_id) from job_cv where job_cv.jobs_id = jobs.jobs_id) as qtd_int,
(select count(distinct job_cv.user_id) from job_cv_history join job_cv on job_cv.job_cv_id = job_cv_history.job_cv_id where job_cv_history.status = '11' and job_cv.jobs_id = jobs.jobs_id ) as fechadas 
FROM (jobs)
JOIN clients ON lients.clients_id=jobs.clients_idJOIN user_profiles ON jobs.consultor_id=user_profiles.user_id
JOIN jobs_status ON jobs.status=jobs_status.jobs_status_id 
JOIN company ON jobs.company_id=company.company_id 
LIMIT 50

有人可以帮助我吗?如果需要,我可以提供更多信息。

更新

使用JOIN而不是SELECT的想法与第一个子查询一起使用,但第二个子查询没有,有一种方法可以传递'变量'在子查询中使用吗?像当前的jobs_id一样?

再次更新

这条线本身很好用。但是在子查询中需要花费大约一分钟的时间来确定值?

SELECT job_cv.jobs_id,count(distinct job_cv.user_id) AS fechadas
FROM job_cv_history 
JOIN job_cv 
ON job_cv.job_cv_id = job_cv_history.job_cv_id 
WHERE job_cv_history.status = '11'
GROUP BY job_cv.jobs_id

3 个答案:

答案 0 :(得分:3)

子查询不是很慢。事实上,您正在为从外部查询返回的每一行执行这些子查询。相反,将它们移动到连接处,您应该观察到性能的提高。

SELECT 
  jobs.jobs_id, clients.nome_fantasia, concat_ws(' ', user_profiles.first_name, user_profiles.last_name) as fullname, 
jobs.titulo_vaga, jobs.qtd_vagas, company.name as nome_company, jobs_status.name as      status_name, DATEDIFF(NOW(), jobs.data_abertura) as date_idade, 
qtd.qtd_int,
fechadas.fechadas 
FROM (jobs)
JOIN clients ON lients.clients_id=jobs.clients_idJOIN user_profiles ON jobs.consultor_id=user_profiles.user_id
JOIN jobs_status ON jobs.status=jobs_status.jobs_status_id 
JOIN company ON jobs.company_id=company.company_id 
JOIN (
  SELECT jobs_id, count(jobs_id) AS qtd_int FROM job_cv GROUP BY jobs_id
) AS qtd ON qtd.jobs_id = jobs.jobs_id
JOIN (
  SELECT job_cv.user_id, count(distinct job_cv.user_id) AS fechadas
  FROM job_cv_history 
  JOIN job_cv 
  ON job_cv.job_cv_id = job_cv_history.job_cv_id 
  WHERE job_cv_history.status = '11'
  GROUP BY job_cv.user_id
) AS fechadas ON job_cv.jobs_id = jobs.jobs_id
LIMIT 50

答案 1 :(得分:2)

您可以尝试创建这些索引:

ALTER TABLE `job_cv` ADD INDEX `job_cv_cindex` (`job_cv_id` ASC, `jobs_id` ASC, `user_id` ASC);
ALTER TABLE `job_cv_history` ADD INDEX `job_cv_history_cindex` (`job_cv_id` ASC, `status` ASC);

答案 2 :(得分:1)

使用联接而不是子查询。它显着提高了MySql的性能。 尝试在你的案例中使用左连接,看看性能是否有所改善