早上好,
我得到了SQL查询的这一部分:
WITH HC AS (
SELECT Dia, Mes, Anyo, Hora, Energia AS HidroConv
FROM Calendar
LEFT OUTER JOIN OMP_PDBC_STOTA O
ON Calendar.Y = O.Anyo AND Calendar.M = O.Mes
AND Calendar.D = O.Dia AND Calendar.H = O.Hora
WHERE (Codigo = 1) AND Calendar.dt BETWEEN '12/31/2013' AND '01/01/2014')
SELECT Calendar.dt AS Fecha, Calendar.Y, Calendar.M, Calendar.D, Calendar.H,
HC.HidroConv
FROM Calendar
LEFT OUTER JOIN HC
ON Calendar.Y = HC.Anyo AND Calendar.M = HC.Mes
AND Calendar.D = HC.Dia AND Calendar.H = HC.Hora
WHERE dt BETWEEN '12/31/2013' AND '01/01/2014'
ORDER by dt, h
在WITH部分,我有另外12个查询。然后,在SELECT部分,我在WITH部分的每个SELECT上检索一列。在FROM部分,每个部分都有一个LEFT OUTER JOIN,总是在连接的左侧有Calendar。
这个大查询的执行时间超过2分钟,我迫切需要将时间缩短到15秒或更短。一些表,如OMP_PDBC_STOTA有300万行。
您对如何优化此查询有任何了解吗?
非常感谢你的帮助。
里卡多
答案 0 :(得分:0)
此查询是否提供相同的输出?
SELECT Calendar.dt AS Fecha, Calendar.Y, Calendar.M, Calendar.D, Calendar.H, Energia AS HidroConv
FROM Calendar
LEFT OUTER JOIN OMP_PDBC_STOTA O
ON Calendar.Y = O.Anyo AND Calendar.M = O.Mes
AND Calendar.D = O.Dia AND Calendar.H = O.Hora
WHERE
Calendar.dt BETWEEN '12/31/2013' AND '01/01/2014'
AND Codigo = 1
ORDER by dt, h
如果是这样,它会更快,告诉我,如果我错了,我会帮助你。
答案 1 :(得分:0)
我发现我并不需要日历来获得相同的结果。
您如何看待这种做法?
SELECT anyo, mes, dia, hora, Energia AS HidroConv
FROM OMP_PDBC_STOTA
WHERE
CAST(CAST(mes AS VARCHAR) + '/' + CAST(dia AS VARCHAR) + '/' + CAST(anyo AS VARCHAR) as datetime) BETWEEN '12/31/2013' AND '01/01/2014'
AND Codigo = 1
ORDER by mes,dia, hora
无法找到将smallints(mes,dia,anyo)转换为日期时间的正确方法。