perf(db): add missing indexes, fix N+1 batch delete, add pagination limits

- Add indexes on Resource(blueprintId, roleId), DemandRequirement(roleId),
  Assignment(roleId) — commonly filtered FK columns that were missing indexes
- Replace N+1 batch delete pattern (2N queries) with findAllocationEntries()
  that does 2 total queries via findMany({ id: { in: ids } })
- Add take/skip pagination with default limit of 500 to listDemands and
  listAssignments to prevent unbounded result sets

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-11 08:09:39 +02:00
parent 110e4ff1aa
commit c098cedf06
7 changed files with 264 additions and 158 deletions
@@ -0,0 +1,12 @@
-- Add missing indexes on foreign key columns used in filtering/joining.
-- These are CREATE INDEX IF NOT EXISTS so they are safe to re-run.
-- Resource: blueprintId and roleId are used in resource filtering and joins
CREATE INDEX CONCURRENTLY IF NOT EXISTS "resources_blueprintId_idx" ON "resources" ("blueprintId");
CREATE INDEX CONCURRENTLY IF NOT EXISTS "resources_roleId_idx" ON "resources" ("roleId");
-- DemandRequirement: roleId is used in role-based demand queries
CREATE INDEX CONCURRENTLY IF NOT EXISTS "demand_requirements_roleId_idx" ON "demand_requirements" ("roleId");
-- Assignment: roleId is used in role-based assignment queries
CREATE INDEX CONCURRENTLY IF NOT EXISTS "assignments_roleId_idx" ON "assignments" ("roleId");
+4
View File
@@ -914,6 +914,8 @@ model Resource {
@@index([orgUnitId])
@@index([resourceType])
@@index([managementLevelGroupId])
@@index([blueprintId])
@@index([roleId])
@@map("resources")
}
@@ -1323,6 +1325,7 @@ model DemandRequirement {
@@index([startDate, endDate])
@@index([status])
@@index([projectId, status, startDate])
@@index([roleId])
@@map("demand_requirements")
}
@@ -1362,6 +1365,7 @@ model Assignment {
@@index([projectId, status, startDate, endDate])
@@index([resourceId, status, endDate])
@@index([projectId, status, endDate])
@@index([roleId])
@@map("assignments")
}