• <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 閱讀(408) 評論(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 過程已成功完成。

            国产精品久久久亚洲| 精品久久久无码中文字幕| 中文精品久久久久人妻不卡| 精品综合久久久久久97| 国产成人精品免费久久久久| 国产亚洲欧美成人久久片| 精品久久人人妻人人做精品| 久久亚洲日韩看片无码| 国产91色综合久久免费| 午夜精品久久影院蜜桃| 国产亚洲精品自在久久| 久久免费99精品国产自在现线| 久久人与动人物a级毛片| 国产精品99久久久久久www| av色综合久久天堂av色综合在| 国产精品18久久久久久vr| 欧美日韩精品久久免费| 精品久久久久成人码免费动漫 | 久久精品国产99久久香蕉| 久久亚洲精品国产亚洲老地址 | 欧美精品久久久久久久自慰| 国产成人精品久久| 国产亚洲美女精品久久久久狼| 久久综合亚洲色HEZYO社区| 久久久国产一区二区三区| 99久久超碰中文字幕伊人| 欧美国产成人久久精品| 久久亚洲中文字幕精品一区四 | 国产精品无码久久久久久| 婷婷五月深深久久精品| 久久人人爽人人爽人人爽| 久久强奷乱码老熟女网站| 国产精品欧美亚洲韩国日本久久| 香蕉久久av一区二区三区| 久久久久久精品免费免费自慰| 婷婷国产天堂久久综合五月| 久久亚洲国产精品123区| 久久久久18| 久久综合久久综合亚洲| 合区精品久久久中文字幕一区| 无码任你躁久久久久久久|