Skip to content

Commit 26df8a4

Browse files
authored
Fix speed regression on model status query by going through eloquent model (#497)
* Fix speed regression on model status query by going through eloquent model * When we're plucking id, go for speed * Abstract out blink column cache
1 parent 3f518af commit 26df8a4

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/Entries/EntryQueryBuilder.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,7 @@ protected function column($column)
5555
$column = 'origin_id';
5656
}
5757

58-
$columns = Blink::once('eloquent-entry-data-column-mappings', fn () => array_merge(self::COLUMNS, (new EloquentEntry)->getDataColumnMappings($this->builder->getModel())));
59-
60-
if (! in_array($column, $columns)) {
58+
if (! in_array($column, $this->entryColumnsAndMappings())) {
6159
if (! Str::startsWith($column, 'data->')) {
6260
$column = 'data->'.$column;
6361
}
@@ -145,7 +143,7 @@ private function ensureCollectionsAreQueriedForStatusQuery(): void
145143
// If no IDs were queried, fall back to all collections.
146144
$ids->isEmpty()
147145
? $this->whereIn('collection', Collection::handles())
148-
: $this->whereIn('collection', app(static::class)->whereIn('id', $ids)->pluck('collection')->map(fn ($collection) => $collection?->handle)->filter()->unique()->values());
146+
: $this->whereIn('collection', app('statamic.eloquent.entries.model')::query()->whereIn('id', $ids)->distinct('collection')->pluck('collection')->values());
149147
}
150148

151149
private function getCollectionsForStatusQuery(): \Illuminate\Support\Collection
@@ -233,4 +231,18 @@ protected function getJsonCasts(): IlluminateCollection
233231
})
234232
->filter();
235233
}
234+
235+
public function pluck($column, $key = null)
236+
{
237+
if (! $key && in_array($column, $this->entryColumnsAndMappings())) {
238+
return $this->builder->pluck($column, $key);
239+
}
240+
241+
return parent::pluck($column, $key);
242+
}
243+
244+
private function entryColumnsAndMappings()
245+
{
246+
return Blink::once('eloquent-entry-data-column-mappings', fn () => array_merge(self::COLUMNS, (new EloquentEntry)->getDataColumnMappings($this->builder->getModel())));
247+
}
236248
}

0 commit comments

Comments
 (0)