"Set" 使用 "where exists" 比不使用时表现更好

"Set" with "where exists" performs better then without

偶遇这个奇案

环境:

据我所知,并在此处报告 Oracle / PLSQL: EXISTS Condition where exists 的使用通常不如其他方式。

然而,在我的例子中,当使用存在的连接条件更新 table 的列时,查询 运行 在大约 12-13 秒内没有问题(我做了只是一些检查,因为我真的不知道table):

的所有内容
update fdm_auftrag ou
set (ou.e_hr,ou.e_budget) =  ( select b.e_hr,b.e_budget
                 from  fdm_budget_auftrag b 
                where b.fk_column1 = ou.fk_column1
                 and b.fk_column2 = ou.fk_column2
                 and  b.fk_col3 = ou.fk_col3 )
where exists ( select b.e_hr,b.e_budget
                 from  fdm_budget_auftrag b 
                where b.fk_column1 = ou.fk_column1
                 and b.fk_column2 = ou.fk_column2
                 and  b.fk_col3 = ou.fk_col3  );

反倒是没有exists,花那么多时间我都打断了

我只是在猜测,因为存在的条件被评估为布尔值,如果工程师发现至少一行,那么就不得不减少对数据库的接触,但我不确定。

这个"guess"是正确的,谁有更清楚的解释?

where 子句限制更新的行数。

更少的更新行意味着 update 查询运行得更快。更新行有很多开销,包括为回滚目的隐藏信息。

我假设您在更大的 table 中更新相对较少的行。如果 where 子句选择了大部分行,则可能没有性能差异。

最后,这两个查询并不相同。没有 where 不匹配的值将被分配 NULL.