perf: add missing indexes and remove N+1 queries

The app degraded progressively over a year of data growth. Four causes,
all cumulative:

1. No indexes. Apart from primary keys, unique constraints and
   session.expire, no table carried an index. PostgreSQL does not index
   foreign keys automatically, so every filter and join on group_id,
   supplier_id, order_id and the date columns did a sequential scan.
   Worst offender: user_groups.user_id, read by getUserWithGroups() on
   every authenticated request.

2. N+1 in the order and delivery listings. getOrders,
   getOrdersByDateRange, getDeliveries and getDeliveriesByDateRange
   issued one to two queries per row to load relations. Relations are now
   loaded in bulk and grouped in Node: three queries regardless of volume.

3. /api/sync-order-delivery-status reloaded the whole deliveries table on
   every iteration of its loop over orders. It now uses the deliveries
   getOrders() already attaches.

4. clearExpiredCache() was implemented but never called, so
   invoice_verification_cache grew without bound. Now scheduled every 6h.

Also replaces the full-history downloads on the Groups and Suppliers
pages, which fetched every order and delivery with nested relations only
to count rows, with aggregate endpoints that count in the database.

Indexes are created via scripts/auto-migrate-production.sh, the script
that actually runs at deploy time, using CREATE INDEX CONCURRENTLY so no
write lock is taken. Note that server/migrations.ts explicitly ignores
the migrations/ directory and runs only hardcoded migrations; the SQL
file added there is for reference and manual application.

Verified: typecheck baseline 440 errors, 430 after, none new in the
changed code; vite build passes; server boots; functional test confirms
the aggregate endpoints match the source data including the delivered
count.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
MichaelandClaude Fable 5 committed 2026-08-14 11:55:49 +02:00
1 parent 693d9460cb
commit 753b301066
9 files changed
+678 -290

No files matched your search

