I have a sql that takes 7 min to execute. It processes a year of data but even so i feel it takes too long. Do you have any suggerences to optimize it?
select count(s.numserviciomedico) AS total
from Actos a,
pacientes p,
Historias h,
serviciosmedicos s
where p.codigo=h.codpaciente
AND p.codigo=a.codPaciente
AND p.codigo = s.codPaciente
AND h.codPaciente = a.codPaciente
AND a.codpaciente=s.codPaciente
AND h.numHistoria = a.numHistoria
AND h.numHistoria = s.numHistoria
AND a.numHistoria = s.numHistoria
AND a.numActo = s.numActo
AND h.codSeccion=a.codSeccion
and p.codcompañia ='38'
and a.codseccion ='9'
and (CAST(FLOOR(CAST(a.fecAtencion AS float)) AS datetime) >='20090101')
and (CAST(FLOOR(CAST(a.fecAtencion AS float)) AS datetime) <='20091231')
and h.modo ='Urgente'
and datename(weekday,a.fecatencion)!= 'Sabado'
and datename(weekday,a.fecatencion)!= 'Domingo'
and CAST(FLOOR(CAST(a.fecAtencion AS float)) AS datetime) NOT IN (
select fechafestiva
from diasfestivos
)