加入收藏 | 设为首页 | 会员中心 | 我要投稿 湖南网 (https://www.hunanwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长百科 > 正文

oracle update from多表机能优化一例

发布时间:2021-01-11 16:19:58 所属栏目:站长百科 来源:网络整理
导读:这几天测试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 = bu

这几天测试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 = ‘0‘)

执行打算是这样的:

oracle update from多表机能优化一例

从执行打算可以看出,走的就是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 = ‘4‘)
        when matched then update
           set business_new.farex_balance_ratio = bfare2.BALANCE_RATIO

改写后执行打算如下:

oracle update from多表机能优化一例

很快就跑出来了。必要留意的是,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 = ‘0‘)
           set fare1_balance_ratio = BALANCE_RATIO ;

oracle update from多表机能优化一例

这也称为inline view更新法,机能是最好的,但对比merge并不明明。但表B的主键必然要在where前提中,而且是以“=”来关联被更新表,不然会碰着ORA-01779:?无法修改与非键值生涯表对应的列。造成这个错误的缘故起因是更新的列不是究竟表的列,而是维度表的列。换句话说,假如两张表关联,个中一张表的关联列是主键,那么另一张表就是究竟表,也就是说另一张表中的列就是可更新的;除非另一张表的关联列也是主键,不然这张表就是不行更新的,假如更新语句涉及到了这张表,就会呈现ORA-1799错误。也就是,要么两张表都通过PK关联,要么只有非PK这张表可更新。

至于for轮回,乖乖,除非逻辑出格伟大,用for bulk collect,不然不要思量。

(编辑:湖南网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读