+132 -1
View File
@@ -111,4 +111,135 @@ else
echo "ℹ️ [AUTO-MIGRATE] Table webhook_bap_config existe déjà - aucune action nécessaire"
fi
echo "✅ [AUTO-MIGRATE] Migration terminée avec succès - webhook_bap_config incluse!"
# Index de performance
# Aucune table ne portait d'index en dehors des clés primaires et contraintes
# UNIQUE. PostgreSQL n'indexe pas les clés étrangères automatiquement : tous les
# filtres sur group_id / supplier_id / order_id et les colonnes de date
# faisaient un scan séquentiel complet, d'où la dégradation progressive à mesure
# que les tables grossissent.
#
# CREATE INDEX CONCURRENTLY ne verrouille pas la table en écriture, mais ne peut
# pas s'exécuter dans une transaction : d'où l'appel psql direct sans BEGIN.
echo "🔄 [AUTO-MIGRATE] Vérification des index de performance..."
if psql "$DATABASE_URL" -tAc "SELECT EXISTS (SELECT FROM pg_indexes WHERE schemaname='public' AND indexname='idx_deliveries_order_id');" | grep -q "f"; then
echo "🔧 [AUTO-MIGRATE] Création des index de performance (sans interruption de service)..."
# Une création CONCURRENTLY interrompue laisse un index invalide, que
# CREATE INDEX IF NOT EXISTS considère ensuite comme déjà présent. On purge
# ces reliquats avant de (re)créer.
psql "$DATABASE_URL" << 'EOF'
DO $$
DECLARE idx RECORD;
BEGIN
FOR idx IN
SELECT c.relname
FROM pg_index i
JOIN pg_class c ON c.oid = i.indexrelid
JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE NOT i.indisvalid AND n.nspname = 'public' AND c.relname LIKE 'idx_%'
LOOP
EXECUTE format('DROP INDEX IF EXISTS public.%I', idx.relname);
RAISE NOTICE 'Index invalide supprimé: %', idx.relname;
END LOOP;
END $$;
EOF
psql "$DATABASE_URL" << 'EOF'
-- user_groups : lu à chaque requête authentifiée via getUserWithGroups()
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_user_groups_user_id ON user_groups (user_id);
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_user_groups_group_id ON user_groups (group_id);
-- orders
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_orders_group_id ON orders (group_id);
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_orders_supplier_id ON orders (supplier_id);
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_orders_created_by ON orders (created_by);
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_orders_planned_date ON orders (planned_date);
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_orders_group_planned_date ON orders (group_id, planned_date);
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_orders_created_at ON orders (created_at DESC);
-- deliveries : order_id est la clé de jointure la plus sollicitée
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_deliveries_order_id ON deliveries (order_id);
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_deliveries_group_id ON deliveries (group_id);
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_deliveries_supplier_id ON deliveries (supplier_id);
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_deliveries_created_by ON deliveries (created_by);
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_deliveries_scheduled_date ON deliveries (scheduled_date);
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_deliveries_group_scheduled_date ON deliveries (group_id, scheduled_date);
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_deliveries_created_at ON deliveries (created_at DESC);
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_deliveries_invoice_reference ON deliveries (invoice_reference) WHERE invoice_reference IS NOT NULL;
-- reconciliation_comments
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_reconciliation_comments_delivery_id ON reconciliation_comments (delivery_id);
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_reconciliation_comments_group_id ON reconciliation_comments (group_id);
-- contacts
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_contacts_group_id ON contacts (group_id);
-- customer_orders
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_customer_orders_group_id ON customer_orders (group_id);
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_customer_orders_supplier_id ON customer_orders (supplier_id);
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_customer_orders_created_at ON customer_orders (created_at DESC);
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_customer_orders_group_status ON customer_orders (group_id, status);
-- dlc_products
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_dlc_products_group_id ON dlc_products (group_id);
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_dlc_products_supplier_id ON dlc_products (supplier_id);
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_dlc_products_expiry_date ON dlc_products (expiry_date);
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_dlc_products_group_status ON dlc_products (group_id, status);
-- tasks
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_tasks_group_id ON tasks (group_id);
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_tasks_created_by ON tasks (created_by);
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_tasks_due_date ON tasks (due_date);
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_tasks_group_status ON tasks (group_id, status);
-- avoirs
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_avoirs_group_id ON avoirs (group_id);
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_avoirs_supplier_id ON avoirs (supplier_id);
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_avoirs_created_at ON avoirs (created_at DESC);
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_avoirs_group_status ON avoirs (group_id, status);
-- sav_tickets
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_sav_tickets_group_id ON sav_tickets (group_id);
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_sav_tickets_supplier_id ON sav_tickets (supplier_id);
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_sav_tickets_created_at ON sav_tickets (created_at DESC);
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_sav_tickets_group_status ON sav_tickets (group_id, status);
-- invoice_verification_cache : expires_at sert la purge périodique
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_invoice_cache_expires_at ON invoice_verification_cache (expires_at);
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_invoice_cache_group_id ON invoice_verification_cache (group_id);
-- publicity_participations : la PK (publicity_id, group_id) ne couvre pas group_id seul
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_publicity_participations_group_id ON publicity_participations (group_id);
-- publicities
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_publicities_year ON publicities (year);
-- dashboard_messages
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_dashboard_messages_store_id ON dashboard_messages (store_id);
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_dashboard_messages_created_at ON dashboard_messages (created_at DESC);
-- weather_data
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_weather_data_date_location ON weather_data (date, location);
EOF
# Rafraîchit les statistiques du planificateur pour qu'il utilise réellement
# les nouveaux index sans attendre le prochain autovacuum.
psql "$DATABASE_URL" << 'EOF'
ANALYZE user_groups;
ANALYZE orders;
ANALYZE deliveries;
ANALYZE reconciliation_comments;
ANALYZE customer_orders;
ANALYZE dlc_products;
ANALYZE tasks;
ANALYZE avoirs;
ANALYZE sav_tickets;
ANALYZE invoice_verification_cache;
EOF
echo "✅ [AUTO-MIGRATE] Index de performance créés avec succès"
else
echo "ℹ️ [AUTO-MIGRATE] Index de performance déjà présents - aucune action nécessaire"
fi
echo "✅ [AUTO-MIGRATE] Migration terminée avec succès - index de performance inclus!"