sql >> Base de Datos >  >> RDS >> Mysql

Subconsulta lenta en MySQL

No es subconsulta que es lento. Es el hecho de que está ejecutando estas subconsultas para cada fila devuelta desde la consulta externa. En su lugar, muévalos a combinaciones y debería observar un aumento en el rendimiento.

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