如果给定以下查询,则返回表中的所有条目或仅匹配当前日期的条目,如何优化此查询?顺便说一句:该查询针对MS Sql 2005上的Oracle链接服务器作为内联函数。。不要将此作为表值函数。
ALTER function [dbo].[ftsls031nnnHades](@withExpiredEntries bit =0) returns table as return select * from openQuery(Hades ,"select '010' comno, trim(t$cuno) t$cuno, trim(t$cpgs) t$cpgs, t$dile, t$qanp, to_char(t$stdt,'dd Mon yy') t$stdt, to_char(t$tdat,'dd Mon yy') t$tdat, to_char(t$disc,'999.99') t$disc, t$damt, t$cdis, t$gnpr, t$refcntd, t$refcntu from baan.ttdsls031010 where (to_char(t$Tdat,'yyyy-mm-dd') >= To_char(current_date,'yyyy-mm-dd')) and (to_char(t$stdt,'yyyy-mm-dd') <= To_char(current_date,'yyyy-mm-dd')) union all select '020' comno, trim(t$cuno) t$cuno, trim(t$cpgs) t$cpgs, t$dile,t$qanp, to_char(t$stdt,'dd Mon yy') t$stdt, to_char(t$tdat,'dd Mon yy') t$tdat, to_char(t$disc,'999.99') t$disc, t$damt, t$cdis, t$gnpr, t$refcntd, t$refcntu from baan.ttdsls031020 where (to_char(t$tdAt,'yyyy-mm-dd') >= To_char(current_date,'yyyy-mm-dd')) and (to_char(t$stdt,'yyyy-mm-dd') <= To_char(current_date,'yyyy-mm-dd')) ")
p.s:列命名约定可能与非BaaN的约定不一样。请原谅我将'BaaN'约定引入StackOverflow。
如果给定以下查询,则返回表中的所有条目或仅匹配当前日期的条目,如何优化此查询?顺便说一句:该查询针对Oracle链接服务器...
除非您具有相应的基于函数的索引,否则请不要对日期列执行任何功能处理(t $ Tdat和t $ stdt属于这种类型吗?)。这种方法不允许您在t $ stdt和t $ Tdat上使用索引,从而显着降低性能。
[如果有be here now
的提示-这是一个好提示-不起作用:您需要收集一些数据以了解时间在哪里。请阅读此OTN线程,以了解如何针对Oracle:http://forums.oracle.com/forums/thread.jspa?messageID=1812597。对于SQL Server,适用相同的原则:使用其工具来查找此查询在哪里花费时间。
不确定这将在多大程度上提高性能,但是我要做的第一件事就是用日期函数替换日期到字符串的转换。也就是说,使用trunc()而不是to_char()。
通过以下方式可以优化Baan查询