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:
@@ -0,0 +1,101 @@
|
||||
"""Indexes on the columns the screens look rows up by (UI review 2026-11-20).
|
||||
|
||||
The order lines had no index on their order, the orders none on their day or customer, and so
|
||||
on: every report and the customers list read whole tables once per order, so a few thousand
|
||||
orders made Customers take ~2 s and Reports → Days ~15 s (and time out). One index per column
|
||||
below; the models carry index=True so new databases get them too.
|
||||
|
||||
Revision ID: 0038_lookup_indexes
|
||||
Revises: 0037_tab_books
|
||||
Create Date: 2026-11-20 15:00:00
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
revision = '0038_lookup_indexes'
|
||||
down_revision = '0037_tab_books'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
INDEXES = [
|
||||
('orders', 'table_id'),
|
||||
('orders', 'business_day_id'),
|
||||
('orders', 'customer_id'),
|
||||
('orders', 'status'),
|
||||
('orders', 'opened_at'),
|
||||
('order_items', 'order_id'),
|
||||
('order_items', 'product_id'),
|
||||
('order_items', 'paid_in_shift_id'),
|
||||
('order_items', 'status'),
|
||||
('order_waiters', 'order_id'),
|
||||
('order_waiters', 'waiter_id'),
|
||||
('order_audit_log', 'order_id'),
|
||||
('order_discounts', 'order_id'),
|
||||
('print_jobs', 'order_id'),
|
||||
('print_log', 'order_id'),
|
||||
('waiter_shifts', 'waiter_id'),
|
||||
('waiter_shifts', 'business_day_id'),
|
||||
('tab_entries', 'tab_id'),
|
||||
('tab_entries', 'order_id'),
|
||||
('tab_entries', 'order_item_id'),
|
||||
('tab_payments', 'tab_id'),
|
||||
('tabs', 'customer_id'),
|
||||
('expenses', 'business_day_id'),
|
||||
('expenses', 'contact_id'),
|
||||
('expense_payments', 'expense_id'),
|
||||
('reservations', 'order_id'),
|
||||
('reservations', 'table_id'),
|
||||
('phone_calls', 'customer_id'),
|
||||
('waste_log', 'business_day_id'),
|
||||
('waste_log', 'product_id'),
|
||||
('tables', 'group_id'),
|
||||
('scheduled_shifts', 'user_id'),
|
||||
('shift_breaks', 'shift_id'),
|
||||
('chat_messages', 'conversation_id'),
|
||||
('conversation_participants', 'conversation_id'),
|
||||
('conversation_participants', 'user_id'),
|
||||
('staff_message_acks', 'message_id'),
|
||||
('price_modifier_conditions', 'modifier_id'),
|
||||
('price_modifier_targets', 'modifier_id'),
|
||||
('deal_conditions', 'deal_id'),
|
||||
('deal_targets', 'deal_id'),
|
||||
('product_ingredients', 'product_id'),
|
||||
('product_modifier_groups', 'product_id'),
|
||||
('product_options', 'product_id'),
|
||||
('product_preference_sets', 'product_id'),
|
||||
('product_preference_choices', 'set_id'),
|
||||
('product_quick_options', 'product_id'),
|
||||
('tablet_orders', 'order_id'),
|
||||
]
|
||||
|
||||
# what a person did in a time window (a shift's orders and lines)
|
||||
PAIRS = [
|
||||
('orders', ('opened_by', 'opened_at')),
|
||||
('order_items', ('added_by', 'added_at')),
|
||||
]
|
||||
|
||||
|
||||
def _existing(table: str) -> set[str]:
|
||||
return {ix['name'] for ix in sa.inspect(op.get_bind()).get_indexes(table)}
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
for table, column in INDEXES:
|
||||
name = f'ix_{table}_{column}'
|
||||
if name not in _existing(table):
|
||||
op.create_index(name, table, [column])
|
||||
for table, columns in PAIRS:
|
||||
name = f'ix_{table}_{"_".join(columns)}'
|
||||
if name not in _existing(table):
|
||||
op.create_index(name, table, list(columns))
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
for table, columns in PAIRS:
|
||||
name = f'ix_{table}_{"_".join(columns)}'
|
||||
if name in _existing(table):
|
||||
op.drop_index(name, table_name=table)
|
||||
for table, column in reversed(INDEXES):
|
||||
name = f'ix_{table}_{column}'
|
||||
if name in _existing(table):
|
||||
op.drop_index(name, table_name=table)
|
||||
Reference in New Issue
Block a user