如何优化此查询?

时间:2010-08-28 00:37:30

标签: sql-server-2005 oracle plsql

如果给定以下查询返回表中的所有条目或仅匹配当前日期的条目,如何优化此查询? 顺便说一句:查询的目标是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中。

4 个答案:

答案 0 :(得分:3)

永远不要对日期列执行任何功能处理(t $ Tdat和t $ stdt属于这种类型,不是吗?),除非你有相应的基于函数的索引。这种方法不允许您在t $ stdt和t $ Tdat上使用索引并显着降低性能。

相反,我会用以下方式重写where子句:

where t$Tdat >= current_date and t$stdt <= current_date

如果current_date属于date类型。如果不是,那么您可以使用to_date(current_date, 'DD-MM-YYYY')代替它。

答案 1 :(得分:1)

以防be here now的提示 - 这是一个好的 - 不起作用: 你需要收集一些数据才能知道花费的时间。请阅读此OTN线程以了解如何为Oracle执行此操作:http://forums.oracle.com/forums/thread.jspa?messageID=1812597。对于SQL Server,适用相同的原则:使用他们的工具找出此查询花费时间的位置。

您可以分享的一些一般信息是:

  • 这两个表中有多少行
  • 该查询返回了多少行
  • 这两个表中存在哪些索引
  • 查询当前需要多长时间
  • 可接受的响应时间,即我们何时完成调整

此致 罗布。

答案 2 :(得分:0)

不确定这会提高多少性能,但我要做的第一件事就是用日期函数替换日期到字符串转换。也就是说,使用trunc()而不是to_char()。

答案 3 :(得分:0)

通过以下方式,您可以优化Baan查询

  • 在条件条件下使用索引并在可能的情况下合并字段。
  • 在何处条件指定上限和下限时使用“间隔/范围”。
  • 如果引用在数据字典中可用,请使用“引用”
  • 尽可能少使用重叠的“或”条件
  • 在选择语句中仅使用表的选定字段,这实际上是必需的。
  • 使用“排序依据”以正确的排序格式获取记录
  • 如果可能,请不要使用NOT INRANGE,BETWEEN,IN运算符,因为该运算符可以扫描整个表。
  • 使用commit.transaction()防止行被打印两次。