我该如何优化这个查询?

发布于 2024-09-16 11:25:05 字数 2122 浏览 4 评论 0原文

如果给定以下查询返回表中的所有条目或仅匹配当前日期的条目,我该如何优化此查询? 顺便说一句:查询的目标是 MS Sql 2005 上的 Oracle 链接服务器作为内联函数。不希望这是一个表值函数。ps

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'))  ")

:列命名约定对于那些非 BaaN 的人来说可能很陌生。请原谅我在 StackOverflow 中引入“BaaN”约定。

How can I optimize this query if given the following query returns either all entries in the table or entries that match only up to current date ?
btw: The Query is targeted to a Oracle Linked Server on MS Sql 2005 as an Inline function.. Do not want this to be a table value function..

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: Column naming conventions may be alien to those who are of non BaaN .. Please excuese me for bringing up 'BaaN' conventions into StackOverflow.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

拥有 2024-09-23 11:25:05

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

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

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

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

Never perform any functional processing of your date column (t$Tdat and t$stdt are of this type, aren't they?) unless you have the corresponding function-based index. This approach doesn't allow you to use indexes on t$stdt and t$Tdat and drops the perfomance dramatically.

Instead, I would rewrite the where clause in the following way:

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

if current_date is of date type. If it's not, then you can use, for example, to_date(current_date, 'DD-MM-YYYY') instead of it.

悲歌长辞 2024-09-23 11:25:05

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

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

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

问候,
抢。

Just in case be here now's tip - which is a good one - doesn't work:
you'll need to collect some data to know where time is being spent. Please read this OTN-thread to see how to do this for Oracle: http://forums.oracle.com/forums/thread.jspa?messageID=1812597. For SQL Server, the same principles apply: use their tools to find out where this query is spending time on.

Some general information you can share is:

  • How many rows are in those two tables
  • How many rows are returned by that query
  • Which indexes are present on those two tables
  • How long does the query currently take
  • What response time is acceptable, i.e. when are we done tuning

Regards,
Rob.

春花秋月 2024-09-23 11:25:05

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

Not sure how much this will improve performance, but the first thing I'd do is replace the date to string conversion with just date functions. That is, use trunc() instead of to_char().

扬花落满肩 2024-09-23 11:25:05

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

  • In Where 条件使用索引并在可能的情况下组合字段。
  • 在where 条件下指定上限和下限时使用“Between/Inrange”。
  • 如果数据字典中存在引用,则使用“Refers To”。
  • 尽可能少地使用重叠的“Or”条件。
  • 在 select 语句中仅使用表的选定字段,这实际上是必需的。
  • 使用“Order by”以正确的排序格式获取记录
  • 如果可能,不要使用 NOT INRANGE、BETWEEN、IN 运算符,因为该运算符可以扫描整个表。
  • 使用 commit.transaction() 来防止行被打印两次。

In the below way you can optimize the Baan Query

  • In Where condition use indexes and combine field if possible.
  • In where condition Use "Between/Inrange" when upper and lower limit specified.
  • Use "Refers To" if reference is available in data dictionary
  • Use few overlapping "Or" condition as possible
  • Use only selected field of table in select statement, Which is actually required.
  • Use "Order by" to get record in correct sorting format
  • If possible Don't use NOT INRANGE,BETWEEN,IN operators because that operator can scan full table.
  • Use commit.transaction() to prevent line being print twice.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文