title: Batch processing versus Single row transactions for atomicity
body: <p>I have two tables; one to hold records of reports generated, and the other to update a flag that the reports have been generated. This script will be scheduled, and the SQLs have been implemented. However, there are two implementations of the script:</p>
  
  <p>Case 1:</p>
  
  <pre><code>- Insert all the records, then
  - Update all the flags,
  - Commit if all is well
  </code></pre>
  
  <p>Case 2:</p>
  
  <pre><code>While (there are records)
  - Insert a record,
  - Update the flag
  - Commit if all is well
  </code></pre>
  
  <p>Which should be preferred and why?</p>
  
  <p>A transaction for Case 1 is for all inserts, then all update. It's all or nothing. I'm to believe this is faster, or not if the connection to the remote database keeps getting interrupted. It requires very little client side processing. But if the inserts fail midway, we'll have to rerun from the top.</p>
  
  <p>A transaction for Case 2 is one insert, update. This requires to keep track of the inserted records, and updating the specific records. I'll have to use placeholders, and while granted the database may cache the SQL, and use the query execution plan repeatedly, I suspect this would be slower than Case 1 because of the additional client side processing. However on an unreliable connection, which we can assume, this looks the better choice.</p>
  
  <p>EDIT 5/11/2015 11:31AM</p>
  
  <p>CASE 1 snippet:</p>
  
  <pre><code>my $sql = "INSERT INTO eval_rep_track_dup\@prod \
              select ert.* \
              from eval_rep_track ert \
              inner join \
              (
                      select erd.evaluation_fk, erd.report_type, LTRIM(erd.assign_group_id, '/site/') course_name \
                      from eval_report_dup\@prod erd \
                      inner join eval_report er \
                      on er.id = erd.id \
                      where erd.status='queue' \
                      and er.status='done' \
              ) cat \
              on ert.eval_id = cat.evaluation_fk \
              and ert.report_type = cat.report_type \
              and ert.course_name = cat.course_name";
      my $sth = $dbh-&gt;prepare($sql) or die "Error with sql statement : $DBI::errstr\n";
      my $noterror = $sth-&gt;execute() or die "Error in sql statement : " . $sth-&gt;errstr . "\n";
  </code></pre>
  
  <p>...</p>
  
  <pre><code># update the status from queue to done      
          $sql = "UPDATE eval_report_dup\@prod \
                  SET status='done' \
                  WHERE id IN \
                  ( \
                          select erd.id \
                          from eval_report_dup\@prod erd \
                          inner join eval_report er \
                          on er.id = erd.id \
                          where erd.status='queue' \
                          and er.status='done' \
                  )";
  
          $sth = $dbh-&gt;prepare($sql);
          $sth-&gt;execute();
  </code></pre>
  
  <p>eval_rep_track_dup has 3 <code>number</code>, 8 <code>varchar2</code> and a <code>timestamp</code> columns
  eval_report_dup has 10 <code>number</code>, 8 <code>varchar2</code> and 3 <code>timestamp</code> columns</p>