Fix rolesPermissions cache query cartesian product (62k → 162 rows) (#19511)

## Summary
- **Split the `rolesPermissions` cache query into parallel queries** to
eliminate a 5-way LEFT JOIN cartesian product. A workspace with a role
having 5 objectPermissions × 4 permissionFlags × 124 fieldPermissions ×
5 RLP predicates × 5 RLP groups was returning 62,000 rows from just ~162
distinct records, causing 10s+ query timeouts on view updates.
- **Added missing `roleId` index on `permissionFlag` table** — the only
permission table without one, which was causing sequential scans on the
JOIN.
This commit is contained in:
Charles Bochet
2026-04-09 18:13:33 +02:00
committed by GitHub
parent 082822b790
commit 07745947ee
5 changed files with 121 additions and 34 deletions
@@ -17,7 +17,7 @@ export const regroupEntitiesByRelatedEntityId = <T extends AllMetadataName>({
entities,
foreignKey,
}: RegroupEntitiesByRelatedEntityIdArgs<T>) => {
const entitiesByRelatedEntityId = new Map<string, RegroupedEntity[]>();
const entitiesByRelatedEntityId = new Map<string, MetadataEntity<T>[]>();
for (const entity of entities) {
const currentRelatedEntityId = entity[
@@ -32,10 +32,7 @@ export const regroupEntitiesByRelatedEntityId = <T extends AllMetadataName>({
entitiesByRelatedEntityId.set(currentRelatedEntityId, []);
}
entitiesByRelatedEntityId.get(currentRelatedEntityId)!.push({
id: entity.id,
universalIdentifier: entity.universalIdentifier,
});
entitiesByRelatedEntityId.get(currentRelatedEntityId)!.push(entity);
}
return entitiesByRelatedEntityId;