Skip to content

Commit 3ad6c4c

Browse files
committed
refactor: reduce memory useage when updateBatch() with $set is not null
1 parent 5165550 commit 3ad6c4c

1 file changed

Lines changed: 23 additions & 10 deletions

File tree

system/Database/BaseBuilder.php

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1975,28 +1975,37 @@ public function updateBatch(?array $set = null, ?string $index = null, int $batc
19751975

19761976
return false; // @codeCoverageIgnore
19771977
}
1978-
} else {
1979-
if (empty($set)) {
1980-
if (CI_DEBUG) {
1981-
throw new DatabaseException('updateBatch() called with no data');
1982-
}
1983-
1984-
return false; // @codeCoverageIgnore
1978+
} elseif (empty($set)) {
1979+
if (CI_DEBUG) {
1980+
throw new DatabaseException('updateBatch() called with no data');
19851981
}
19861982

1987-
$this->setUpdateBatch($set, $index);
1983+
return false; // @codeCoverageIgnore
19881984
}
19891985

1986+
$hasQBSet = ($set === null);
1987+
19901988
$table = $this->QBFrom[0];
19911989

19921990
$affectedRows = 0;
19931991
$savedSQL = [];
19941992
$savedQBWhere = $this->QBWhere;
19951993

1996-
for ($i = 0, $total = count($this->QBSet); $i < $total; $i += $batchSize) {
1994+
if ($hasQBSet) {
1995+
$set = $this->QBSet;
1996+
}
1997+
1998+
for ($i = 0, $total = count($set); $i < $total; $i += $batchSize) {
1999+
if ($hasQBSet) {
2000+
$QBSet = array_slice($this->QBSet, $i, $batchSize);
2001+
} else {
2002+
$this->setUpdateBatch(array_slice($set, $i, $batchSize), $index);
2003+
$QBSet = $this->QBSet;
2004+
}
2005+
19972006
$sql = $this->_updateBatch(
19982007
$table,
1999-
array_slice($this->QBSet, $i, $batchSize),
2008+
$QBSet,
20002009
$this->db->protectIdentifiers($index)
20012010
);
20022011

@@ -2007,6 +2016,10 @@ public function updateBatch(?array $set = null, ?string $index = null, int $batc
20072016
$affectedRows += $this->db->affectedRows();
20082017
}
20092018

2019+
if (! $hasQBSet) {
2020+
$this->resetWrite();
2021+
}
2022+
20102023
$this->QBWhere = $savedQBWhere;
20112024
}
20122025

0 commit comments

Comments
 (0)