【问题标题】:update with exists in oracle在 oracle 中存在更新
【发布时间】:2022-02-07 20:08:46
【问题描述】:

我有这个问题:

update rexcms e
   set e.cocnd=(select r.cocnd
                  from rexcms r
                 where NVL(r.marche,'*')=NVL(e.marche,'*')
                   and r.cocrd= e.cocrd
                   and r.cocol=e.cocol
                   and r.cocol='CHBIL'
                   and r.marche='CBOT')
 where e.cocol ='CDVHB' ;

如何使用 EXISTS 来优化它

【问题讨论】:

  • 您似乎对EXISTS 是什么有误解。 EXISTS 不是优化查询的工具。它旨在将查找条件添加到查询中。因此,如果您将 EXISTS 放入更新语句中,您不会对其进行增强,而是对其进行更改(通过将更新的行限制为存在 的行)。
  • 究竟如何使用 exixts 编写查询,您能帮我吗? PLZ
  • 你还是不明白。 EXISTS 更改您的查询。如果您只想更新表 abc 中存在 cocnd 的行,则添加 and exists (select null from abc where abc.cocnd = e.cocnd),但如果您只想更新 Marches 表中存在 marche 的行,则添加 and exists (select null from marches where marches.marche = e.marche)。我怎么知道您想以哪种方式更改查询的功能?我无法读心。
  • 我现在取消了正确的查询,谢谢 :)

标签: sql oracle optimization sql-update exists


【解决方案1】:

据我了解,这是经过优化的代码。您可以使用另一种语法作为 MERGE 来实现结果 -

MERGE INTO rexcms e
USING rexcms r
   ON (NVL(r.marche,'*') = NVL(e.marche,'*')
       and r.cocrd = e.cocrd
       and r.cocol = e.cocol
       and r.cocol = 'CHBIL'
       and r.marche = 'CBOT')
 WHEN MATCHED THEN UPDATE
                      SET e.cocnd = r.cocnd
                    WHERE e.cocol ='CDVHB';

【讨论】:

  • 你的意思是 :UPDATE rexcms e SET e.cocnd = ( SELECT r.cocnd FROM rexpmj r WHERE EXISTS (SELECT 1 FROM rexpmj r WHERE nvl(r.marche, '') = nvl ( e.marche, '' ) AND r.cocrd = e.cocrd AND r.cocol = e.cocol AND r.cocol = 'CHBIL' AND r.marche = 'CBOT')) 在哪里 e.cocol ='CDVHB'; ??
  • 是的。您的代码似乎已优化。但是请尝试上面的代码来比较执行时间。
【解决方案2】:

您从同一个表的行更新表。使用where e.cocol = 'CDVHB',您只能更新 CDVHB 行。使用r.cocol = e.cocol,您只能使用 CDVHB 行中的数据更新它们。使用r.cocol = 'CHBIL',您说这些行必须是 CHBIL 行。但是一行不能同时是CDVHB和CHBIL行,因为一行的cocol只能是其中之一。

因此您的查询可以简化为:

update rexcms set cocnd = null where cocol = 'CDVHB';

并且不需要进一步优化。

【讨论】:

  • cocnd = null IS NOT NULL 我必须从中获取它 (select r.cocnd from rexcms r where NVL(r.marche,'')=NVL(e.marche,' i>') and r.cocrd= e.cocrd and r.cocol=e.cocol and r.cocol='CHBIL' and r.marche='CBOT') 所以我想知道如何使用exists子句增强它
  • 不,我告诉你,你的更新语句和我的完全一样:将所有 CDVHB 行的 cocnd 设置为 null,因为你的子查询不能返回任何值,因为没有行可以符合您的标准。没有什么可以增强这一点。那会是怎样的增强呢?如果您不想进行此更新,请不要运行它。如果你想要另一个更新,那么你首先需要另一个更新语句。
猜你喜欢
  • 2020-04-04
  • 2020-10-20
  • 1970-01-01
  • 2015-09-02
  • 2014-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-27
相关资源
最近更新 更多