From ec0dc1315a6050b23d0d215b23ae101ecb754a73 Mon Sep 17 00:00:00 2001 From: Steve Bauman Date: Fri, 22 Nov 2024 14:44:17 -0500 Subject: [PATCH] Improve first and find query performance by increasing chunk size --- src/Query.php | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/Query.php b/src/Query.php index 9983b18..3c85a25 100644 --- a/src/Query.php +++ b/src/Query.php @@ -29,14 +29,14 @@ public function __construct( /** * Find a model by its key. */ - public function find(?string $id): ?Model + public function find(?string $id, int $chunk = 1000): ?Model { if (is_null($id)) { return null; } if (! empty($this->model->getSearchable())) { - return $this->whereKey($id)->first(); + return $this->whereKey($id)->first($chunk); } if (! $this->cache->exists($hash = $this->model->getBaseHashWithKey($id))) { @@ -49,9 +49,9 @@ public function find(?string $id): ?Model /** * Find a model by its key or call a callback. */ - public function findOr(?string $id, Closure $callback): mixed + public function findOr(?string $id, Closure $callback, int $chunk = 1000): mixed { - if (! is_null($model = $this->find($id))) { + if (! is_null($model = $this->find($id, $chunk))) { return $model; } @@ -61,9 +61,9 @@ public function findOr(?string $id, Closure $callback): mixed /** * Find a model by its key or throw an exception. */ - public function findOrFail(?string $id): Model + public function findOrFail(?string $id, int $chunk = 1000): Model { - if (! $model = $this->find($id)) { + if (! $model = $this->find($id, $chunk)) { throw (new ModelNotFoundException)->setModel(get_class($this->model), $id); } @@ -85,9 +85,9 @@ public function create(array $attributes = [], bool $force = false): Model /** * Create a new model or return the existing one. */ - public function firstOrCreate(array $attributes = [], array $values = []): Model + public function firstOrCreate(array $attributes = [], array $values = [], int $chunk = 1000): Model { - if (! is_null($instance = (clone $this)->where($attributes)->first())) { + if (! is_null($instance = (clone $this)->where($attributes)->first($chunk))) { return $instance; } @@ -97,9 +97,9 @@ public function firstOrCreate(array $attributes = [], array $values = []): Model /** * Update a model or create a new one. */ - public function updateOrCreate(array $attributes, array $values = [], bool $force = true): Model + public function updateOrCreate(array $attributes, array $values = [], bool $force = true, int $chunk = 1000): Model { - if (! is_null($instance = (clone $this)->where($attributes)->first())) { + if (! is_null($instance = (clone $this)->where($attributes)->first($chunk))) { $instance->fill($values)->save($force); return $instance; @@ -168,7 +168,7 @@ public function whereKey(?string $value): self /** * Execute the query and get the first result. */ - public function first(): ?Model + public function first(int $chunk = 1000): ?Model { $instance = null; @@ -176,7 +176,7 @@ public function first(): ?Model $instance = $model; return false; - }, 1); + }, $chunk); return $instance; } @@ -184,9 +184,9 @@ public function first(): ?Model /** * Execute the query and get the first result or throw an exception. */ - public function firstOrFail(): Model + public function firstOrFail(int $chunk = 1000): Model { - if (! $model = $this->first()) { + if (! $model = $this->first($chunk)) { throw (new ModelNotFoundException)->setModel(get_class($this->model)); } @@ -208,17 +208,17 @@ public function get(int $chunk = 1000): Collection /** * Determine if any models exist for the current query. */ - public function exists(): bool + public function exists(int $chunk = 1000): bool { - return $this->first() !== null; + return $this->first($chunk) !== null; } /** * Execute a callback over each item. */ - public function each(Closure $callback, int $count = 1000): void + public function each(Closure $callback, int $chunk = 1000): void { - $this->chunk($count, function (Collection $models) use ($callback) { + $this->chunk($chunk, function (Collection $models) use ($callback) { foreach ($models as $key => $model) { if ($callback($model, $key) === false) { return false;