Skip to content

Commit

Permalink
Improve first and find query performance by increasing chunk size
Browse files Browse the repository at this point in the history
  • Loading branch information
stevebauman committed Nov 22, 2024
1 parent 72275b0 commit ec0dc13
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -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))) {
Expand All @@ -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;
}

Expand All @@ -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);
}

Expand All @@ -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;
}

Expand All @@ -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;
Expand Down Expand Up @@ -168,25 +168,25 @@ 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;

$this->each(function (Model $model) use (&$instance) {
$instance = $model;

return false;
}, 1);
}, $chunk);

return $instance;
}

/**
* 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));
}

Expand All @@ -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;
Expand Down

0 comments on commit ec0dc13

Please sign in to comment.