Commit outside the cursor

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • amoldiego
    New Member
    • Jun 2007
    • 28

    Commit outside the cursor

    Suppose I create one for loop for update statement . That updates arround 1000 rows .
    So please tell me where i should place commit .
    Inside the loop or outside the loop .
    and how that improve the performance.

    Please reply
  • debasisdas
    Recognized Expert Expert
    • Dec 2006
    • 8119

    #2
    place the commit inside the loop.

    Comment

    • gintsp
      New Member
      • Aug 2007
      • 36

      #3
      Originally posted by amoldiego
      Suppose I create one for loop for update statement . That updates arround 1000 rows .
      So please tell me where i should place commit .
      Inside the loop or outside the loop .
      and how that improve the performance.

      Please reply
      The question however remains - cannot you just issue a simple update statement? It would be the fastest. If you can do something in pure SQL do it - it would be faster, easier to understand and maintain.

      Gints Plivna
      [Edit: Sorry Gints, links not allowed in technical questions]

      Comment

      • amoldiego
        New Member
        • Jun 2007
        • 28

        #4
        Thanks for Ur reply ...
        Could u plz tell me how it will improve the performance if i palce commit inside the loop ..

        Thanks

        Comment

        • amoldiego
          New Member
          • Jun 2007
          • 28

          #5
          I have tried both posibilities ie commit inside the loop and outside the loop ..

          I have one table emptest with 8214 rows...
          i ve created one cursor for update sal of emptest and place commit inside the loop ..

          declare
          cursor c1 is select empno from emptest ;
          begin
          for r1 in c1
          loop
          update emptest set sal =5000 where empno=r1.empno and rownum<=200;
          commit; ----commit inside the loop
          end loop;

          end;

          That code took arround Elapsed: 00:02:57.03 to execute ..
          Then i placed commit outside the loop and fired the code

          The total time for execution is Elapsed: 00:03:17.01

          But how it will improve the performance by placing commit inside the loop ...
          what happens internally in oracle when u place commit inside the loop ..

          Please reply

          Comment

          • gintsp
            New Member
            • Aug 2007
            • 36

            #6
            Originally posted by amoldiego
            I have tried both posibilities ie commit inside the loop and outside the loop ..

            I have one table emptest with 8214 rows...
            i ve created one cursor for update sal of emptest and place commit inside the loop ..

            declare
            cursor c1 is select empno from emptest ;
            begin
            for r1 in c1
            loop
            update emptest set sal =5000 where empno=r1.empno and rownum<=200;
            commit; ----commit inside the loop
            end loop;

            end;

            That code took arround Elapsed: 00:02:57.03 to execute ..
            Then i placed commit outside the loop and fired the code

            The total time for execution is Elapsed: 00:03:17.01

            But how it will improve the performance by placing commit inside the loop ...
            what happens internally in oracle when u place commit inside the loop ..

            Please reply
            WHY you cannot just
            update emptest set sal =5000?????????? ???????

            Have you measured the time for this statement?

            Gints Plivna
            [Edit: Sorry Gints, links not allowed in technical questions]

            Comment

            • amol10
              New Member
              • Sep 2007
              • 2

              #7
              I dont want to use simple update statement my requirement is to write a PL/sql code for that ...so please tell me which will be more effective inside the loop or outside the loop.


              thanks

              Comment

              • debasisdas
                Recognized Expert Expert
                • Dec 2006
                • 8119

                #8
                Originally posted by amol10
                I dont want to use simple update statement my requirement is to write a PL/sql code for that ...so please tell me which will be more effective inside the loop or outside the loop.

                thanks
                Commit must be inside the loop.

                Comment

                • ellenraju
                  New Member
                  • Sep 2007
                  • 5

                  #9
                  Originally posted by amoldiego
                  Suppose I create one for loop for update statement . That updates arround 1000 rows .
                  So please tell me where i should place commit .
                  Inside the loop or outside the loop .
                  and how that improve the performance.

                  Please reply
                  If you simply asks where to put the commit, it's better put out side the cursor, becasue frequent commit gives performace problem and lot of I/O will happen.
                  You mensioned number of updates but you did not mension on what type of objects whether updation on smaller table or very large.......,
                  1. Iif updates are very small data or normal table use above or
                  2. You can use inside the cursor to commit for every specific nuber of loops...for example handle parameter that every 10 loops one commit.

                  Comment

                  • amitpatel66
                    Recognized Expert Top Contributor
                    • Mar 2007
                    • 2358

                    #10
                    Originally posted by ellenraju
                    If you simply asks where to put the commit, it's better put out side the cursor, becasue frequent commit gives performace problem and lot of I/O will happen.
                    You mensioned number of updates but you did not mension on what type of objects whether updation on smaller table or very large.......,
                    1. Iif updates are very small data or normal table use above or
                    2. You can use inside the cursor to commit for every specific nuber of loops...for example handle parameter that every 10 loops one commit.
                    Make use of FORALL to improve performance as shown below

                    [CODE=oracle]
                    forall r1 in c1
                    update emptest set sal =5000 where empno=r1.empno and rownum<=200;
                    commit;
                    [/CODE]

                    Comment

                    Working...