Setting query offset I need to batch process a set of data. To do this I am using the code below. I am trying to process 100 at a time, but the offset doesn't seem to work. It works the first time, but in the second iteration of the loop it doesn't set the offset properly.

  $criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
  $criteria-&gt;section = 'affiliates';
  $criteria-&gt;limit = null;
  $row_count = 6000;
  $batch_size = 100;
  $num_batches = ceil($row_count / $batch_size);
  $offset = 100;

  for ($j=0; $j &lt;= $num_batches; $j++) {
      $query-&gt;setOffset($offset);
      $query-&gt;limit = $batch_size;
      $offset = $offset + $batch_size;        
      $queryResults = $query-&gt;queryAll();
      $affiliates = EntryModel::populateModels($queryResults);    

      foreach ($affiliates as $affiliate) {
        //process
      }
   }
Not totally sure I understand everything going on in your example code, but I believe it could be a simplified a little:

$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
$criteria-&gt;section = 'affiliates';
$criteria-&gt;limit = null;

$rowCount = $criteria-&gt;total();
$batchSize = 100;
$numBatches = ceil($rowCount / $batchSize);

$criteria-&gt;limit = $batchSize;

for ($i = 0; $i &lt; $numBatches; ++$i) {
    $criteria-&gt;offset = $batchSize * $i;
    $affiliates = $criteria-&gt;find();
    foreach ($affiliates as $affiliate) {
        //process
    }
}


Tested, and works.