这几天测试java内存数据库,和oracle比较时发下一个update from语句很慢,如下:

update business_new
set fare1_balance_ratio = (select BALANCE_RATIO from bfare2
where bfare2.exchange_type = business_new.exchange_type and
bfare2.stock_type = business_new.stock_type and
(bfare2.entrust_way = business_new.entrust_way) and
(bfare2.entrust_type = business_new.entrust_type)
and bfare2.fare_type = '')

执行计划是这样的:

从执行计划可以看出,走的就是nl关联,所以慢是正常的。

于是将其改写为merge,如下:

merge into business_new using bfare2
on (bfare2.exchange_type = business_new.exchange_type and
bfare2.stock_type = business_new.stock_type and
(bfare2.entrust_way = business_new.entrust_way) and
(bfare2.entrust_type = business_new.entrust_type)
and bfare2.fare_type = '')
when matched then update
set business_new.farex_balance_ratio = bfare2.BALANCE_RATIO

改写后执行计划如下:

很快就跑出来了。需要注意的是,update语句本身是通过hint让两表强制走hash join的。

除了用merge改写让两表关联走hash join外,还有一种更优、但有条件的做法。如下:

update (select fare1_balance_ratio,BALANCE_RATIO from business_new,bfare2
where bfare2.exchange_type = business_new.exchange_type and
bfare2.stock_type = business_new.stock_type and
(bfare2.entrust_way = business_new.entrust_way) and
(bfare2.entrust_type = business_new.entrust_type)
and bfare2.fare_type = '')
set fare1_balance_ratio = BALANCE_RATIO ;

这也称为inline view更新法,性能是最好的,但相比merge并不明显。但表B的主键一定要在where条件中,并且是以“=”来关联被更新表,否则会遇到ORA-01779: 无法修改与非键值保存表对应的列。造成这个错误的原因是更新的列不是事实表的列,而是维度表的列。换句话说,如果两张表关联,其中一张表的关联列是主键,那么另一张表就是事实表,也就是说另一张表中的列就是可更新的;除非另一张表的关联列也是主键,否则这张表就是不可更新的,如果更新语句涉及到了这张表,就会出现ORA-1799错误。也就是,要么两张表都通过PK关联,要么只有非PK这张表可更新。

至于for循环,乖乖,除非逻辑特别复杂,用for bulk collect,否则不要考虑。

最新文章

  1. md语法
  2. WPF面试准备
  3. 获取文件hash值
  4. Tornado实战项目(伪JD商城)
  5. 药企信息sop
  6. Sql Server来龙去脉系列 必须知道的权限控制核心篇
  7. 记一次系统稳定性问题的分析处理过程(因CallContext使用不当而造成bug)
  8. QSS的作用需要正确设置文件编码才能起作用
  9. SQLite数据插入异常
  10. Fast CGI 工作原理
  11. 全球顶级专家为你解读:什么是真正的 DevOps?
  12. Powerful Regex
  13. NSThread 的用法
  14. org.springframework.core.Ordered接口
  15. HttpClient--HttpGet使用
  16. LoadRunner基础知识
  17. [Swift]LeetCode238. 除自身以外数组的乘积 | Product of Array Except Self
  18. 表单组件 form fastadmin(生成表单元素)
  19. DP爬台阶问题
  20. 在云服务器跑Python程序

热门文章

  1. Swagger Liunx环境搭建(亲测百分百可用)
  2. SpringCloud-Zuul源码分析和路由改造
  3. postgres常用运维sql
  4. TreeMap 的简单解释
  5. sort()函数中的key
  6. 【NOIP2015】斗地主 D1 T3 及 增强版 (送命题)
  7. [ARIA] Add aria-expanded to add semantic value and styling
  8. EFK架构图
  9. c++ main函数
  10. 洛谷 P2939 [USACO09FEB]改造路Revamping Trails 题解