I have a sql that takes 1:20 min to execute. It processes a year of data but even so i feel it takes too long. I changed the IN uses with EXISTS as recommended for another query (in this case that optimization wasnt enought :S )
Do you have another suggerence to optimize it?
select gd.descripcion,count(gd.descripcion) as total
from diagnosticos d,gruposdiagnosticos gd, ServiciosMedicos s, pacientes p,Actos a,historias h
where p.codigo=h.codpaciente and p.codigo=a.codpaciente and p.codigo=h.codpaciente and p.codigo=s.codpaciente and h.codpaciente=a.codpaciente and h.codpaciente=s.codpaciente and a.codpaciente=s.codpaciente and h.numhistoria=a.numhistoria and h.numhistoria=s.numhistoria and a.numacto=s.numacto and h.codseccion=a.codseccion and a.codseccion=s.codseccion and d.codigo=s.codDiagnostico and gd.codigo=d.codgrupo
and p.codcompañia ='35' and a.codseccion ='18'
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 NOT EXISTS (select * from diasfestivos af where (datename(d,a.fecatencion) + datename(m,a.fecatencion))=(datename(d,af.fechafestiva) + datename(m,af.fechafestiva)) AND CAST(FLOOR(CAST(a.fecAtencion AS float)) AS datetime) >= af.fechafestiva AND CAST(FLOOR(CAST(a.fecAtencion AS float)) AS datetime) < af.fechafestiva + 1 and datepart(yy,af.fechafestiva)='1990')
AND NOT EXISTS (SELECT * FROM diasfestivos af WHERE CAST(FLOOR(CAST(a.fecAtencion AS float)) AS datetime) >= af.fechafestiva AND CAST(FLOOR(CAST(a.fecAtencion AS float)) AS datetime) < af.fechafestiva + 1 AND datepart(yy,af.fechafestiva)!=1990)
group by gd.descripcion order by gd.descripcion
Changing the query with your suggestions it makes it in 50 secs, thanks but there should be a way to reduce it more…The query now is:
select gd.descripcion,count(gd.descripcion) as total
from diagnosticos d,gruposdiagnosticos gd, ServiciosMedicos s, pacientes p,Actos a,historias h
where p.codigo=h.codpaciente and p.codigo=a.codpaciente and p.codigo=h.codpaciente and p.codigo=s.codpaciente and h.codpaciente=a.codpaciente and h.codpaciente=s.codpaciente and a.codpaciente=s.codpaciente and h.numhistoria=a.numhistoria and h.numhistoria=s.numhistoria and a.numacto=s.numacto and h.codseccion=a.codseccion and a.codseccion=s.codseccion and d.codigo=s.codDiagnostico and gd.codigo=d.codgrupo
and p.codcompañia ='35' and a.codseccion ='18'
and a.fecAtencion +1 >'20090101'
and a.fecAtencion -1 <'20091231'
and h.modo ='Urgente'
and DATEPART(dw,a.fecatencion)!=6
and DATEPART(dw,a.fecatencion)!=7
AND NOT EXISTS (select * from diasfestivos af where (datename(d,a.fecatencion) + datename(m,a.fecatencion))=(datename(d,af.fechafestiva) + datename(m,af.fechafestiva)) AND a.fecAtencion +1 > af.fechafestiva AND a.fecAtencion -1 < af.fechafestiva and datepart(yy,af.fechafestiva)='1990')
AND NOT EXISTS (SELECT * FROM diasfestivos af WHERE a.fecAtencion +1 > af.fechafestiva AND a.fecAtencion -1 < af.fechafestiva AND datepart(yy,af.fechafestiva)!=1990)
group by gd.descripcion order by gd.descripcion
I have 2 exists parts because i have two types of festivity dates. Ones especifical to the present year and others that apply for everyyear (so i insert them like 25/12/1990)
Finally I have found the problem..its in this part:
where datename(d,a.fecatencion) + datename(m,a.fecatencion))=(datename(d,af.fechafestiva) + datename(m,af.fechafestiva))
Someone knows a better way of doing it? (Compare 2 datetimes in tsql omiting the year)
You should learn how to use “DateDiff”. The query should use this construct to get only records from 2009:
A few other things…
FIRST – Don’t do the casts for date.
Static dates (e.g. ‘2009-01-01’) have an implied midnight (00:00:00) start time. Thus, you won’t need the cast at all for the starting date since effectively everything in 2009 is greater than ‘2009-01-01’. You could also use ‘2010-01-01’ as your end date since everything in 2009 (and nothing in 2010) will be before that.
Another reason not to use these ugly casts is that you can specify hours, minutes, seconds and msecs in a SQL Query like so: ‘2009-01-01 00:00:00:000’ if you don’t want to leave the query ambiguous in this area.
The other advice about using the profiler, etc. is still good. But I bet your problem lies in your date handling.