Skip to content

Commit 2b271ac

Browse files
committed
Limit recommendation query scope for performance
1 parent 7dcc886 commit 2b271ac

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

app/Services/CocktailRecommendationService.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ public function recommend(BarMembership $barMembership, int $limit = 10): Collec
3030
{
3131
$barMembership->loadMissing('cocktailFavorites');
3232

33+
// Limit considerations for performance on large datasets
34+
$considerationsLimit = 750;
35+
36+
// Early exit if no favorites
37+
if ($barMembership->cocktailFavorites->isEmpty()) {
38+
return collect();
39+
}
40+
3341
$excludedCocktailIds = $this->getExcludedCocktails($barMembership);
3442

3543
// Collect all favorite tags
@@ -50,6 +58,7 @@ public function recommend(BarMembership $barMembership, int $limit = 10): Collec
5058
->where('ingredients.bar_id', $barMembership->bar_id)
5159
->join('ingredients', 'ingredients.id', '=', 'cocktail_ingredients.ingredient_id')
5260
->groupBy('ingredient_id')
61+
->limit($considerationsLimit)
5362
->get();
5463

5564
// Bar shelf ingredients
@@ -63,6 +72,7 @@ public function recommend(BarMembership $barMembership, int $limit = 10): Collec
6372
->whereNotIn('cocktails.id', $excludedCocktailIds)
6473
->where('cocktails.bar_id', $barMembership->bar_id)
6574
->with('tags', 'ingredients.ingredient', 'images')
75+
->limit($considerationsLimit)
6676
// ->inRandomOrder()
6777
->get();
6878

0 commit comments

Comments
 (0)