Fix N+1 query issues across order processing and menu display #38
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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, computeonly_choicesinline to avoid redundant querypos_server/views.py
order_marking(),active_orders(): Addprefetch_related('orderdish_set__dish', 'delivery')andselect_related('authorization')pos()POST: Batch fetch dishes instead of per-item queriesday_stats(): Prefetch full order→dish→component→ingredient chaincompile_menu(): Prefetchdishcomponent_set__component__child_dishescollect_order(): Useorder.orderdish_set.all()to leverage prefetched dataonline_store/views.py
dish(): Prefetch allergen chaindishcomponent_set__component__componentingredient_set__ingredientplace_order(): Batch fetch dishes with prefetched componentsdeliveries/views.py
ready_orders(): Replace Python iteration with single filtered queryprofile(): Replace loop with.exists()checkinventory/views.py
craft_component(): Prefetchcomponentingredient_set__ingredientExample
Before:
After:
Also added warning logs for missing dishes during order processing.
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.