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.7] Improve eager loading performance #26453

Merged
merged 1 commit into from
Nov 9, 2018
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
4 changes: 3 additions & 1 deletion src/Illuminate/Database/Eloquent/Relations/BelongsTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ public function addEagerConstraints(array $models)
// our eagerly loading query so it returns the proper models from execution.
$key = $this->related->getTable().'.'.$this->ownerKey;

$this->query->whereIn($key, $this->getEagerModelKeys($models));
$whereIn = $this->whereInMethod($this->related, $this->ownerKey);

$this->query->{$whereIn}($key, $this->getEagerModelKeys($models));
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,9 @@ protected function addWhereConstraints()
*/
public function addEagerConstraints(array $models)
{
$this->query->whereIn($this->getQualifiedForeignPivotKeyName(), $this->getKeys($models, $this->parentKey));
$whereIn = $this->whereInMethod($this->parent, $this->parentKey);

$this->query->{$whereIn}($this->getQualifiedForeignPivotKeyName(), $this->getKeys($models, $this->parentKey));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ public function throughParentSoftDeletes()
*/
public function addEagerConstraints(array $models)
{
$this->query->whereIn(
$whereIn = $this->whereInMethod($this->farParent, $this->localKey);

$this->query->{$whereIn}(
$this->getQualifiedFirstKeyName(), $this->getKeys($models, $this->localKey)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function addConstraints()
*/
public function addEagerConstraints(array $models)
{
$whereIn = in_array($this->parent->getKeyType(), ['int', 'integer']) ? 'whereInRaw' : 'whereIn';
$whereIn = $this->whereInMethod($this->parent, $this->localKey);

$this->query->{$whereIn}(
$this->foreignKey, $this->getKeys($models, $this->localKey)
Expand Down
15 changes: 15 additions & 0 deletions src/Illuminate/Database/Eloquent/Relations/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,21 @@ public function relatedUpdatedAt()
return $this->related->getUpdatedAtColumn();
}

/**
* Get the name of the "where in" method for eager loading.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param string $key
* @return string
*/
protected function whereInMethod(Model $model, $key)
{
return $model->getKeyName() === last(explode('.', $key))
&& in_array($model->getKeyType(), ['int', 'integer'])
? 'whereInRaw'
: 'whereIn';
}

/**
* Set or get the morph map for polymorphic relations.
*
Expand Down
16 changes: 12 additions & 4 deletions tests/Database/DatabaseEloquentBelongsToTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,19 @@ public function testUpdateMethodRetrievesModelAndUpdates()
public function testEagerConstraintsAreProperlyAdded()
{
$relation = $this->getRelation();
$relation->getQuery()->shouldReceive('whereIn')->once()->with('relation.id', ['foreign.value', 'foreign.value.two']);
$relation->getRelated()->shouldReceive('getKeyName')->andReturn('id');
$relation->getRelated()->shouldReceive('getKeyType')->andReturn('int');
$relation->getQuery()->shouldReceive('whereInRaw')->once()->with('relation.id', ['foreign.value', 'foreign.value.two']);
$models = [new EloquentBelongsToModelStub, new EloquentBelongsToModelStub, new AnotherEloquentBelongsToModelStub];
$relation->addEagerConstraints($models);
}

public function testIdsInEagerConstraintsCanBeZero()
{
$relation = $this->getRelation();
$relation->getQuery()->shouldReceive('whereIn')->once()->with('relation.id', ['foreign.value', 0]);
$relation->getRelated()->shouldReceive('getKeyName')->andReturn('id');
$relation->getRelated()->shouldReceive('getKeyType')->andReturn('int');
$relation->getQuery()->shouldReceive('whereInRaw')->once()->with('relation.id', ['foreign.value', 0]);
$models = [new EloquentBelongsToModelStub, new EloquentBelongsToModelStubWithZeroId];
$relation->addEagerConstraints($models);
}
Expand Down Expand Up @@ -154,7 +158,9 @@ public function testAssociateMethodSetsForeignKeyOnModelById()
public function testDefaultEagerConstraintsWhenIncrementing()
{
$relation = $this->getRelation();
$relation->getQuery()->shouldReceive('whereIn')->once()->with('relation.id', m::mustBe([null]));
$relation->getRelated()->shouldReceive('getKeyName')->andReturn('id');
$relation->getRelated()->shouldReceive('getKeyType')->andReturn('int');
$relation->getQuery()->shouldReceive('whereInRaw')->once()->with('relation.id', m::mustBe([null]));
$models = [new MissingEloquentBelongsToModelStub, new MissingEloquentBelongsToModelStub];
$relation->addEagerConstraints($models);
}
Expand All @@ -170,7 +176,9 @@ public function testDefaultEagerConstraintsWhenIncrementingAndNonIntKeyType()
public function testDefaultEagerConstraintsWhenNotIncrementing()
{
$relation = $this->getRelation(null, false);
$relation->getQuery()->shouldReceive('whereIn')->once()->with('relation.id', m::mustBe([null]));
$relation->getRelated()->shouldReceive('getKeyName')->andReturn('id');
$relation->getRelated()->shouldReceive('getKeyType')->andReturn('int');
$relation->getQuery()->shouldReceive('whereInRaw')->once()->with('relation.id', m::mustBe([null]));
$models = [new MissingEloquentBelongsToModelStub, new MissingEloquentBelongsToModelStub];
$relation->addEagerConstraints($models);
}
Expand Down
2 changes: 2 additions & 0 deletions tests/Database/DatabaseEloquentHasManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ public function testRelationIsProperlyInitialized()
public function testEagerConstraintsAreProperlyAdded()
{
$relation = $this->getRelation();
$relation->getParent()->shouldReceive('getKeyName')->once()->andReturn('id');
$relation->getParent()->shouldReceive('getKeyType')->once()->andReturn('int');
$relation->getQuery()->shouldReceive('whereInRaw')->once()->with('table.foreign_key', [1, 2]);
$model1 = new EloquentHasManyModelStub;
Expand All @@ -212,6 +213,7 @@ public function testEagerConstraintsAreProperlyAdded()
public function testEagerConstraintsAreProperlyAddedWithStringKey()
{
$relation = $this->getRelation();
$relation->getParent()->shouldReceive('getKeyName')->once()->andReturn('id');
$relation->getParent()->shouldReceive('getKeyType')->once()->andReturn('string');
$relation->getQuery()->shouldReceive('whereIn')->once()->with('table.foreign_key', [1, 2]);
$model1 = new EloquentHasManyModelStub;
Expand Down
1 change: 1 addition & 0 deletions tests/Database/DatabaseEloquentHasOneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ public function testRelationIsProperlyInitialized()
public function testEagerConstraintsAreProperlyAdded()
{
$relation = $this->getRelation();
$relation->getParent()->shouldReceive('getKeyName')->once()->andReturn('id');
$relation->getParent()->shouldReceive('getKeyType')->once()->andReturn('int');
$relation->getQuery()->shouldReceive('whereInRaw')->once()->with('table.foreign_key', [1, 2]);
$model1 = new EloquentHasOneModelStub;
Expand Down
2 changes: 2 additions & 0 deletions tests/Database/DatabaseEloquentMorphTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function testMorphOneSetsProperConstraints()
public function testMorphOneEagerConstraintsAreProperlyAdded()
{
$relation = $this->getOneRelation();
$relation->getParent()->shouldReceive('getKeyName')->once()->andReturn('id');
$relation->getParent()->shouldReceive('getKeyType')->once()->andReturn('string');
$relation->getQuery()->shouldReceive('whereIn')->once()->with('table.morph_id', [1, 2]);
$relation->getQuery()->shouldReceive('where')->once()->with('table.morph_type', get_class($relation->getParent()));
Expand All @@ -51,6 +52,7 @@ public function testMorphManySetsProperConstraints()
public function testMorphManyEagerConstraintsAreProperlyAdded()
{
$relation = $this->getManyRelation();
$relation->getParent()->shouldReceive('getKeyName')->once()->andReturn('id');
$relation->getParent()->shouldReceive('getKeyType')->once()->andReturn('int');
$relation->getQuery()->shouldReceive('whereInRaw')->once()->with('table.morph_id', [1, 2]);
$relation->getQuery()->shouldReceive('where')->once()->with('table.morph_type', get_class($relation->getParent()));
Expand Down
4 changes: 3 additions & 1 deletion tests/Database/DatabaseEloquentMorphToManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ public function tearDown()
public function testEagerConstraintsAreProperlyAdded()
{
$relation = $this->getRelation();
$relation->getQuery()->shouldReceive('whereIn')->once()->with('taggables.taggable_id', [1, 2]);
$relation->getParent()->shouldReceive('getKeyName')->andReturn('id');
$relation->getParent()->shouldReceive('getKeyType')->andReturn('int');
$relation->getQuery()->shouldReceive('whereInRaw')->once()->with('taggables.taggable_id', [1, 2]);
$relation->getQuery()->shouldReceive('where')->once()->with('taggables.taggable_type', get_class($relation->getParent()));
$model1 = new EloquentMorphToManyModelStub;
$model1->id = 1;
Expand Down