perf: Optmize DB queries around attribute routing (#27600)

* Replace n x m array functions with maps

* feat: add attributeIds filter to findManyByOrgMembershipIds

Allow filtering attribute-to-user assignments by specific attribute IDs.
This enables querying only the attributes needed for a routing rule
instead of fetching all attributes for all team members.

Co-Authored-By: Claude Opus 4.5 <[email protected]>

* feat: extract attribute IDs from routing rules and filter queries

Add extractAttributeIdsFromQueryValue helper to parse routing rules
and identify which attributes are referenced. Pass these IDs through
getAttributesAssignmentData to _queryAllData to only fetch the
attribute assignments that are actually needed for evaluation.

For a team with 785 members and 14 attributes, if a routing rule
only references 2-3 attributes, this reduces the query from ~3500
rows to ~500 rows.

Co-Authored-By: Claude Opus 4.5 <[email protected]>

* feat: use attribute filtering in findTeamMembersMatchingAttributeLogic

Extract attribute IDs from the routing rules (main + fallback) and
pass them to getAttributesForLogic to only fetch necessary data.

This completes the optimization to reduce database load when
evaluating routing rules that only reference a subset of attributes.

Co-Authored-By: Claude Opus 4.5 <[email protected]>

* feat: filter attributes query by attribute IDs

Also filter the attributes query (not just attributeToUser assignments)
by the attribute IDs referenced in routing rules.

For a routing rule that references 2 out of 14 attributes with 6524
total options, this reduces:
- Attributes fetched: 14 → 2
- Options fetched: 6524 → ~930 (proportional reduction)
- Fewer regex replacements in replaceAttributeOptionIdsWithOptionLabel

Co-Authored-By: Claude Opus 4.5 <[email protected]>

* test: add comprehensive test coverage for attribute routing query optimization

Co-Authored-By: [email protected] <[email protected]>

* Revert "Replace n x m array functions with maps"

This reverts commit 312eeb03bd1aba19760f093c798861a23b5fc66f.

* fix: re-establish extractAttributeIdsFromQueryValue mock after resetAllMocks

Co-Authored-By: [email protected] <[email protected]>

---------

Co-authored-by: Claude Opus 4.5 <[email protected]>
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
authored and GitHub committed 2026-02-04 11:28:46 -05:00
1 parent 1da2edb9e2
commit 8d79209e74
6 files changed
+620 -8

No files matched your search

@@ -16,6 +16,7 @@ import {
vi.mock("@calcom/features/attributes/lib/getAttributes", () => {
return {
getAttributesAssignmentData: vi.fn(),
extractAttributeIdsFromQueryValue: vi.fn().mockReturnValue([]),
};
});
@@ -265,6 +266,8 @@ function buildScenarioWhereMainAttributeLogicFails() {
describe("findTeamMembersMatchingAttributeLogic", () => {
beforeEach(() => {
vi.resetAllMocks();
// Re-establish the mock for extractAttributeIdsFromQueryValue after resetAllMocks
vi.mocked(getAttributesModule.extractAttributeIdsFromQueryValue).mockReturnValue([]);
});
it("should return null if the route does not have an attributesQueryValue set", async () => {
@@ -1,6 +1,9 @@
import { acrossQueryValueCompatiblity, raqbQueryValueUtils } from "@calcom/app-store/_utils/raqb/raqbUtils";
import type { Attribute } from "@calcom/app-store/routing-forms/types/types";
import { getAttributesAssignmentData } from "@calcom/features/attributes/lib/getAttributes";
import {
extractAttributeIdsFromQueryValue,
getAttributesAssignmentData,
} from "@calcom/features/attributes/lib/getAttributes";
import type { RoutingFormTraceService } from "@calcom/features/routing-trace/domains/RoutingFormTraceService";
import { RaqbLogicResult } from "@calcom/lib/raqb/evaluateRaqbLogic";
import jsonLogic from "@calcom/lib/raqb/jsonLogic";
@@ -435,9 +438,19 @@ async function runFallbackAttributeLogic(data: RunAttributeLogicData, options: R
};
}
export async function getAttributesForLogic({ teamId, orgId }: { teamId: number; orgId: number }) {
export async function getAttributesForLogic({
teamId,
orgId,
attributeIds,
}: {
teamId: number;
orgId: number;
/** If provided, only fetch attribute assignments for these attribute IDs.
* This significantly improves performance when only a few attributes are needed. */
attributeIds?: string[];
}) {
const [result, ttAttributes] = await asyncPerf(async () => {
return getAttributesAssignmentData({ teamId, orgId });
return getAttributesAssignmentData({ teamId, orgId, attributeIds });
});
return {
@@ -482,6 +495,9 @@ export async function findTeamMembersMatchingAttributeLogic(
routeIsFallback,
} = data;
// Extract attribute IDs from the routing rules to only fetch necessary data
const attributeIds = extractAttributeIdsFromQueryValue(attributesQueryValue, fallbackAttributesQueryValue);
const {
attributesOfTheOrg,
teamMembersWithAttributeOptionValuePerAttribute,
@@ -489,6 +505,8 @@ export async function findTeamMembersMatchingAttributeLogic(
} = await getAttributesForLogic({
teamId,
orgId,
// Only pass attributeIds if we found some - otherwise fetch all (backwards compatible)
attributeIds: attributeIds.length > 0 ? attributeIds : undefined,
});
const runAttributeLogicOptions = {