perf(prisma): add missing hot-path composite indexes and cascade relations for 10k-100k+ scale
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 45/100
- Loại issue
- Tính năng
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- postgresql, typescript
Hướng nghiên cứu
Start with packages/prisma/schema.prisma and review the listed models and their current indexes and relations. Then inspect the cited query and cleanup call sites to confirm the proposed indexes match their filters and sort order. Add the listed indexes and cascade relations, handle orphaned records in the migration, and verify the generated migration and Prisma schema.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Problem Description
Several high-volume tables in packages/prisma/schema.prisma are missing composite indexes on hot query paths, and two tables lack onDelete: Cascade foreign keys to InstagramAccount:
Scale Impact (100 vs 10,000 vs 100,000+ Users)
- ~100 users (1k–10k rows): Sequential table scans fit in memory and finish in
< 5ms— everything works smoothly with no visible slowdown. - 1,000–10,000 users (500k–5M+ rows): Hot endpoints and webhook handlers scan hundreds of thousands of rows per request. DB CPU and disk I/O spike, query latency climbs to
200ms–2s+, and connection pools begin queuing under concurrent traffic. - 100,000+ users (10M–100M+ rows): Tables exceed PostgreSQL shared buffers and spill to disk. Webhook bursts, inbox loads, and nightly cleanup jobs take
5s–30s+, causing API timeouts, worker queue backlogs, and Meta webhook delivery failures.
Missing Indexes & Relations
InstagramScheduledPost(L1036-1061):- Missing
@@index([instagramMediaId])—instagram-comments.service.ts:116queries this on every incoming comment webhook across all tenants, forcing a full table scan. - Missing
@@index([companyId, status, scheduledAt])for scheduled post listings.
- Missing
Message(L548-572):- Missing
@@index([conversationId, sentAt])and@@index([conversationId, direction, sentAt])— scanned/sorted on every inbox load, cron follow-up check (cron.service.ts:305), and 24-hour messaging window check (v1-messages.controller.ts:53,v1-conversations.controller.ts:66,127).
- Missing
Conversation(L505-546):- Missing
@@index([instagramAccountId, archivedAt, lastMessageAt])for paginated inbox queries (v1-conversations.controller.ts:59-63).
- Missing
InstagramUserProfile(L250-293):- Missing
@@index([instagramAccountId, createdAt])and@@index([instagramAccountId, status])for CRM/Public API contact listing.
- Missing
AutomationExecution(L473-489) &ExecutionStep(L491-503):- Missing
@@index([automationId, instagramUserId, startedAt])(10s deduplication check ininstagram-comments.service.ts:247),@@index([automationId, startedAt]), and@@index([startedAt])(nightly purge incron.service.ts:557-568). ExecutionStepis missing@@index([executedAt])for nightly cleanup.
- Missing
BackgroundJob(L348-370):- Missing
@@index([status, completedAt])for nightly completed job purge (cron.service.ts:572).
- Missing
- Orphaned Records (
ContactCaptureL296-312 &BackgroundJobL348-370):- Both store
instagramAccountIdwithout an@relation(..., onDelete: Cascade)toInstagramAccount, leaving orphaned rows when an account or company is deleted.
- Both store
Expected Behavior
- All hot-path lookups, inbox sorts, webhook media checks, and nightly purges execute as fast index scans (
O(log N)) at 10k–100k+ users. - Deleting an
InstagramAccountautomatically cascades and cleans up itsContactCaptureandBackgroundJobrecords. - Zero breaking changes to existing application queries or API contracts.
Where to Look
packages/prisma/schema.prisma(InstagramScheduledPost,Message,Conversation,InstagramUserProfile,AutomationExecution,ExecutionStep,BackgroundJob,ContactCapture)
Solution / Implementation Guide
- Add the composite
@@index([...])definitions inpackages/prisma/schema.prismafor all 7 tables listed above. - Add
onDelete: Cascaderelations onContactCapture.instagramAccountIdandBackgroundJob.instagramAccountIdreferencingInstagramAccount. - Generate a safe Prisma migration:
- In the migration SQL, clean up any orphaned
ContactCapture/BackgroundJobrows (DELETE FROM ... WHERE "instagramAccountId" NOT IN (SELECT id FROM "InstagramAccount")) before adding the foreign key constraints. - Use
CREATE INDEX CONCURRENTLYif applying to a large live production database to avoid locking writes.
- In the migration SQL, clean up any orphaned
- Ngôn ngữ chính
- TypeScript
- Star
- 2
- Fork
- 1
- Merge trung bình
- 6 phút
- Pull request đã merge (30 ngày)
- 27
Chuẩn bị môi trường
- Có Dockerfile hoặc tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của sumamakhan761/tryinstance
-
backend good first issue
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
sumamakhan761/tryinstance#65 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
backend good first issue
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
sumamakhan761/tryinstance#64 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
backend good first issue
Độ khó 1/5 1-3 giờ Mức phù hợp với người mới 88/100
sumamakhan761/tryinstance#63 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
backend good first issue
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
sumamakhan761/tryinstance#62 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
backend good first issue
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
sumamakhan761/tryinstance#60 ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của sumamakhan761/tryinstance
Issue tương tự
-
提出物確認の詳細で状態バッジの重複と期限切れの欠落を直すĐang mởCodex使用可 かんたん コード
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
ryuki-moro/Nicomaru#348 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 77/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Bug
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 84/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
-
area-integrations
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
Maintainer thường phản hồi trong vòng 1 ngày