Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.2] Speed up chunk for large sets of data #12861

Merged
merged 2 commits into from
Mar 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,37 @@ public function chunk($count, callable $callback)
return true;
}

/**
* Chunk the results of a query, when the ID to query by is known.
*
* Because if we know the ID to key by, then we can get through each page much faster,
* especially in larger sets of data.
*
* @param $count
* @param callable $callback
* @param $idField
* @return bool
*/
public function chunkById($count, callable $callback, $idField)
{
$lastId = null;
$results = $this->pageAfterId($count, $idField)->get();

while (! $results->isEmpty()) {
if (call_user_func($callback, $results) === false) {
return false;
}

if ($idField) {
$lastId = last($results->all())->{$idField};
}

$results = $this->pageAfterId($count, $idField, $lastId)->get();
}

return true;
}

/**
* Execute a callback over each item while chunking.
*
Expand Down
48 changes: 48 additions & 0 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1314,6 +1314,23 @@ public function forPage($page, $perPage = 15)
return $this->skip(($page - 1) * $perPage)->take($perPage);
}

/**
* Set the limit and query for the next set of results, given the
* last scene ID in a set.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scene or seen?

*
* @param int $perPage
* @param $idField
* @param int $lastId
* @return Builder|static
*/
public function pageAfterId($perPage = 15, $idField = 'id', $lastId = 0)
{
return $this->select($idField)
->where($idField, '>', $lastId)
->orderBy($idField, 'asc')
->take($perPage);
}

/**
* Add a union statement to the query.
*
Expand Down Expand Up @@ -1611,6 +1628,37 @@ public function chunk($count, callable $callback)
return true;
}

/**
* Chunk the results of a query, when the ID to query by is known.
*
* Because if we know the ID to key by, then we can get through each page much faster,
* especially in larger sets of data.
*
* @param $count
* @param callable $callback
* @param $idField
* @return bool
*/
public function chunkById($count, callable $callback, $idField)
{
$lastId = null;
$results = $this->pageAfterId($count, $idField)->get();

while (! $results->isEmpty()) {
if (call_user_func($callback, $results) === false) {
return false;
}

if ($idField) {
$lastId = last($results->all())->{$idField};
}

$results = $this->pageAfterId($count, $idField, $lastId)->get();
}

return true;
}

/**
* Execute a callback over each item while chunking.
*
Expand Down
16 changes: 16 additions & 0 deletions tests/Database/DatabaseEloquentBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,22 @@ public function testChunkCanBeStoppedByReturningFalse()
});
}

public function testChunkPaginatesUsingWhereBetweenIds()
{
$builder = m::mock('Illuminate\Database\Eloquent\Builder[pageAfterId,get]', [$this->getMockQueryBuilder()]);
$builder->shouldReceive('pageAfterId')->once()->with(2, 'someIdField')->andReturn($builder);
$builder->shouldReceive('pageAfterId')->once()->with(2, 'someIdField', 2)->andReturn($builder);
$builder->shouldReceive('pageAfterId')->once()->with(2, 'someIdField', 10)->andReturn($builder);

$builder->shouldReceive('get')->times(3)->andReturn(
new Collection([(object) ['someIdField' => 1], (object) ['someIdField' => 2]]),
new Collection([(object) ['someIdField' => 10]]),
new Collection([])
);

$builder->chunkById(2, function ($results) {}, 'someIdField');
}

public function testPluckReturnsTheMutatedAttributesOfAModel()
{
$builder = $this->getBuilder();
Expand Down