Skip to content

Commit acd566d

Browse files
committed
refactor: reduce memory useage when insertBatch() with $set is not null
1 parent c18c70e commit acd566d

1 file changed

Lines changed: 18 additions & 4 deletions

File tree

system/Database/BaseBuilder.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1610,24 +1610,38 @@ public function insertBatch(?array $set = null, ?bool $escape = null, int $batch
16101610

16111611
return false; // @codeCoverageIgnore
16121612
}
1613-
1614-
$this->setInsertBatch($set, '', $escape);
16151613
}
16161614

1615+
$hasQBSet = ($set === null);
1616+
16171617
$table = $this->QBFrom[0];
16181618

16191619
$affectedRows = 0;
16201620
$savedSQL = [];
16211621

1622-
for ($i = 0, $total = count($this->QBSet); $i < $total; $i += $batchSize) {
1623-
$sql = $this->_insertBatch($this->db->protectIdentifiers($table, true, null, false), $this->QBKeys, array_slice($this->QBSet, $i, $batchSize));
1622+
if ($hasQBSet) {
1623+
$set = $this->QBSet;
1624+
}
1625+
1626+
for ($i = 0, $total = count($set); $i < $total; $i += $batchSize) {
1627+
if ($hasQBSet) {
1628+
$QBSet = array_slice($this->QBSet, $i, $batchSize);
1629+
} else {
1630+
$this->setInsertBatch(array_slice($set, $i, $batchSize), '', $escape);
1631+
$QBSet = $this->QBSet;
1632+
}
1633+
$sql = $this->_insertBatch($this->db->protectIdentifiers($table, true, null, false), $this->QBKeys, $QBSet);
16241634

16251635
if ($this->testMode) {
16261636
$savedSQL[] = $sql;
16271637
} else {
16281638
$this->db->query($sql, null, false);
16291639
$affectedRows += $this->db->affectedRows();
16301640
}
1641+
1642+
if (! $hasQBSet) {
1643+
$this->resetWrite();
1644+
}
16311645
}
16321646

16331647
if (! $this->testMode) {

0 commit comments

Comments
 (0)