From 792a740f0284ead0e561382a9a7e2243287c5220 Mon Sep 17 00:00:00 2001 From: Anna Larch Date: Tue, 10 Oct 2023 13:41:33 +0200 Subject: [PATCH] fix: chunk delete in query Signed-off-by: Anna Larch --- lib/Data.php | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/lib/Data.php b/lib/Data.php index 0312000b1..27638bb3a 100755 --- a/lib/Data.php +++ b/lib/Data.php @@ -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(); } @@ -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) { @@ -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); } }