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

chore: Optimize Loads on Queries #4220

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
13 changes: 3 additions & 10 deletions mealie/repos/repository_recipes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from pydantic import UUID4
from slugify import slugify
from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm import InstrumentedAttribute, joinedload
from sqlalchemy.orm import InstrumentedAttribute
from typing_extensions import Self

from mealie.db.models.recipe.category import Category
Expand Down Expand Up @@ -165,15 +165,6 @@ def page_all( # type: ignore
pagination_result = pagination.model_copy()
q = sa.select(self.model)

args = [
joinedload(RecipeModel.recipe_category),
joinedload(RecipeModel.tags),
joinedload(RecipeModel.tools),
joinedload(RecipeModel.user),
]

q = q.options(*args)

fltr = self._filter_builder()
q = q.filter_by(**fltr)

Expand Down Expand Up @@ -212,6 +203,8 @@ def page_all( # type: ignore

q, count, total_pages = self.add_pagination_to_query(q, pagination_result)

# Apply options late, so they do not get used for counting
q = q.options(*RecipeSummary.loader_options())
try:
data = self.session.execute(q).scalars().unique().all()
except Exception as e:
Expand Down
9 changes: 6 additions & 3 deletions mealie/schema/household/group_shopping_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
ShoppingListRecipeReference,
)
from mealie.db.models.recipe import IngredientFoodModel, RecipeModel
from mealie.db.models.users.users import User
from mealie.schema._mealie import MealieModel
from mealie.schema._mealie.mealie_model import UpdatedAtField
from mealie.schema._mealie.types import NoneFloat
Expand Down Expand Up @@ -137,7 +138,9 @@ def loader_options(cls) -> list[LoaderOption]:
joinedload(ShoppingListItem.label),
joinedload(ShoppingListItem.unit),
selectinload(ShoppingListItem.recipe_references),
joinedload(ShoppingListItem.shopping_list).joinedload(ShoppingList.user),
joinedload(ShoppingListItem.shopping_list)
.joinedload(ShoppingList.user)
.load_only(User.household_id, User.group_id),
]


Expand Down Expand Up @@ -232,7 +235,7 @@ def loader_options(cls) -> list[LoaderOption]:
.joinedload(ShoppingListRecipeReference.recipe)
.joinedload(RecipeModel.tools),
selectinload(ShoppingList.label_settings).joinedload(ShoppingListMultiPurposeLabel.label),
joinedload(ShoppingList.user),
joinedload(ShoppingList.user).load_only(User.household_id, User.group_id),
]


Expand Down Expand Up @@ -279,7 +282,7 @@ def loader_options(cls) -> list[LoaderOption]:
.joinedload(ShoppingListRecipeReference.recipe)
.joinedload(RecipeModel.tools),
selectinload(ShoppingList.label_settings).joinedload(ShoppingListMultiPurposeLabel.label),
joinedload(ShoppingList.user),
joinedload(ShoppingList.user).load_only(User.household_id, User.group_id),
]


Expand Down
10 changes: 10 additions & 0 deletions mealie/schema/household/household_preferences.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from pydantic import UUID4, ConfigDict
from sqlalchemy.orm import joinedload
from sqlalchemy.orm.interfaces import LoaderOption

from mealie.db.models.household.household import Household
from mealie.db.models.household.preferences import HouseholdPreferencesModel
from mealie.schema._mealie import MealieModel


Expand Down Expand Up @@ -27,3 +31,9 @@ class SaveHouseholdPreferences(UpdateHouseholdPreferences):
class ReadHouseholdPreferences(CreateHouseholdPreferences):
id: UUID4
model_config = ConfigDict(from_attributes=True)

@classmethod
def loader_options(cls) -> list[LoaderOption]:
return [
joinedload(HouseholdPreferencesModel.household).load_only(Household.group_id),
]
3 changes: 2 additions & 1 deletion mealie/schema/meal_plan/new_meal.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from mealie.db.models.household import GroupMealPlan
from mealie.db.models.recipe import RecipeModel
from mealie.db.models.users.users import User
from mealie.schema._mealie import MealieModel
from mealie.schema.recipe.recipe import RecipeSummary
from mealie.schema.response.pagination import PaginationBase
Expand Down Expand Up @@ -66,7 +67,7 @@ def loader_options(cls) -> list[LoaderOption]:
selectinload(GroupMealPlan.recipe).joinedload(RecipeModel.recipe_category),
selectinload(GroupMealPlan.recipe).joinedload(RecipeModel.tags),
selectinload(GroupMealPlan.recipe).joinedload(RecipeModel.tools),
selectinload(GroupMealPlan.user),
selectinload(GroupMealPlan.user).load_only(User.household_id),
]


Expand Down
10 changes: 10 additions & 0 deletions mealie/schema/recipe/recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from sqlalchemy.orm.interfaces import LoaderOption

from mealie.core.config import get_app_dirs
from mealie.db.models.users.users import User
from mealie.schema._mealie import MealieModel, SearchType
from mealie.schema._mealie.mealie_model import UpdatedAtField
from mealie.schema.response.pagination import PaginationBase
Expand Down Expand Up @@ -121,6 +122,15 @@ def clean_strings(val: Any):

return val

@classmethod
def loader_options(cls) -> list[LoaderOption]:
return [
joinedload(RecipeModel.recipe_category),
joinedload(RecipeModel.tags),
joinedload(RecipeModel.tools),
joinedload(RecipeModel.user).load_only(User.household_id),
]


class RecipePagination(PaginationBase):
items: list[RecipeSummary]
Expand Down
3 changes: 2 additions & 1 deletion mealie/schema/recipe/recipe_timeline_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from mealie.core.config import get_app_dirs
from mealie.db.models.recipe.recipe_timeline import RecipeTimelineEvent
from mealie.db.models.users.users import User
from mealie.schema._mealie import MealieModel
from mealie.schema._mealie.mealie_model import UpdatedAtField
from mealie.schema.recipe.recipe import Recipe
Expand Down Expand Up @@ -67,7 +68,7 @@ class RecipeTimelineEventOut(RecipeTimelineEventCreate):
def loader_options(cls) -> list[LoaderOption]:
return [
joinedload(RecipeTimelineEvent.recipe),
joinedload(RecipeTimelineEvent.user),
joinedload(RecipeTimelineEvent.user).load_only(User.household_id, User.group_id),
]

@classmethod
Expand Down
8 changes: 8 additions & 0 deletions mealie/schema/user/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
from sqlalchemy.orm.interfaces import LoaderOption

from mealie.core.config import get_app_dirs, get_app_settings
from mealie.db.models.recipe.recipe import RecipeModel
from mealie.db.models.users import User
from mealie.db.models.users.user_to_recipe import UserToRecipe
from mealie.db.models.users.users import AuthMethod, LongLiveToken
from mealie.schema._mealie import MealieModel
from mealie.schema.group.group_preferences import ReadGroupPreferences
Expand Down Expand Up @@ -88,6 +90,12 @@ class UserRatingUpdate(MealieModel):
class UserRatingOut(UserRatingCreate):
id: UUID4

@classmethod
def loader_options(cls) -> list[LoaderOption]:
return [
joinedload(UserToRecipe.recipe).joinedload(RecipeModel.user).load_only(User.household_id, User.group_id)
]


class UserRatings(BaseModel, Generic[DataT]):
ratings: list[DataT]
Expand Down
Loading