From b11deebbbc91522cb4e678c449f9c97bb55bd10a Mon Sep 17 00:00:00 2001 From: Levin Herr Date: Sat, 20 Apr 2024 19:13:53 +0200 Subject: [PATCH] :zap: Improve performance of polyline cleanup (#2507) --- app/Console/Commands/CleanUpPolylines.php | 34 +++++++++++++++++++---- 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/app/Console/Commands/CleanUpPolylines.php b/app/Console/Commands/CleanUpPolylines.php index 8a077b1d8..a3099ae0c 100644 --- a/app/Console/Commands/CleanUpPolylines.php +++ b/app/Console/Commands/CleanUpPolylines.php @@ -2,21 +2,43 @@ namespace App\Console\Commands; -use App\Models\Trip; use App\Models\PolyLine; use Illuminate\Console\Command; +use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Log; class CleanUpPolylines extends Command { protected $signature = 'trwl:cleanUpPolylines'; - protected $description = 'Delete unused and old polylines from database'; + protected $description = 'Find and delete unused and old polylines from database'; public function handle(): int { - $usedPolylineIds = Trip::where('polyline_id', '<>', null)->groupBy('polyline_id')->select('polyline_id'); - $usedParentIds = Polyline::where('parent_id', '<>', null)->groupBy('parent_id')->select('parent_id'); - $affectedRows = Polyline::whereNotIn('id', $usedPolylineIds)->whereNotIn('id', $usedParentIds)->delete(); - Log::debug($affectedRows . ' unused polylines deleted.'); + $start = microtime(true); + $rows = DB::table('poly_lines') + ->selectRaw('poly_lines.id, poly_lines.parent_id') + ->leftJoin('hafas_trips', 'poly_lines.id', '=', 'hafas_trips.polyline_id') + ->leftJoin( + 'poly_lines AS parent_poly_lines', + 'poly_lines.id', + '=', + 'parent_poly_lines.parent_id' + ) + ->whereRaw('hafas_trips.polyline_id IS NULL AND parent_poly_lines.parent_id IS NULL') + ->get(); + $this->info('Found ' . $rows->count() . ' unused polylines.'); + $affectedRows = 0; + + // get 100 rows at a time + foreach ($rows->chunk(100) as $chunk) { + $ids = $chunk->pluck('id')->toArray(); + $affectedRows += PolyLine::whereIn('id', $ids)->delete(); + $this->output->write('.'); + } + $this->output->newLine(); + + $time_elapsed_secs = microtime(true) - $start; + Log::debug($affectedRows . ' unused polylines deleted in ' . $time_elapsed_secs . ' seconds.'); + $this->info($affectedRows . ' unused polylines deleted in ' . $time_elapsed_secs . ' seconds.'); return 0; } }