Skip to content

Commit

Permalink
Reverse prune query optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
erikn69 authored Jun 26, 2024
1 parent 783f824 commit db5ed9c
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/Drivers/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit db5ed9c

Please sign in to comment.