attempt to improve results loading with eloquent

This commit is contained in:
Daniël Klabbers 2021-11-08 16:04:42 +01:00
parent 99dff8289d
commit 3106667519
1 changed files with 32 additions and 24 deletions

View File

@ -88,35 +88,43 @@ class BuildCommand extends Command
foreach ($seeders as $seeder) {
if ($only && $seeder->type() !== $only) continue;
$seeder->query()
->when($this->option('max-id'), function ($query, $id) {
$query->where('id', '<=', $id);
})
->when($this->option('continue'), function ($query) use ($seeder) {
if ($continueAt = $this->continueAt($seeder->type())) {
$query->where('id', '<=', $continueAt);
}
})
->latest('id')
->chunk($this->option('chunk-size') ?? 1000, function (Collection $collection) use ($queue, &$total, $seeder) {
$queue->pushOn(Job::$onQueue, new SavingJob($collection, $seeder));
$total = 0;
$this->info("Pushed into the index, type: {$seeder->type()}, amount: {$collection->count()}.");
$continueAt = $this->option('continue')
? ($this->continueAt($seeder->type()) ?? $seeder->query()->max('id'))
: $seeder->query()->max('id');
$total += $collection->count();
while($continueAt !== null) {
/** @var Collection $collection */
$collection = $seeder->query()
->latest('id')
->whereBetween('id', [$continueAt - 1000, $continueAt])
->when($this->option('max-id'), function ($query, $id) {
$query->where('id', '<=', $id);
})
->get();
$this->continueAt(
$seeder->type(),
$collection->min('id')
);
});
$min = $collection->min('id');
$continueAt = $min && $min > 2 ? $min - 1 : null;
$queue->pushOn(Job::$onQueue, new SavingJob($collection, $seeder));
$this->info("Pushed into the index, type: {$seeder->type()}, amount: {$collection->count()}.");
$total += $collection->count();
$this->continueAt(
$seeder->type(),
$continueAt
);
if ($throttle = $this->option('throttle')) {
$this->info("Throttling for $throttle seconds");
sleep($throttle);
}
}
$this->info("Pushed a total of $total into the index.");
if ($throttle = $this->option('throttle')) {
$this->info("Throttling for $throttle seconds");
sleep($throttle);
}
}
}