perf(backend): index the lookup columns and load order lines in batches

Order lines had no index on their order, orders none on their day/customer/status, and each
line's product was loaded to tell a fee apart: with ~3000 orders Customers took ~2 s and
Reports -> Days ~15 s. Migration 0038 adds the indexes (models carry index=True), Order.items
loads with selectin, sales.lines looks the fee products up once, cancellations and prep-zones
load their orders with the lines. Days 15 s -> 0.2 s (a month), customers 2 s -> 0.1 s.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-20 14:42:09 +02:00
co-authored by Claude Opus 5.5
parent fd917797a7
commit 618fef53ef
17 changed files with 167 additions and 62 deletions
+6 -6
View File
@@ -135,7 +135,7 @@ class ProductModifierGroup(Base):
__tablename__ = "product_modifier_groups"
id = Column(Integer, primary_key=True, index=True)
product_id = Column(Integer, ForeignKey("products.id"), nullable=False)
product_id = Column(Integer, ForeignKey("products.id"), nullable=False, index=True)
# "option" | "ingredient" | "preference"
modifier_type = Column(String, nullable=False)
name = Column(String, nullable=False)
@@ -148,7 +148,7 @@ class ProductOption(Base):
__tablename__ = "product_options"
id = Column(Integer, primary_key=True, index=True)
product_id = Column(Integer, ForeignKey("products.id"), nullable=False)
product_id = Column(Integer, ForeignKey("products.id"), nullable=False, index=True)
name = Column(String, nullable=False)
extra_cost = Column(Float, default=0.0)
allow_multiple = Column(Boolean, default=False, nullable=False)
@@ -170,7 +170,7 @@ class ProductQuickOption(Base):
__tablename__ = "product_quick_options"
id = Column(Integer, primary_key=True, index=True)
product_id = Column(Integer, ForeignKey("products.id"), nullable=False)
product_id = Column(Integer, ForeignKey("products.id"), nullable=False, index=True)
name = Column(String, nullable=False)
price = Column(Float, default=0.0, nullable=False)
allow_multiple = Column(Boolean, default=False, nullable=False)
@@ -186,7 +186,7 @@ class ProductIngredient(Base):
__tablename__ = "product_ingredients"
id = Column(Integer, primary_key=True, index=True)
product_id = Column(Integer, ForeignKey("products.id"), nullable=False)
product_id = Column(Integer, ForeignKey("products.id"), nullable=False, index=True)
name = Column(String, nullable=False)
extra_cost = Column(Float, default=0.0)
is_favorite = Column(Boolean, default=False, nullable=False)
@@ -203,7 +203,7 @@ class ProductPreferenceSet(Base):
__tablename__ = "product_preference_sets"
id = Column(Integer, primary_key=True, index=True)
product_id = Column(Integer, ForeignKey("products.id"), nullable=False)
product_id = Column(Integer, ForeignKey("products.id"), nullable=False, index=True)
name = Column(String, nullable=False)
default_choice_id = Column(Integer, nullable=True)
# JSON: {name, default_choice_index, choices:[{name,extra_cost,is_default}]}
@@ -226,7 +226,7 @@ class ProductPreferenceChoice(Base):
__tablename__ = "product_preference_choices"
id = Column(Integer, primary_key=True, index=True)
set_id = Column(Integer, ForeignKey("product_preference_sets.id"), nullable=False)
set_id = Column(Integer, ForeignKey("product_preference_sets.id"), nullable=False, index=True)
name = Column(String, nullable=False)
extra_cost = Column(Float, default=0.0)
# JSON array of sub-choice objects: [{name, extra_cost, is_default, is_compact}]