Skip to content

Conversation

Copy link

Copilot AI commented Nov 26, 2025

Database queries in order processing, menu display, and statistics views were triggering N+1 patterns, causing significant performance degradation under load.

Changes

pos_server/models.py

  • Dish.serialize_with_options(): Prefetch components/child_dishes, compute only_choices inline to avoid redundant query

pos_server/views.py

  • order_marking(), active_orders(): Add prefetch_related('orderdish_set__dish', 'delivery') and select_related('authorization')
  • pos() POST: Batch fetch dishes instead of per-item queries
  • day_stats(): Prefetch full order→dish→component→ingredient chain
  • compile_menu(): Prefetch dishcomponent_set__component__child_dishes
  • collect_order(): Use order.orderdish_set.all() to leverage prefetched data

online_store/views.py

  • dish(): Prefetch allergen chain dishcomponent_set__component__componentingredient_set__ingredient
  • place_order(): Batch fetch dishes with prefetched components

deliveries/views.py

  • ready_orders(): Replace Python iteration with single filtered query
  • profile(): Replace loop with .exists() check

inventory/views.py

  • craft_component(): Prefetch componentingredient_set__ingredient

Example

Before:

for dish_id, quantity in dish_counts.items():
    dish = Dish.objects.get(id=dish_id)  # N queries

After:

dishes_queryset = Dish.objects.filter(id__in=dish_ids).prefetch_related(
    'components__child_dishes',
    'dishcomponent_set__component'
)
dishes_map = {dish.id: dish for dish in dishes_queryset}  # 1 query

Also added warning logs for missing dishes during order processing.

Original prompt

Identify and suggest improvements to slow or inefficient code


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI self-assigned this Nov 26, 2025
Copilot AI and others added 2 commits November 26, 2025 18:40
… fix N+1 query issues

Co-authored-by: Collert <17819526+Collert@users.noreply.github.com>
… missing dishes

Co-authored-by: Collert <17819526+Collert@users.noreply.github.com>
Copilot AI changed the title [WIP] Identify and suggest improvements for slow code Fix N+1 query issues across order processing and menu display Nov 26, 2025
Copilot AI requested a review from Collert November 26, 2025 18:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants