From db5ed9cdd3370cb6325865abf524dc67de32ed7b Mon Sep 17 00:00:00 2001 From: erikn69 Date: Wed, 26 Jun 2024 09:03:15 -0500 Subject: [PATCH] Reverse prune query optimization --- src/Drivers/Database.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Drivers/Database.php b/src/Drivers/Database.php index d0cc1d9d..e0fc4c68 100644 --- a/src/Drivers/Database.php +++ b/src/Drivers/Database.php @@ -25,11 +25,15 @@ public function prune(Auditable $model): bool if (($threshold = $model->getAuditThreshold()) > 0) { $class = get_class($model->audits()->getModel()); $keyName = (new $class)->getKeyName(); - + + $idsToKeep = $model->audits() + ->select($keyName) + ->limit($threshold) + ->latest() + ->pluck($keyName); + return $model->audits() - ->whereNotIn( - $keyName, $model->audits()->select($keyName)->limit($threshold)->latest() - ) + ->whereNotIn($keyName, $idsToKeep) ->delete() > 0; }