Skip to content

Commit ec0dc13

Browse files
committed
Improve first and find query performance by increasing chunk size
1 parent 72275b0 commit ec0dc13

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

src/Query.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ public function __construct(
2929
/**
3030
* Find a model by its key.
3131
*/
32-
public function find(?string $id): ?Model
32+
public function find(?string $id, int $chunk = 1000): ?Model
3333
{
3434
if (is_null($id)) {
3535
return null;
3636
}
3737

3838
if (! empty($this->model->getSearchable())) {
39-
return $this->whereKey($id)->first();
39+
return $this->whereKey($id)->first($chunk);
4040
}
4141

4242
if (! $this->cache->exists($hash = $this->model->getBaseHashWithKey($id))) {
@@ -49,9 +49,9 @@ public function find(?string $id): ?Model
4949
/**
5050
* Find a model by its key or call a callback.
5151
*/
52-
public function findOr(?string $id, Closure $callback): mixed
52+
public function findOr(?string $id, Closure $callback, int $chunk = 1000): mixed
5353
{
54-
if (! is_null($model = $this->find($id))) {
54+
if (! is_null($model = $this->find($id, $chunk))) {
5555
return $model;
5656
}
5757

@@ -61,9 +61,9 @@ public function findOr(?string $id, Closure $callback): mixed
6161
/**
6262
* Find a model by its key or throw an exception.
6363
*/
64-
public function findOrFail(?string $id): Model
64+
public function findOrFail(?string $id, int $chunk = 1000): Model
6565
{
66-
if (! $model = $this->find($id)) {
66+
if (! $model = $this->find($id, $chunk)) {
6767
throw (new ModelNotFoundException)->setModel(get_class($this->model), $id);
6868
}
6969

@@ -85,9 +85,9 @@ public function create(array $attributes = [], bool $force = false): Model
8585
/**
8686
* Create a new model or return the existing one.
8787
*/
88-
public function firstOrCreate(array $attributes = [], array $values = []): Model
88+
public function firstOrCreate(array $attributes = [], array $values = [], int $chunk = 1000): Model
8989
{
90-
if (! is_null($instance = (clone $this)->where($attributes)->first())) {
90+
if (! is_null($instance = (clone $this)->where($attributes)->first($chunk))) {
9191
return $instance;
9292
}
9393

@@ -97,9 +97,9 @@ public function firstOrCreate(array $attributes = [], array $values = []): Model
9797
/**
9898
* Update a model or create a new one.
9999
*/
100-
public function updateOrCreate(array $attributes, array $values = [], bool $force = true): Model
100+
public function updateOrCreate(array $attributes, array $values = [], bool $force = true, int $chunk = 1000): Model
101101
{
102-
if (! is_null($instance = (clone $this)->where($attributes)->first())) {
102+
if (! is_null($instance = (clone $this)->where($attributes)->first($chunk))) {
103103
$instance->fill($values)->save($force);
104104

105105
return $instance;
@@ -168,25 +168,25 @@ public function whereKey(?string $value): self
168168
/**
169169
* Execute the query and get the first result.
170170
*/
171-
public function first(): ?Model
171+
public function first(int $chunk = 1000): ?Model
172172
{
173173
$instance = null;
174174

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

178178
return false;
179-
}, 1);
179+
}, $chunk);
180180

181181
return $instance;
182182
}
183183

184184
/**
185185
* Execute the query and get the first result or throw an exception.
186186
*/
187-
public function firstOrFail(): Model
187+
public function firstOrFail(int $chunk = 1000): Model
188188
{
189-
if (! $model = $this->first()) {
189+
if (! $model = $this->first($chunk)) {
190190
throw (new ModelNotFoundException)->setModel(get_class($this->model));
191191
}
192192

@@ -208,17 +208,17 @@ public function get(int $chunk = 1000): Collection
208208
/**
209209
* Determine if any models exist for the current query.
210210
*/
211-
public function exists(): bool
211+
public function exists(int $chunk = 1000): bool
212212
{
213-
return $this->first() !== null;
213+
return $this->first($chunk) !== null;
214214
}
215215

216216
/**
217217
* Execute a callback over each item.
218218
*/
219-
public function each(Closure $callback, int $count = 1000): void
219+
public function each(Closure $callback, int $chunk = 1000): void
220220
{
221-
$this->chunk($count, function (Collection $models) use ($callback) {
221+
$this->chunk($chunk, function (Collection $models) use ($callback) {
222222
foreach ($models as $key => $model) {
223223
if ($callback($model, $key) === false) {
224224
return false;

0 commit comments

Comments
 (0)