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