Customer Portal

Get Involved. Join the Conversation.

Topic

    JJ
    Commit at an interval of records processed such as every 100...Answered
    Topic posted August 19, 2018 by JJ Bronze Crown: 15,000+ Points 
    73 Views, 2 Comments
    Title:
    Commit at an interval of records processed such as every 100 or 1000 records
    Content:

    Hi,

    I have a loop that goes through *all* our incidents, some 250k. Code is below for what happens.

    I raised a SR saying it wasn't performing as expecting as an arbitrary number were getting saved. They suggested "committing at an interval of records processed such as every 100 or 1000 records"

    My question would be how I'd go about it? In fact, how would I optimise the code below? I was thinking of rather than deleting, I could update the record...

    Code Snippet:

    Best Comment

    Vlad

    Hey JJ,

    You can declare a counter variable (e.g. $counter) at the start and set it to 0. Then, for each incident processed, add 1 to it ($counter++). Finally, when you call commit(), check if the $counter value is a multiple of 100 or 1000. Something like this.

    $counter=0;
    foreach ($a as $v) {
    $counter++;
     
    //processing goes here
    //incident is saved
     
    if ($counter % 1000 == 0) {
    RNCPHP\ConnectAPI::commit(); 
    }
    }

    Comment

     

    • Vlad

      Hey JJ,

      You can declare a counter variable (e.g. $counter) at the start and set it to 0. Then, for each incident processed, add 1 to it ($counter++). Finally, when you call commit(), check if the $counter value is a multiple of 100 or 1000. Something like this.

      $counter=0;
      foreach ($a as $v) {
      $counter++;
       
      //processing goes here
      //incident is saved
       
      if ($counter % 1000 == 0) {
      RNCPHP\ConnectAPI::commit(); 
      }
      }
    • JJ

      Ha - I should really not comment before I work it out :-)

      Did this as soon as I posted my comment. Did an additional commit because it might be an odd number so did some extra checks for that