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.x] Fix slowdown caused by status PR #9928

Merged
merged 3 commits into from
Apr 19, 2024
Merged
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
36 changes: 36 additions & 0 deletions src/Stache/Query/EntryQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Statamic\Contracts\Entries\QueryBuilder;
use Statamic\Entries\EntryCollection;
use Statamic\Facades;
use Statamic\Facades\Blink;
use Statamic\Facades\Collection;
use Statamic\Support\Arr;

Expand Down Expand Up @@ -69,6 +70,39 @@ protected function getFilteredKeys()
: $this->getKeysFromCollectionsWithWheres($collections, $this->wheres);
}

private function addCollectionWheres(): void
{
$this->collections = $this->getCollectionWheres();
}

private function getCollectionWheres(): array
{
// If the collections property isn't empty, it means the user has explicitly
// queried for them. In that case, we'll use them and skip the auto-detection.
if (! empty($this->collections)) {
return $this->collections;
}

// Otherwise, we'll detect them by looking at where clauses targeting the "id" column.
$ids = collect($this->wheres)->where('column', 'id')->flatMap(fn ($where) => $where['values'] ?? [$where['value']]);

// If no IDs were queried, fall back to all collections.
if ($ids->isEmpty()) {
return Collection::handles()->all();
}

return Blink::once('entry-to-collection-map', function () {
return Collection::handles()
->flatMap(fn ($collection) => $this->getWhereColumnKeysFromStore($collection, ['column' => 'collectionHandle']))
->keys()
->mapWithKeys(function ($value) {
[$collection, $id] = explode('::', $value);

return [$id => $collection];
});
})->only($ids->all())->unique()->values()->all();
}

protected function getKeysFromCollections($collections)
{
return collect($collections)->flatMap(function ($collection) {
Expand Down Expand Up @@ -146,6 +180,8 @@ protected function getWhereColumnKeyValuesByIndex($column)

public function whereStatus(string $status)
{
$this->addCollectionWheres();

if (! in_array($status, self::STATUSES)) {
throw new \Exception("Invalid status [$status]");
}
Expand Down
Loading