From 93e0f43a37d023a4b13769c7daabb593663da6ff Mon Sep 17 00:00:00 2001 From: Marcel Date: Thu, 25 Jun 2026 16:58:48 +1200 Subject: [PATCH] Replace offset-based batching for log_visit with index-based pagination --- Db/BatchQuery.php | 5 +++++ Migrations/LogMigration.php | 14 +++++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/Db/BatchQuery.php b/Db/BatchQuery.php index 99edf08..f77627d 100644 --- a/Db/BatchQuery.php +++ b/Db/BatchQuery.php @@ -46,4 +46,9 @@ private function makeQuery($sql, $offset) { return $sql . ' LIMIT ' . (int)$this->limit . ' OFFSET ' . (int) $offset; } + + public function getLimit(): int + { + return $this->limit; + } } diff --git a/Migrations/LogMigration.php b/Migrations/LogMigration.php index 9a7b032..fbab033 100644 --- a/Migrations/LogMigration.php +++ b/Migrations/LogMigration.php @@ -37,14 +37,22 @@ public function migrate(Request $request, TargetDb $targetDb) $logActionMigration = new LogActionMigration(); - $batchQuery = new BatchQuery(); $count = 0; - $loggedAt = array(0.05, 0.1, 0.2, 0.4, 0.6, 0.8, 0.9); - foreach ($batchQuery->generateQuery('SELECT * FROM ' . Common::prefixTable('log_visit') . ' WHERE idsite = ? ORDER BY idvisit ASC', array($request->sourceIdSite)) as $visitRows) { + $table = Common::prefixTable('log_visit'); + + $batchQuery = new BatchQuery(); + $limit = $batchQuery->getLimit(); + + $sql = 'SELECT * FROM %s WHERE idsite = ? AND idvisit > ? ORDER BY idvisit ASC LIMIT %d'; + $sql = sprintf($sql, $table, $limit); + + $lastIdVisit = 0; + while ($visitRows = Db::fetchAll($sql, [$request->sourceIdSite, $lastIdVisit])) { $count += count($visitRows); $this->migrateVisits($visitRows, $request, $logActionMigration, $targetDb); + $lastIdVisit = end($visitRows)['idvisit']; foreach ($loggedAt as $logAt) { if ($numVisits && ($count / $numVisits) > $logAt) {