• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Prayer

            在一般中尋求卓越
            posts - 1256, comments - 190, trackbacks - 0, articles - 0
              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

            利用游標更新數據

            Posted on 2010-05-10 23:04 Prayer 閱讀(409) 評論(0)  編輯 收藏 引用 所屬分類: DB2
            基本用法:
             cursor c1 is

                 select *

                   from tablename

                  where name is null

                    for update [of column]

             ……

             for r1 in c1 loop

                 update tablename

                    set column = ……   --已限制了條件了,更新只是當前記錄集  

                  where current of c1;
            end loop;

            where current of 子句只能在定義游標的時候使用了for update語句才可以使用。  
            上面的語句中r1 是c1中的一行數據,更新的時候應該用current of c1,因為c1只是一個隱式游標,它本身不是通過for update定義的,只是代表了循環中的當前記錄。這樣,你的update語句或者delete語句的作用范圍就只在你循環的當前行的范圍中了。  
               
            要注意的是,用for update定義的游標會讓數據庫對涉及的行加鎖,別的會話如果要訪問該游標中的行便會進入等待狀態。你也可以明確指明要加鎖的列,用for update of "列名"  就可以了。 

            如果select for update選中的行已經被別的會話加鎖的話,會話就需要等待解鎖,如果別的會話一直不解鎖,那么你的select就會一直等待下去,如果你不想等,只需在for update后面加上nowait就可以解決這個問題了,這樣你的選擇會立即返回。

            針對where current of的使用和我們日常的更新方法,進行了一個簡單的測試。
            編寫了兩個簡單的過程,比較使用where current of和不使用時,性能的差異。
            SQL> create table t
              as
              select * from all_objects;
            表已創建。

            SQL> create table t1
              as
              select * from t;
            表已創建。

            SQL> select count(*) from t;
              COUNT(*)
            ----------
                 37003

            SQL> exec runstats.rs_start;
            PL/SQL 過程已成功完成。

            SQL> declare
                cursor sal_cursor is
                  select object_name
                    from t
                     for update nowait;
              begin
                for i in sal_cursor loop
                  update t
                     set data_object_id = data_object_id  + 1
             10       where current of sal_cursor;
             11    end loop;
             12  end;
             13  /
            PL/SQL 過程已成功完成。

            SQL> exec runstats.rs_middle;
            PL/SQL 過程已成功完成。

            SQL> declare
                cursor sal_cursor is
                  select object_name
                    from t1
                     for update nowait;
              begin
                for i in sal_cursor loop
                  update t1
                     set data_object_id = data_object_id  + 1
             10       where object_name = i.object_name;
             11    end loop;
             12    commit;
             13  end;
             14  /
            PL/SQL 過程已成功完成。

            SQL> exec runstats.rs_stop(1000000);
            Run1 ran in 3767 hsecs
            Run2 ran in 126597 hsecs
            run 1 ran in 2.98% of the time
            Name                                  Run1        Run2        Diff
            STAT..no work - consistent rea      37,268   9,212,891   9,175,623
            STAT..table scan blocks gotten      37,247   9,212,999   9,175,752
            STAT..buffer is not pinned cou      37,282   9,213,043   9,175,761
            STAT..consistent gets               37,311   9,400,510   9,363,199
            STAT..session logical reads        119,901   9,521,124   9,401,223
            STAT..redo size                 15,563,548  25,830,156  10,266,608
            LATCH.cache buffers chains         502,564  19,504,830  19,002,266
            STAT..table scan rows gotten        73,998########################
            Run1 latches total versus runs -- difference and pct
            Run1        Run2        Diff       Pct
            782,133  20,678,374  19,896,241      3.78%
            PL/SQL 過程已成功完成。

            對t1表建立索引的差異:
            SQL> analyze index idx_object_name compute statistics;
            索引已分析

            SQL> exec runstats.rs_stop(100000);
            Run1 ran in 2989 hsecs
            Run2 ran in 5250 hsecs
            run 1 ran in 56.93% of the time
            Name                                  Run1        Run2        Diff
            STAT..session logical reads        112,542     231,954     119,412
            STAT..session logical reads        112,542     232,502     119,960
            STAT..session logical reads        112,542     233,080     120,538
            STAT..session pga memory                    131,072     131,072
            STAT..session pga memory                    131,072     131,072
            LATCH.cache buffers chains         488,072     828,849     340,777
            LATCH.cache buffers chains         488,072     832,711     344,639
            LATCH.cache buffers chains         488,072     835,333     347,261
            STAT..redo size                 15,561,972  25,909,144  10,347,172
            STAT..redo size                 15,561,972  25,970,072  10,408,100
            STAT..redo size                 15,561,972  26,030,812  10,468,840
            Run1 latches total versus runs -- difference and pct
            Run1        Run2        Diff       Pct
            2,293,947   3,511,140   1,217,193     65.33%
            PL/SQL 過程已成功完成。

            亚洲一区二区三区日本久久九| 久久久99精品成人片中文字幕| 人妻无码αv中文字幕久久琪琪布 人妻无码精品久久亚瑟影视 | 久久九色综合九色99伊人| 欧美成人免费观看久久| 久久国产精品成人影院| 久久综合视频网站| 97r久久精品国产99国产精| 亚洲精品无码久久久| 久久久久久狠狠丁香| 久久午夜伦鲁片免费无码| 久久精品国产亚洲7777| 91久久婷婷国产综合精品青草| 性做久久久久久久久浪潮| 999久久久免费国产精品播放| 国产成人无码精品久久久性色| 久久国产影院| 国产精品无码久久久久| 精品久久久久久国产| 久久亚洲私人国产精品| 久久久久亚洲AV片无码下载蜜桃| 精品熟女少妇aⅴ免费久久| 久久久精品2019免费观看 | 人妻无码αv中文字幕久久琪琪布| 深夜久久AAAAA级毛片免费看 | 久久精品人人做人人爽电影| 久久久人妻精品无码一区| 久久久91精品国产一区二区三区 | 国产高潮国产高潮久久久91 | 999久久久国产精品| 国产欧美久久一区二区| 99麻豆久久久国产精品免费| 国产精品99久久久久久人| 久久精品一本到99热免费| 久久天堂AV综合合色蜜桃网 | 99久久精品免费看国产| 一本伊大人香蕉久久网手机| 国产综合精品久久亚洲| 99久久99久久精品国产| 亚洲精品第一综合99久久| 久久久精品国产免大香伊|