有人能解释一下 Oracle 中 MERGE 语句的真正作用吗?

发布于 2024-10-15 04:44:09 字数 534 浏览 3 评论 0原文

我正在寻找 Oracle 中 MERGE 语句真正作用的清晰解释。

这就是我所追求的:

MERGE INTO (target_table) t
USING (source_view) s
   ON (join condition)
 WHEN MATCHED THEN UPDATE SET col1 = val1 [, ...]
 WHEN NOT MATCHED THEN INSERT (col1 [, ...]) VALUES ( val1 [, ...])
  • 执行什么样的连接?我认为这是完全外连接,对吗?
  • 关于 WHEN MATCHED 部分:当 t 中的一行与 s 中的多行匹配时会发生什么?
  • 关于 WHEN NOT MATCHED 部分,我认为这意味着“当 s 中的一行在 t 中没有对应关系时” 。我说得对吗?

谢谢。

I am looking for a clear explanation of what the MERGE statement in Oracle really does.

Here is what I am after:

MERGE INTO (target_table) t
USING (source_view) s
   ON (join condition)
 WHEN MATCHED THEN UPDATE SET col1 = val1 [, ...]
 WHEN NOT MATCHED THEN INSERT (col1 [, ...]) VALUES ( val1 [, ...])
  • what kind of join is performed? I think it is full outer join, am I right?
  • regarding the WHEN MATCHED part: what happens when a row from t matches multiple rows from s?
  • regarding the WHEN NOT MATCHED part I believe it means “when a row in s has no correspondence in t”. Am I right?

Thank you.

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

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

发布评论

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

评论(2

寄人书 2024-10-22 04:44:09

执行什么样的连接?我
我认为这是完全外连接,是吗
对吗?

不,这是常规的外连接。查询需要知道目标表中何时存在也在源表中的行以及源表中何时存在目标表中没有的记录。由于查询不需要响应目标表中但不在源表中的行,因此不需要双向外连接。

但是,如果没有 notmatched 子句(这是完全有效的),则不会执行外连接。优化器足够聪明,知道在这种情况下,内部联接就足够了。

关于 WHEN MATCHED 部分:什么
当 t 中的一行匹配时发生
s 中的多行?

当有多个匹配项时,针对每个匹配项执行更新。这意味着最后出现的更新将是提交中写入的更新。无法指定顺序,因此在这种情况下,更新源实际上是随机的(来自匹配集)。

正如 @ Vincent Malgrat 指出的那样,这是不正确的。如果有多个匹配,Oracle 似乎会产生“ORA-40926:无法在源表中获取稳定的行集”错误。

关于“何时不匹配”第一部分
相信这意味着“当 s 中的一行有
t”中没有对应关系。我说得对吗?

这是正确的。

what kind of join is performed? I
think it is full outer join, am I
right?

No, it's a regular outer join. The query needs to know when there are rows in the target table that are also in the source table and when there are records in the source table that are not in the target table. Since the query doesn't need to respond to rows that are in the target table but are not in the source table, it doesn't need the outer join to go both ways.

However, the outer join will not be performed if there is no not matched clause (which is perfectly valid). The optimizer is smart enough to know that in that case, an inner join is sufficient.

regarding the WHEN MATCHED part: what
happens when a row from t matches
multiple rows from s?

When there are multiple matches, the update is performed for each match. This means that whichever update comes last will be the one written in the commit. There's no way to dictate an order, so in this case the source of the update is effectively random (from the set of matches).

As @ Vincent Malgrat pointed out, this was incorrect. It seems that Oracle will produce an "ORA-40926: unable to get a stable set of rows in the source table" error if there are multiple matches.

regarding the WHEN NOT MATCHED part I
believe it means “when a row in s has
no correspondence in t”. Am I right?

That is correct.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文