我遇到的问题是,当选择超过30年的日期范围时,以下查询会导致一些超时问题。
以下可以提高效率吗?
select TB_INJECTIONANDPRODUCTION.STANDARDOILFIELD,
TB_INJECTIONANDPRODUCTION.WELLNAME,
to_char(TB_INJECTIONANDPRODUCTION.MEASUREMENTDATE,'YYYY'),
sum(TB_INJECTIONANDPRODUCTION.MONTHLYOILPRODUCTION),
sum(TB_INJECTIONANDPRODUCTION.MONTHLYGASPRODUCTION),
sum(TB_INJECTIONANDPRODUCTION.CONDENSATE),
sum(TB_INJECTIONANDPRODUCTION.FREEGAS),
sum(TB_INJECTIONANDPRODUCTION.MONTHLYWATERPRODUCTION),
sum(TB_INJECTIONANDPRODUCTION.MONTHLYWATERINJECTION)
from TB_INJECTIONANDPRODUCTION
where
TB_INJECTIONANDPRODUCTION.ENTITLEMENT like 'AZNEFT_28May%' and
TB_INJECTIONANDPRODUCTION.STANDARDOILFIELD = ? and
TB_INJECTIONANDPRODUCTION.WELLNAME = ? and
to_char(TB_INJECTIONANDPRODUCTION.MEASUREMENTDATE,'YYYY') >= ? and
to_char(TB_INJECTIONANDPRODUCTION.MEASUREMENTDATE,'YYYY') <= ?
group by
TB_INJECTIONANDPRODUCTION.STANDARDOILFIELD,
TB_INJECTIONANDPRODUCTION.WELLNAME,
to_char(TB_INJECTIONANDPRODUCTION.MEASUREMENTDATE,'YYYY')
order by
TB_INJECTIONANDPRODUCTION.STANDARDOILFIELD,
TB_INJECTIONANDPRODUCTION.WELLNAME,
to_char(TB_INJECTIONANDPRODUCTION.MEASUREMENTDATE,'YYYY')
非常感谢你。
答案 0 :(得分:1)
我建议您在查询时做一些事情。
首先使用EXTRACT而不是TO_CHAR,因为日期提取更快。
to_char(TB_INJECTIONANDPRODUCTION.MEASUREMENTDATE,'YYYY')
要
extract(YEAR From TB_INJECTIONANDPRODUCTION.MEASUREMENTDATE)
避免这种类型的操作
to_char(TB_INJECTIONANDPRODUCTION.MEASUREMENTDATE,'YYYY') >= ?
最有可能进行隐式转换(这对性能非常不利)。我假设您将数字传递给此参数。所以在这里再次使用提取。
请参阅其他参数类型和数据库字段类型以避免隐式转换。例如。如果TB_INJECTIONANDPRODUCTION.STANDARDOILFIELD是一个数字,则将参数作为数字传递。
使用between
避免两次调用提取函数:
to_char(TB_INJECTIONANDPRODUCTION.MEASUREMENTDATE,'YYYY') >= ? and
to_char(TB_INJECTIONANDPRODUCTION.MEASUREMENTDATE,'YYYY') <= ?
对此:
extract(year from TB_INJECTIONANDPRODUCTION.MEASUREMENTDATE) between ? and ?
这只是建议。没有关于你的问题的更多信息,我可以建议。