<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    海上月明

    editer by sun
    posts - 162, comments - 51, trackbacks - 0, articles - 8
       :: 首頁 :: 新隨筆 ::  :: 聚合  :: 管理

    from:http://www.webjx.com/htmldata/2006-01-19/1137630095.html

    為了方便起見,建立了以下簡單模型,和構造了部分測試數據:
      在某個業務受理子系統BSS中,
      --客戶資料表
      create table customers
      (
       customer_id   number(8)    not null,  -- 客戶標示
       city_name     varchar2(10) not null,  -- 所在城市
       customer_type char(2)      not null,  -- 客戶類型
     
       ...
      )
      create unique index PK_customers on customers (customer_id)
      由于某些原因,客戶所在城市這個信息并不什么準確,但是在
      客戶服務部的CRM子系統中,通過主動服務獲取了部分客戶20%的所在
      城市等準確信息,于是你將該部分信息提取至一張臨時表中:
      create table tmp_cust_city
      (
       customer_id    number(8) not null,
       citye_name     varchar2(10) not null,
       customer_type  char(2)   not null
      )

    1) 最簡單的形式
       --經確認customers表中所有customer_id小于1000均為'北京'
       --1000以內的均是公司走向全國之前的本城市的老客戶:)
       update customers
       set    city_name='北京'
       where  customer_id<1000

    2) 兩表(多表)關聯update -- 僅在where字句中的連接
       --這次提取的數據都是VIP,且包括新增的,所以順便更新客戶類別
       update customers  a       -- 使用別名
       set    customer_type='01' --01 為vip,00為普通
       where  exists (select 1 
                      from   tmp_cust_city b
                      where  b.customer_id=a.customer_id
                     )

    3) 兩表(多表)關聯update -- 被修改值由另一個表運算而來
       update customers a   -- 使用別名
       set    city_name=(select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id)
       where  exists (select 1 
                      from   tmp_cust_city b
                      where  b.customer_id=a.customer_id
                     )
       -- update 超過2個值
       update customers a   -- 使用別名
       set    (city_name,customer_type)=(select b.city_name,b.customer_type 
                                         from   tmp_cust_city b 
                                         where  b.customer_id=a.customer_id)
       where  exists (select 1 
                      from   tmp_cust_city b
                      where  b.customer_id=a.customer_id
                     )
       注意在這個語句中,
                                       =(select b.city_name,b.customer_type 
                                         from   tmp_cust_city b 
                                         where  b.customer_id=a.customer_id
                                        )
       與
                     (select 1 
                      from   tmp_cust_city b
                      where  b.customer_id=a.customer_id
                     )
       是兩個獨立的子查詢,查看執行計劃可知,對b表/索引掃描了2篇;
       如果舍棄where條件,則默認對A表進行全表
       更新,但由于(select b.city_name from tmp_cust_city b where where  b.customer_id=a.customer_id)
       有可能不能提供"足夠多"值,因為tmp_cust_city只是一部分客戶的信息,
       所以報錯(如果指定的列--city_name可以為NULL則另當別論):
       
    01407, 00000, "cannot update (%s) to NULL"
    // *Cause:
    // *Action:

       一個替代的方法可以采用:
       update customers a   -- 使用別名
       set    city_name=nvl((select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id),a.city_name)
       或者
       set    city_name=nvl((select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id),'未知')
       -- 當然這不符合業務邏輯了

    4) 上述3)在一些情況下,因為B表的紀錄只有A表的20-30%的紀錄數,
       考慮A表使用INDEX的情況,使用cursor也許會比關聯update帶來更好的性能:
       
    set serveroutput on

    declare
        cursor city_cur is
        select customer_id,city_name
        from   tmp_cust_city
        order by customer_id;
    begin
        for my_cur in city_cur loop

            update customers
            set    city_name=my_cur.city_name
            where  customer_id=my_cur.customer_id;
           
           /** 此處也可以單條/分批次提交,避免鎖表情況 **/
    --     if mod(city_cur%rowcount,10000)=0 then
    --        dbms_output.put_line('----');
    --        commit;
    --     end if;
        end loop;
    end;

    5) 關聯update的一個特例以及性能再探討
       在oracle的update語句語法中,除了可以update表之外,也可以是視圖,所以有以下1個特例:
        update (select a.city_name,b.city_name as new_name
                from   customers a,
                       tmp_cust_city b
                where  b.customer_id=a.customer_id
               )
        set    city_name=new_name
        這樣能避免對B表或其索引的2次掃描,但前提是 A(customer_id) b(customer_id)必需是unique index
        或primary key。否則報錯:
        
    01779, 00000, "cannot modify a column which maps to a non key-preserved table"
    // *Cause: An attempt was made to insert or update columns of a join view which
    //         map to a non-key-preserved table.
    // *Action: Modify the underlying base tables directly.

    6)oracle另一個常見錯誤
       回到3)情況,由于某些原因,tmp_cust_city customer_id 不是唯一index/primary key
       update customers a   -- 使用別名
       set    city_name=(select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id)
       where  exists (select 1 
                      from   tmp_cust_city b
                      where  b.customer_id=a.customer_id
                     )
       當對于一個給定的a.customer_id
       (select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id)
       返回多余1條的情況,則會報如下錯誤:
       
    01427, 00000, "single-row subquery returns more than one row"
    // *Cause:
    // *Action:

       一個比較簡單近似于不負責任的做法是
       update customers a   -- 使用別名
       set    city_name=(select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id)

       如何理解 01427 錯誤,在一個很復雜的多表連接update的語句,經常因考慮不周,出現這個錯誤,
       仍已上述例子來描述,一個比較簡便的方法就是將A表代入 值表達式 中,使用group by 和
       having 字句查看重復的紀錄
       (select b.customer_id,b.city_name,count(*)
        from tmp_cust_city b,customers a 
        where b.customer_id=a.customer_id
        group by b.customer_id,b.city_name
        having count(*)>=2
       )


    評論

    # re: ORACLE UPDATE 語句語法與性能分析看法【轉】  回復  更多評論   

    2013-03-06 18:10 by 324
    234

    # re: ORACLE UPDATE 語句語法與性能分析看法【轉】  回復  更多評論   

    2013-03-06 18:10 by 324
    2342342342343

    只有注冊用戶登錄后才能發表評論。


    網站導航:
    博客園   IT新聞   Chat2DB   C++博客   博問  
     
    主站蜘蛛池模板: 亚洲国产免费综合| 好男人视频社区精品免费| 亚洲成片观看四虎永久| 亚洲av无码一区二区三区人妖 | 青草草在线视频永久免费| 亚洲一区动漫卡通在线播放| 日韩免费一区二区三区在线 | 2022年亚洲午夜一区二区福利| 男人进去女人爽免费视频国产| 亚洲成AV人片一区二区| 亚洲视频在线观看免费| 亚洲美女视频网站| 欧美三级在线电影免费| 亚洲另类自拍丝袜第五页| 国产免费一区二区三区VR| 免费国产黄网站在线观看动图| 久久久青草青青国产亚洲免观 | 亚洲色图激情文学| 国内一级一级毛片a免费| 国产亚洲精品精品精品| 亚洲最大av无码网址| 日本中文字幕免费高清视频| 亚洲国产成人久久| 国产在线观看免费视频播放器| 手机永久免费的AV在线电影网| 国产亚洲3p无码一区二区| 99re6在线精品视频免费播放| 亚洲午夜成激人情在线影院| 麻豆国产精品入口免费观看| 一区二区免费电影| 911精品国产亚洲日本美国韩国| 成人免费视频网址| 91视频免费网站| 亚洲精品第一国产综合野| 亚洲?v女人的天堂在线观看| 日韩电影免费在线观看中文字幕| 国产人成亚洲第一网站在线播放| 亚洲成a人片在线观看日本麻豆| 日韩人妻一区二区三区免费| 亚洲av无码专区亚洲av不卡| 亚洲AV福利天堂一区二区三|