chahinebrini dbc62b98ca perf(chat): index direct_messages + DB-side latest-per-partner query; remove unconditional Lyra welcome-back
- getDmConversations: DISTINCT ON (partner) ORDER BY partner, created_at DESC
  → one row per conversation in a single indexed query instead of fetching
  up to 500 rows and de-duplicating in JS
- add indexes on direct_messages (sender_id,created_at DESC),
  (receiver_id,created_at DESC), (receiver_id,read_at) — table had none, so
  every conversation-list load (runs per user on app launch for the badge)
  was a full-table scan + sort
- lyra.tsx: drop the welcome-back greeting that fired on every first coach
  open per session regardless of protection status/language (always German,
  unconditional). Endpoint kept for future conditional use

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-03 10:57:29 +02:00

19 lines
995 B
SQL

-- DM-Conversation-Liste + Unread-Badge Performance.
-- Vorher: 0 Indizes auf direct_messages → jeder Conversation-List-Load
-- (läuft bei JEDEM User beim App-Start fürs Tab-Badge, plus Refetch nach
-- Send/Focus/Back-Nav) war ein Full-Table-Scan + Sort. Skaliert nicht.
--
-- Deploy: pnpm prisma migrate deploy (auf Hetzner, via deploy-from-artifact.sh)
-- Conversation-Liste: DISTINCT ON (partner) ORDER BY partner, created_at DESC.
-- Ein Index pro Richtung (sender/receiver) deckt das OR-Predicate + Sort ab.
CREATE INDEX IF NOT EXISTS "direct_messages_sender_id_created_at_idx"
ON "rebreak"."direct_messages" ("sender_id", "created_at" DESC);
CREATE INDEX IF NOT EXISTS "direct_messages_receiver_id_created_at_idx"
ON "rebreak"."direct_messages" ("receiver_id", "created_at" DESC);
-- Unread-Badge: WHERE receiver_id = $1 AND read_at IS NULL.
CREATE INDEX IF NOT EXISTS "direct_messages_receiver_id_read_at_idx"
ON "rebreak"."direct_messages" ("receiver_id", "read_at");