Skip to content

Commit

Permalink
⚡ Improve performance of polyline cleanup (#2507)
Browse files Browse the repository at this point in the history
  • Loading branch information
HerrLevin authored Apr 20, 2024
1 parent 9f13828 commit b11deeb
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions app/Console/Commands/CleanUpPolylines.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit b11deeb

Please sign in to comment.