Mysql
 sql >> Database >  >> RDS >> Mysql

Sottoquery lenta in MySQL

Non è la sottoquery che è lenta. È il fatto che stai eseguendo queste sottoquery per ogni riga restituita dalla query esterna. Spostali invece nei join e dovresti osservare un aumento delle prestazioni.

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