Skip to content

Commit

Permalink
fix: chunk delete in query
Browse files Browse the repository at this point in the history
Signed-off-by: Anna Larch <anna@nextcloud.com>
  • Loading branch information
miaulalala authored and susnux committed Nov 9, 2023
1 parent 8a4d7d2 commit 665e503
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions lib/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -395,10 +395,6 @@ public function deleteActivities($conditions): void {

$deleteQuery->andWhere($deleteQuery->expr()->comparison($column, $operation, $deleteQuery->createNamedParameter($value)));
}




// Dont use chunked delete - let the DB handle the large row count natively
$deleteQuery->executeStatement();
}
Expand Down Expand Up @@ -495,7 +491,7 @@ private function deleteActivitiesForMySQL(array $conditions): void {
$query->where($query->expr()->comparison($column, $operation, $query->createNamedParameter($value)));
}

$query->setMaxResults(10000);
$query->setMaxResults(50000);
$result = $query->executeQuery();
$count = $result->rowCount();
if ($count === 0) {
Expand All @@ -506,12 +502,15 @@ private function deleteActivitiesForMySQL(array $conditions): void {
}, $result->fetchAll(\PDO::FETCH_NUM));
$result->closeCursor();

$queryResult = 0;
$deleteQuery = $this->connection->getQueryBuilder();
$deleteQuery->delete('activity');
$deleteQuery->where($deleteQuery->expr()->in('activity_id', $deleteQuery->createParameter('ids'), IQueryBuilder::PARAM_INT_ARRAY));
$deleteQuery->setParameter('ids', $ids, IQueryBuilder::PARAM_INT_ARRAY);
$queryResult = $deleteQuery->executeStatement();
if ($queryResult === 10000) {
foreach (array_chunk($ids, 1000) as $chunk) {
$deleteQuery->setParameter('ids', $chunk, IQueryBuilder::PARAM_INT_ARRAY);
$queryResult += $deleteQuery->executeStatement();
}
if ($queryResult === 50000) {
$this->deleteActivitiesForMySQL($conditions);
}
}
Expand Down

0 comments on commit 665e503

Please sign in to comment.