简体   繁体   中英

how to optimize subquery mysql

I have the following query, from which I obtain the expected results, the problem is that it takes a long time to return the data, I know that the slowness is due to the following line

(select fecha_hora 
 from horas
 where horas.consulta_id = consultas.id
 AND fecha_hora >= NOW() 
 AND horas.estado = 0
 ORDER BY fecha_hora limit 0, 1) as hora 

What I do not know is how to solve it.

The objective is to obtain the date and time closest to the actual day available that a doctor has (there are doctors who do not have availability)

select `medicos`.`id` as `med_id`
, `medicos`.`estado` as `med_estado`
, `medicos`.`descripcion`
, `clinicas`.`nombre` as `cli_nombre`
, `clinicas`.`direccion` as `cli_direccion`
, `regiones`.`nombre` as `region`
, `comunas`.`nombre` as `comuna`
, (select fecha_hora 
   from horas 
   where horas.consulta_id = consultas.id 
   AND fecha_hora >= NOW() 
   AND horas.estado = 0 
   ORDER BY fecha_hora 
   limit 0, 1) as hora
from `consultas` 
inner join `consulta_especialidad` 
 on `consulta_especialidad`.`consulta_id` = `consultas`.`id` 
inner join `especialidades` 
  on `especialidades`.`id` = `consulta_especialidad`.`especialidad_id` 
inner join `medicos` 
  on `medicos`.`id` = `consultas`.`medico_id` 
inner join `clinicas` 
  on `clinicas`.`id` = `consultas`.`clinica_id` 
inner join `regiones` 
  on `regiones`.`id` = `clinicas`.`region_id` 
inner join `comunas` on `comunas`.`id` = `clinicas`.`comuna_id` 
where `medicos`.`estado` != 5 
and `clinicas`.`region_id` = 15 
and `especialidades`.`id` = 116 
group by `medicos`.`id` 
order by `medicos`.`estado` desc
 , -`hora` desc, `medicos`.`nombres` asc
 , `medicos`.`apellidos` asc

Not an answer. Too long for a comment...

FWIW, I find this easier to read...

SELECT m.id med_id
     , m.estado med_estado
     , m.nombre_completo med_nombre_completo
     , m.seguridad med_seguridad
     , m.foto_perfil med_foto_perfil
     , m.genero med_genero
     , m.descripcion
     , l.nombre cli_nombre
     , l.direccion cli_direccion
     , l.id cli_id
     , l.telefono cli_telefono
     , l.link_agenda cli_link_agenda
     , r.nombre region
     , c.nombre comuna
     , (SELECT fecha_hora 
          FROM horas h
         WHERE h.consulta_id = k.id 
           AND fecha_hora >= NOW() 
           AND h.estado = 0 
         ORDER 
            BY fecha_hora 
         LIMIT 0,1
       ) hora
  FROM consultas k
  JOIN consulta_especialidad ke 
    ON ke.consulta_id = k.id 
  JOIN especialidades e
    ON e.id = ke.especialidad_id 
  JOIN medicos m 
    ON m.id = k.medico_id 
  JOIN clinicas l
    ON l.id = k.clinica_id 
  JOIN regiones r
    ON r.id = l.region_id 
  JOIN comunas c 
    ON c.id = l.comuna_id 
 WHERE m.estado != 5 
   AND l.region_id = 15 
   AND e.id = 116 
 GROUP 
    BY m.id 
 ORDER 
    BY m.estado DESC
     , -hora DESC -- ISN'T THIS THE SAME AS ASC???
     , m.nombres ASC
     , m.apellidos ASC

An observation: You have a GROUP BY clause, but no aggregating functions. This is a recipe for disaster.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM